How to automate the master merge after releases for Continuous Delivery - merge

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.

Related

How to merge only specific files from a pull request?

Is it possible to discard a file when merging a pull-request? Also, how should I edit a file if there is no conflict and then merge the pull-request?
Thanks! :)
In the PR you should have two branches let's call them master and develop.
Checkout to the develop branch in your local env.
git checkout develop
Then you can discard any file in this branch with the checkout command
git checkout master DISCARDED_FILE_WITH_PATH
This command above will grab the file from the master, so basically you discard the change.
Then commit the change
git commit -am 'discard the file'
and push back to the repo
git push
If you reload the PR, the file will missing.

Do I Need to Pull From Github Before Pushing the Project From Another Machine? [duplicate]

Is there any way of simulating a git merge between two branches, the current working branch and the master, but without making any changes?
I often have conflicts when I have to make a git merge. Is there any way of simulating the merge first?
You can use git merge --no-commit to prevent the merge from actually being committed, and if you don't like how the merge works out, just reset to the original head.
If you definitely don't want to finalize the merge, even if it's a fast-forward (and thus has no conflicts, by definition), you could add --no-ff as well.
I don't think there is a way of simulating what will happen until you try the merge. However, if you make sure that the output of git status is empty before you do the merge, it is quite safe to just go ahead and try it. If you get conflicts, you can immediately get back to the state you were at before with:
git reset --merge
Since git 1.7.4, you can also abort the merge by doing:
git merge --abort
(As the commit message that added that option explains, this was added for consistency with git rebase --abort and so on.)
If I want to compare changes on a topic branch to master, I find it easiest and safest to do the following:
git checkout master
git checkout -b trial_merge
git merge topic_branch
After completing the merge, it is easy to see the consolidated change from master
git diff master
When done, simply delete the trial_merge branch
git checkout master
git branch -D trial_merge
This way, the master branch never changes.
Here is the solution that I have found: git merge-tree does merging "in memory" and prints the diff without touching your working directory. You can even test a branch without checking it out.
Get the merge diff
First, do this to make sure your repository knows about all the remote branches:
$ git fetch --all
Now use this bash snippet to see how branch $branch would merge into $master:
$ branch='feature'
$ git merge-tree $(git merge-base $branch master) master $branch
No changes are made to your workdir or index. It's a dry-run merge.
Pick information from the output
The output is a diff.
In case the branch has been merged, it will be empty.
To find whether there are conflicts, grep it for <<<:
$ git merge-tree $(git merge-base $branch master) master $branch | fgrep '<<<'
To extract conflict diffs, use sed to extract lines between <<< and >>>:
$ git merge-tree $(git merge-base $branch master) master $branch | \
sed -ne '/^\+<<</,/^\+>>>/ p'
Features
The diff will be empty if a branch is already merged
Use grep/sed to extract conflicts information
Use origin/feature to test branches you've never worked with
Can be used to see how 2 branches have diverged
Add it to your favorites
Get the diff of the merge:
git config --global alias.mergediff '!f(){ branch="$1" ; into="$2" ; git merge-tree $(git merge-base "$branch" "$into") "$into" "$branch" ; };f '
Usage:
$ git mergediff <feature-branch> <merge-into>
$ git mergediff feature master
Get merge conflicts:
git config --global alias.mergetest '!f(){ git mergediff $# | sed -ne "/^+<<</,/^+>>>/ p" ; };f '
Usage:
$ git mergetest <feature-branch> <merge-into>
$ git mergetest feature master
Why not just create a throwaway branch (git checkout -b), and do a test merge there?
I use :
git merge --ff-only
according to documentation:
Refuse to merge and exit with a non-zero status unless the current HEAD is already up-to-date or the merge can be resolved as a fast-forward.
It's not really a simulation because there will be a fast-forward merge in case of no conflicts between the two branches. But in case of conflicts, you will be informed and nothing will happens.
I've been able to use git merge --abort, recently. However, this can only be used if there is a merge conflict. If you are sure that you will not want to commit, then use the other mentioned methods above.
I don't know exactly if it is your case, but your question remember me that sometimes I start a feature, I commit over the days and I merge the develop on it many times.
On this point I lose the control over the exact files I changed and I will only know it when my feature were closed and my code go to develop.
In this case, a good way to know what modifications you did (not other from the merges) is using Sourcetree.
You must click with the right button on the base branch and select Diff Against Current:
Then sourcetree will show you all the modifications that will be merged if you merge your branch into base branch.
Of course, it will not show you the conflicts, but it is a useful tool in merges.

Github - How to merge to remote feature branch

My team mate created a feature branch from our master branch. I created my own feature branch based off of team mate's feature branch. i.e.
Team mate:
git checkout master
git checkout -b teammateA-feature-branch
What I did:
git checkout teammateA-feature-branch
git pull
git checkout -b teammateB-feature-branch
I made my changes into teammateB-feature-branch and committed them.
Now, how would I create a PR such that my committed changes gets merged to team mate's remote branch i.e. teammateB-feature-branch merging into teammateA-feature-branch??
Any thoughts??
You can do this locally using Git bash:
git checkout teammateA-feature-branch
git merge teammateB-feature-branch
Then, push changes to teammateA-feature-branch:
git push origin teammateA-feature-branch:teammateA-feature-branch
OR
If these branches are available remotely (and locally), you can do the following:
Go to the pull requests tab in your repository.
Click on the Compare button.
Compare teammateA-feature-branch with teammateB-feature-branch.
Create your pull request.
Merge your changes.

git pull origin master via VSTS Rest API

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

How to merge master branch in local feature branch

In one of case I have created an branch and started to work on. I keep on commit & push changes in local branch but did not merge in master & neither pulled any changes from master.
Now I'm done with local branch changes. I followed derekgourlay tutorial & followed following steps to merge my project.
git fetch origin
git rebase −p origin/develop
First it game me number of conflict which was obvious but changes that I committed in my local branch those are not there after merge.
Am I missing anything. Any suggestion?
You can merge develop branch with your feature branch.
$ git checkout feature
$ git pull origin develop # pull (fetch + merge) develop branch into feature
$ git push origin HEAD # update remote/feature