Git branching allows developers to work independently in parallel on different parts of the same project. Branches are pointers to commits and don't change the repository when created. Git checkout switches between branches, commits, and files. Git revert undoes changes by creating a new commit, while reset removes commits. Common workflows include centralize, feature branch, and Gitflow models, with the latter having main branches of master and develop plus supporting feature, release, and hotfix branches.
3. It's important to understand that
branches are just pointers to
commits.
When you create a branch, all Git
needs to do is create a new pointer
—it doesn’t change the repository
in any other way.
6. Git Revert
Generate a new commit that undoes all of the
changes introduced in <commit>, then apply it to
the current branch.
Git Revert does not “revert” back to the previous
state of a project
8. Compare
Commit File Branch
Git Checkout
Makes the entire
working directory
match that
commit
Unmodify a
modified file
Change to
another branch
Git Revert
Undo a
committed
snapshot
- -
Git Reset
Remove all
subsequent
commits
Unstage a staged
file
Remove all
subsequent
commits
10. Gitflow Workflow Model
The Gitflow Workflow still uses a central repository as the
communication hub for all developers.
As in the other workflows, developers work locally and push
branches to the central repo. The only difference is the
branch structure of the project.
2 Types of Branches: Main branches & Supporting branches
11. The Main Branches
Master - the main branch where the source code of HEAD
always reflects a production-ready state.
Develop - the main branch where the source code of HEAD
always reflects a state with the latest delivered
development changes for the next release.
Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.
The Git feature that really makes it stand apart from nearly every other SCM out there is its branching model.
Git allows and encourages you to have multiple local branches that can be entirely independent of each other. The creation, merging, and deletion of those lines of development takes seconds.
Checking out a commit makes the entire working directory match that commit.
If you’re only interested in a single file, you can also use git checkout to fetch an old version of it.
The git revert command undoes a committed snapshot. But, instead of removing the commit from the project history, it figures out how to undo the changes introduced by the commit and appends anew commit with the resulting content. This prevents Git from losing history, which is important for the integrity of your revision history and for reliable collaboration.
Reverting should be used when you want to remove an entire commit from your project history. This can be useful, for example, if you’re tracking down a bug and find that it was introduced by a single commit. Instead of manually going in, fixing it, and committing a new snapshot, you can use git revert to automatically do all of this for you.
Reverting vs. Resetting
It's important to understand that git revert undoes a single commit—it does not “revert” back to the previous state of a project by removing all subsequent commits. In Git, this is actually called a reset, not a revert.
The Gitflow Workflow still uses a central repository as the communication hub for all developers. And, as in the other workflows, developers work locally and push branches to the central repo. The only difference is the branch structure of the project.
We consider origin/master to be the main branch where the source code of HEAD always reflects a production-ready state.
We consider origin/develop to be the main branch where the source code of HEAD always reflects a state with the latest delivered development changes for the next release.
Some would call this the “integration branch”. This is where any automatic nightly builds are built from.
When the source code in the develop branch reaches a stable point and is ready to be released, all of the changes should be merged back into master somehow and then tagged with a release number. How this is done in detail will be discussed further on.
Therefore, each time when changes are merged back into master, this is a new production release by definition. We tend to be very strict at this, so that theoretically, we could use a Git hook script to automatically build and roll-out our software to our production servers everytime there was a commit on master.
Some would call this the “integration branch”. This is where any automatic nightly builds are built from.
When the source code in the develop branch reaches a stable point and is ready to be released, all of the changes should be merged back into master somehow and then tagged with a release number. How this is done in detail will be discussed further on.
Therefore, each time when changes are merged back into master, this is a new production release by definition. We tend to be very strict at this, so that theoretically, we could use a Git hook script to automatically build and roll-out our software to our production servers everytime there was a commit on master.
Next to the main branches master and develop, our development model uses a variety of supporting branches to aid parallel development between team members, ease tracking of features, prepare for production releases and to assist in quickly fixing live production problems. Unlike the main branches, these branches always have a limited life time, since they will be removed eventually.
Each of these branches have a specific purpose and are bound to strict rules as to which branches may be their originating branch and which branches must be their merge targets.
By no means are these branches “special” from a technical perspective. The branch types are categorized by how we use them. They are of course plain old Git branches.
Feature branches (or sometimes called topic branches) are used to develop new features for the upcoming or a distant future release. Feature branches typically exist in developer repos only, not in origin.
Release branches support preparation of a new production release.
When the state of the release branch is ready to become a real release, some actions need to be carried out.
First, the release branch is merged into master (since every commit on master is a new release by definition, remember).
Next, that commit on master must be tagged for easy future reference to this historical version.
Finally, the changes made on the release branch need to be merged back into develop, so that future releases also contain these bug fixes.
Hotfix branches are very much like release branches in that they are also meant to prepare for a new production release, albeit unplanned.
When a critical bug in a production version must be resolved immediately, a hotfix branch may be branched off from the corresponding tag on the master branch that marks the production version.The essence is that work of team members (on the develop branch) can continue, while another person is preparing a quick production fix.