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 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.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.
-
ZorinOS 17.1 Released, Includes Improved Windows App Support
If you need or desire to run Windows applications on Linux, there's one distribution intent on making that easier for you and its new release further improves that feature.
-
Linux Market Share Surpasses 4% for the First Time
Look out Windows and macOS, Linux is on the rise and has even topped ChromeOS to become the fourth most widely used OS around the globe.
-
KDE’s Plasma 6 Officially Available
KDE’s Plasma 6.0 "Megarelease" has happened, and it's brimming with new features, polish, and performance.
-
Latest Version of Tails Unleashed
Tails 6.0 is based on Debian 12 and includes GNOME 43.
-
KDE Announces New Slimbook V with Plenty of Power and KDE’s Plasma 6
If you're a fan of KDE Plasma, you'll be thrilled to hear they've announced a new Slimbook with an AMD CPU and the latest version of KDE Plasma desktop.
-
Monthly Sponsorship Includes Early Access to elementary OS 8
If you want to get a glimpse of what's in the pipeline for elementary OS 8, just set up a monthly sponsorship to help fund its continued existence.