Git - Revision Control Perfected
Branching and Merging
The work you do in Git is specific to the current branch. A branch is simply a moving reference to a commit (SHA1 object name). Every time you create a new commit, the reference is updated to point to it—this is how Git knows where to find the most recent commit, which is also known as the tip, or head, of the branch.
By default, there is only one branch ("master"), but you can have as many
as you want. You create branches with
git branch and switch between
git checkout. This may seem odd at first, but the reason
it's called "checkout" is that you are "checking
out" the head of that
branch into your working copy. This alters the files in your working
copy to match the commit at the head of the branch.
Branches are super-fast and easy, and they're a great way to try out new ideas, even for trivial things. If you are used to other systems like CVS/SVN, you might have negative thoughts associated with branches—forget all that. Branching and merging are free in Git and can be used without a second thought.
Run the following commands to create and switch to a new local branch named "myidea":
git branch myidea git checkout myidea
All commits now will be tracked in the new branch until you switch to
another. You can work on more than one branch at a time by switching
back and forth between them with
Branches are really useful only because they can be merged back together later. If you decide that you like the changes in myidea, you can merge them back into master:
git checkout master git merge myidea
Unless there are conflicts, this operation will merge all the changes from myidea into your working copy and automatically commit the result to master in one fell swoop. The new commit will have the previous commits from both myidea and master listed as parents.
However, if there are conflicts—places where the same part of a file
was changed differently in each branch—Git will warn you and update
the affected files with "conflict markers" and not commit
the merge automatically. When this happens, it's up to you to edit the files by hand,
make decisions between the versions from each branch, and then remove the
conflict markers. To complete the merge, use
git add on each formerly
conflicted file, and then
After you merge from a branch, you don't need it anymore and can delete it:
git branch -d myidea
If you decide you want to throw myidea away without merging it, use
-D instead of a lowercase
-d as listed above. As a safety
feature, the lowercase switch won't let you delete a branch that hasn't
To list all local branches, simply run:
Git provides a number of tools to examine the history and differences
between commits and branches. Use
git log to view commit histories and
git diff to view the differences between specific commits.
These are text-based tools, but graphical tools also are available, such as
the gitk repository browser, which essentially is a GUI version of
git log --graph to visualize branch history. See Figure 2 for a screenshot.
Figure 2. gitk
Git can merge from a branch in a remote repository simply by transferring needed objects and then running a local merge. Thanks to the content-addressed storage design, Git knows which objects to transfer based on which object names in the new commit are missing from the local repository.
git pull command performs both the transfer step
(the "fetch") and
the merge step together. It accepts the URL of the remote repository (the
"Git URL") and a branch name (or a full "refspec") as arguments. The
Git URL can be a local filesystem path, or an SSH, HTTP, rsync or
Git-specific URL. For instance, this would perform a pull using SSH:
git pull user@host:/some/repo/path master
Git provides some useful mechanisms for setting up relationships with remote repositories and their branches so you don't have to type them out each time. A saved URL of a remote repository is called a "remote", which can be configured along with "tracking branches" to map the remote branches into the local repository.
Fast/Flexible Linux OS Recovery
On Demand Now
In this live one-hour webinar, learn how to enhance your existing backup strategies for complete disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible full-system recovery solution for UNIX and Linux systems.
Join Linux Journal's Shawn Powers and David Huffman, President/CEO, Storix, Inc.
Free to Linux Journal readers.Register Now!
- Google's Abacus Project: It's All about Trust
- Download "Linux Management with Red Hat Satellite: Measuring Business Impact and ROI"
- Seeing Red and Getting Sleep
- Back to Backups
- Secure Desktops with Qubes: Introduction
- Fancy Tricks for Changing Numeric Base
- Working with Command Arguments
- Secure Desktops with Qubes: Installation
- Linux Mint 18
- CentOS 6.8 Released
Until recently, IBM’s Power Platform was looked upon as being the system that hosted IBM’s flavor of UNIX and proprietary operating system called IBM i. These servers often are found in medium-size businesses running ERP, CRM and financials for on-premise customers. By enabling the Power platform to run the Linux OS, IBM now has positioned Power to be the platform of choice for those already running Linux that are facing scalability issues, especially customers looking at analytics, big data or cloud computing.
￼Running Linux on IBM’s Power hardware offers some obvious benefits, including improved processing speed and memory bandwidth, inherent security, and simpler deployment and management. But if you look beyond the impressive architecture, you’ll also find an open ecosystem that has given rise to a strong, innovative community, as well as an inventory of system and network management applications that really help leverage the benefits offered by running Linux on Power.Get the Guide