When you are working with multiple branches in Git, it’s important to be able to compare them and contrast the differences.
$ git checkout -b feature Switched to a new branch 'feature' You created some commits in your branch, you want to set the tracking branch to be master. $ git branch -u origin/master Branch 'feature' set up to track remote branch 'master' from 'origin'. You successfully set the upstream branch for your existing local branch. Git: Merge Branch into Master By Jacob Stopak. 16 Comments One of Git's most powerful features is the ability to easily create and merge branches. Git's distributed nature encourages users to create new branches often and to merge them regularly as a part of the development process. Wrapped in a single session, you'll find the concepts and techniques that convert the average Git practitioner into a master of the craft. We'll go from tech.
In this short note i will show how to compare two branches in Git using the git diff
command.
I will show how to git diff
between any two branches, e.g. current branch and master or git diff
between master and staging and how to list only files that are different between two branches (without changes themselves).
Cool Tip: How to git diff
staged and unstaged files! Read more →
Diff between current branch and master:
Diff between two branches, e.g. master and staging:
Show only files that are different between the two branches (without changes themselves):
-->Distributed version control systems like Git give you flexibility in how you use version control to share and manage code.Your team should find a balance between this flexibility and the need to collaborate and share code in a consistent manner.
Team members publish, share, review, and iterate on code changes through Git branches shared with others.Adopt a branching strategy for your team. You can collaborate better and spend less time managing version control and more time developing code.
The following branching strategies are based on the way we use Git here at Microsoft. For more information, see How we use Git at Microsoft.
Keep your branch strategy simple. Build your strategy from these three concepts:
A strategy that extends these concepts and avoids contradictions will result in a version control workflow for your team that is consistent and easy to follow.
Develop your features and fix bugs in feature branches based off your main branch. These branches are also known as topic branches.Feature branches isolate work in progress from the completed work in the main branch.Git branches are inexpensive to create and maintain. Even small fixes and changes should have their own feature branch.
Creating feature branches for all your changes makes reviewing history simple. Look at the commits made in the branch and look at the pull request that merged the branch.
Use a consistent naming convention for your feature branches to identify the work done in the branch.You can also include other information in the branch name, such as who created the branch.
Some suggestions for naming your feature branches:
Note
For information on setting policies to enforce a branch naming strategy, see Require branch folders.
Learn more about using feature flags in your code.
The review that takes place in a pull request is critical for improving code quality.Only merge branches through pull requests that pass your review process.Avoid merging branches to the main branch without a pull request.
Reviews in pull requests take time to complete. Your team should agree on what's expected from pull request creators and reviewers.Distribute reviewer responsibilities to share ideas across your team and spread out knowledge of your codebase.
Some suggestions for successful pull requests:
The code in your main branch should pass tests, build cleanly, and always be current.Your main branch needs these qualities so that feature branches created by your team start from a known good version of code.
Set up a branch policy for your main branch that:
Tip
The build pipeline for your pull requests should be quick to complete, so it doesn't interfere with the review process.
Use release branches to coordinate and stabilize changes in a release of your code.This branch is long-lived and isn't merged back into the main branch in a pull request, unlike the feature branches.Create as many release branches as you need. Keep in mind that each active release branch represents another version of the code you need to support.Lock release branches when you're ready to stop supporting a particular release.
Create a release branch from the main branch when you get close to your release or other milestone, such as the end of a sprint.Give this branch a clear name associating it with the release, for example release/20.
Create branches to fix bugs from the release branch and merge them back into the release branch in a pull request.
Make sure that fixes land in both your release branch and your main branch.One approach is to make fixes in the release branch, then bring changes into your main branch to prevent regression in your code.Another approach (and the one employed by the Azure DevOps team) is to always make changes in the mainline, then port those to the release branch.You can read more about our Release Flow strategy.
In this topic, we'll cover making changes in the release branch and porting them into mainline.Use cherry-picking instead of merging so that you have exact control over which commits are ported back to the main branch.Merging the feature branch into the main branch can bring over release-specific changes you don't want in the main branch.
Update the main branch with a change made in the release branch with these steps:
This release branch workflow keeps the pillars of the basic workflow intact: feature branches, pull requests, and a strong main branch that always has the latest version of the code.
Other branching workflows use Git tags to mark a specific commit as a release.Tags are useful for marking points in your history as important. Tags introduce extra steps in your workflow that aren't necessary if you're using branches for your releases.
Tags are maintained and pushed separately from your commits.Team members can easily miss tagging a commit and then have to go back through the history afterwards to fix the tag.You can also forget the extra step to push the tag, leaving the next developer working from an older version of the code when supporting the release.
The release branch strategy extends the basic feature branch workflow to handle releases.Your team doesn't have to adopt any new version control process other than the cherry-pick to port changes.
You can handle multiple deployments of your code in the same way you handle multiple releases.Create a clear naming convention, such as deploy/performance-test, and treat the environment branches like release branches.Your team should agree on a process to update deployment branches with the code from your main branch.Cherry-pick bug fixes in the deployment branch back to the main branch. Use the same steps as porting changes from a release branch.
An exception to this recommendation is if you're using a form of continuous deployment.Use Azure Pipelines when working with continuous deployment to promote builds from your main branch to your deployment targets.