git pull origin master via VSTS Rest API - azure-devops

I would like to merge two branches using visual studio team services API.
In other words, I have a branch features/feature1 and I would like to execute "git pull origin master" in that branch?
Can I do that using the API without going through a pull request?
Also, in case there are merge conflicts, can I resolve them by simply choosing source or target version?

There is no such REST API to merge two branches or execute git pull command since git commands can be execute directly or by calling some functions (no matter for command line, powershell or C# etc).
You can run git pull command for your code as the way how git commands executed.
And when execute git pull command and to solve the merge conflicts automatically by adding -X options:
Choose source (features/feature1 branch) version: git pull origin master -X theirs
Choose target (master branch) version: git pull origin master -X ours

Related

How to make a Pull Request using the new github cli, to a remote repo without pushing a remote branch too?

I am trying to figure out how to make a PR to my remote repository from a local branch (or even from local master/main branch). However, no matter what I do I get the following error:
Attempt from local main:
(master)$ gh pr create --title "Adding readme" --body "Testing pr from cli" --head armsp:feature
Creating pull request for armsp:feature into master in armsp/----
pull request create failed: GraphQL error: Head sha can't be blank, Base sha can't be blank, No commits between master and feature, Head ref must be a branch
Attempt from the local feature branch:
(feature)$ gh pr create --title "Adding readme" --body "Testing pr from cli" --head armsp:feature
Creating pull request for armsp:feature into master in armsp/----
pull request create failed: GraphQL error: Head sha can't be blank, Base sha can't be blank, No commits between master and feature, Head ref must be a branch
The general steps for the whole situation is -
Commit and push some files from local main to remote main
Make a new local branch feature, edit something, commit
PR
Use --head arguement of gh from local branch to make PR directly to remote without making the same remote branch
Use --head arguement of gh from the local master without making a remote branch
I have seen a couple of issues on the github cli repo and they seem to have been fixed in a release, but it unfortunately still doesn't work for me.
Issue 1: https://github.com/cli/cli/issues/1820
Issue 2: https://github.com/cli/cli/issues/1709
My gh version
$ gh version
gh version 1.2.1 (2020-11-11)
NOTE: It is IMPERATIVE that I make the PR completely via terminal/cli.
You can't, you should at least create a branch on the remote first.
After the mandatory introduction on what is a Pull Request in Git vs GitHub, I'll quote the following:
Pull requests let you tell others about changes you've pushed to a
branch in a repository on GitHub. Once a pull request is opened, you
can discuss and review the potential changes with collaborators and
add follow-up commits before your changes are merged into the base
branch. source.
GitHub PR expect some code on the remote GitHub server, at least a branch.
Create a pull request to propose and collaborate on changes to a
repository. These changes are proposed in a branch, which ensures that
the default branch only contains finished and approved work. source.
You expect to open a Pull Request on remote for a branch that doesn't exists. Create the branch first, then try again. Remember that you won't be able to have the remote automatically fetching or pulling content from your local to the remote, so in the end you'll have to push it.

How to automate the master merge after releases for Continuous Delivery

What is the best practice of automation of master merge?
Right now we are doing it manually. Should we do it from the CD(vsts) pipeline using a merge task? What happens in case of a merge failure?
This is the merge task we have found in the marketplace. Which is not working as expected.
https://marketplace.visualstudio.com/items?itemName=dtzar.git-merge
You can merge other branches into master branch automatically by Powershell script. Details as below:
Add a Powershell task in the end of the release definition, with the scripts:
git clone https://<alternate username>:<Alternate password>#account.visualstudio.com/project/_git/reponame repo
cd repo
git checkout $(BUILD.SOURCEBRANCHNAME)
git checkout master
git merge $(BUILD.SOURCEBRANCHNAME) -X ours -m 'merge $(BUILD.SOURCEBRANCHNAME) into master'
git push origin master
Note: for the workflow, there mainly has no conflicts for merging branches into master. the -X ours option for git merge is using the master version if there has conflicts. And you can also use -X theris instead to make the conflicts use the source branch's version.

On-premises GIT repository with Visual Studio Team Services Project

We have on-premises installed GIT. There we have our code repositories.
Is it possible to connect a repository from this on-premises instance to Visual studio team services project?
So they display under "Code" bar?
vsts
I need it hosted on premises, but see code changes/commits and other GIT stuff in VSTS project
No, it isn’t supported to display files or code of another repository under Code bar. You need to import that repository to the repository in VSTS. After that, you can push updates to VSTS repository if there are changes in your on-premises git repository.
You can't connect on premise GIT to VSTS. You may however use the VSTS rest APIs to push in code from your on premise GIT to VSTS.
Typically you will setup a hook/trigger on your on premise GIT repo in order to automate the replication process.
As others have said, you can have a "git hook", which is basically a git trigger to take action on some event. In this case, when code is pushed, to push it up to VSTS, but I assume you need to know the technical commands.
I had to do this the opposite way, and I did this quick and dirty. This pushes everything in one go, not each commit. This can also catch up a repo that is behind.
# Clone source repo (your local git repo)
git clone some_repo_path_goes_here
# I am skipping steps and assuming you are only syncing master branch.
# I have code to get all branches down before proceeding, but not posting it here.
# Assuming tags are on master branch..
# Get all tags
git fetch origin --tags
# Test to see if remote alias already exists
git ls-remote http://path_to_.visualstudio.com/org/project/_git/TargetRepoSameName
# Add a remote alias
git remote add any_name_123 http://path_to_.visualstudio.com/org/project/_git/TargetRepoSameName
# push local repo to 'any_name_123'
git push any_name_123 --all
# optional: delete all tags before attempting to push local tags
git push any_name_123 --delete `$(git tag -l)
# push local tags to remote repo
git push any_name_123 --tags
You can schedule this job if you would like. I have a PowerShell job to do this with a lot more functions to do pull down the branches.

git fetch not working for fetch new files

I am new to Github and have checked lots of examples for fetching and pulling files from the Git server. However, my fetch command does not work as I expected; my pull command works as I expected and downloads new files onto my system. Is the fetch command not able to download files locally to my branch?
You need to understand the difference between fetching and pulling. When you do a fetch:
git fetch
you update all the local tracking branches in your Git folder. The tracking branches are your local copy of what is actually on the repository, and it is these branches which Git uses for most operations. This means that every tracking branch will now be in sync with the latest changes from GitHub. However, this does not mean that the local branch you have is now up to date. To bring your local branch up to date you have to additionally either merge or rebase that branch on the remote tracking branch.
Assuming you are on the master branch, you can remember what git pull does as follows:
git pull = git fetch + git merge origin/master
In other words, there is merge here to actually get the changes from the remote repository into your local branch. You could also rebase your local branch on the version in the remote via git pull --rebase master.
So the answer to your question is that the fetch command absolutely does bring in changes from the remote to your system, but you have to additionally merge or rebase to update your local working branch.

What does it mean "to fetch" before I can push?

I uploaded my whole project on my repository of github yesterday.
I'd like to update the online version today, and when I use git push -u origin masterorder, the bash window says:
! [rejected] master -> master (fetch first)
So how do I "fetch"?
If I use git pull first, would my local files be overwritten by the online version?
So how do I "fetch"?
By using the command:
git fetch
Basically, your local repo is out of sync with the remote (Github) repository. If you have multiple remote repos, you can specify which one, and you can also specify which branch. Try
git help fetch
for a complete description of the command and the various parameters you can pass in.
If I use git pull first, would my local files be overwritten by the online version?
git pull is like doing a git fetch followed by git merge -- that is, it gets the updates from the remote and attempts to merge those into your local files. That may be fine, or not, depending on what you intend. Read the help for both commands so that you can make an informed decision.