The Basics of Version Control
Working with Branches
An important version control concept is branches, which are different versions of the same sets of files. Branches may be organized according to the stage of development – for instance, alpha, beta, and release – or by contributors or any other useful criteria. Especially in large projects with lots of files and users, it is more useful to work with branches as collections of files rather than with individual files, because changes to one file often affect others, and you want to be sure that your changes affect only those you intend to change.
To view a list of branches in numerical and alphabetical order, enter git branch
(Figure 6).The output will include a branch called master
, which is the main branch created with the repository, with an asterisk beside the branch that is currently active. To create a new branch, enter:
git branch BRANCH-NAME
To switch to another branch, enter
git checkout BRANCH-NAME
All your changes to files will affect only the versions of the files on the current branch. Each branch can have different versions of files with the same name. You can merge the file versions of the branch you are currently in with the file versions in another branch with:
git merge BRANCH-NAME
If there are any conflicts between the two branches, you can use git diff
to view and reconcile them. When all conflicts are resolved, use
git commit -a
to commit the changes (Figure 7). The command gitk
will show a graphical depiction of the merge. Any branches no longer needed can be removed with:
git branch -d BRANCH-NAME
If you are working with branches of repositories that are remote from each other, use
git pull URL
to copy files from one branch to another. If conflicts result, branches are not merged until problems are resolved locally. The keeper of one repository can view useful information from the other one with:
git fetch URL master git log -p HEAD..FETCH_HEAD
FETCH_HEAD
refers to the remote head.
Using Log Information
Resolving conflicts between branches is easier with information from Git's log. The command git log
shows a complete history of changes, while the addition of the -p
option displays diffs, and the combo of –stat
and --summary
gives a convenient overview (Figure 8).
The log in Figure 8 identifies a commit with a 40 character name consisting of letters and numbers. Mercifully, you do not need to use the entire name, but can often use only enough of the name to identify the commit as unique or use git tag
to give the commit a human-friendly name. In any of these ways, you can then use
git show COMMIT
to zero in on its details. In addition, git grep
offers a strong search command, while
git log INCLUDED-BRANCH..EXCLUDED-BRANCH
restricts the branches to include in a search.
Next Steps
This overview should be enough to get you started with Git. However, there are countless additional details. Once you are comfortable with the basics, start looking up the options for Git's subcommands and gradually expand your range of choices.
Take advantage, too, of Git's exhaustive man pages. Perhaps because version control is essential to development, Git is one of the most documented commands in all of Linux. Besides the man page for the command itself, the two-part gittutorial(7) is a lengthy lesson in itself. It is supported by gitcvs-migration(7), gitcore-tutorial(7), gitglossary(7), git-help(1), gitworkflows(7), giteveryday(7), and The Git User's Manual[1] – if that is not enough, each of dozens of subcommands also has its separate man page.
One potentially puzzling aspect of this documentation is that it often classifies commands into porcelain (high-level commands about managing repositories like git branch
or git checkout
) and plumbing (low-level commands about working with files and branches like git apply
and git merge FILES
). This distinction does not affect Git's various tasks, but it can puzzle newcomers.
Aside from the porcelain/plumbing distinction, Git is complex, but its complexity is due to its size rather than structure. Once you understand the different types of tasks it is designed for, Git is not that hard to learn. And when you grasp it, you will understand one of the vital tools – if not rituals – of open source development.
Infos
- Git: https://git-scm.com/
- Git history: https://www.pcworld.idg.com.au/article/129776/after_controversy_torvalds_begins_work_git_/
- Naming of Git: https://www.youtube.com/watch?v=4XpnKHJAok8
- Configuring a repository: https://git-scm.com/docs/git-config
- Git add: https://git-scm.com/docs/git-add
« Previous 1 2
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Canonical Releases Ubuntu 24.04
After a brief pause because of the XZ vulnerability, Ubuntu 24.04 is now available for install.
-
Linux Servers Targeted by Akira Ransomware
A group of bad actors who have already extorted $42 million have their sights set on the Linux platform.
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.
-
XZ Gets the All-Clear
The back door xz vulnerability has been officially reverted for Fedora 40 and versions 38 and 39 were never affected.
-
Canonical Collaborates with Qualcomm on New Venture
This new joint effort is geared toward bringing Ubuntu and Ubuntu Core to Qualcomm-powered devices.
-
Kodi 21.0 Open-Source Entertainment Hub Released
After a year of development, the award-winning Kodi cross-platform, media center software is now available with many new additions and improvements.
-
Linux Usage Increases in Two Key Areas
If market share is your thing, you'll be happy to know that Linux is on the rise in two areas that, if they keep climbing, could have serious meaning for Linux's future.
-
Vulnerability Discovered in xz Libraries
An urgent alert for Fedora 40 has been posted and users should pay attention.
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.