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
Direct Download
Read full article as PDF:
Price $2.95
News
-
OpenMandriva Lx ROME Technical Preview Released
OpenMandriva’s rolling release distribution technical preview has been released for testing purposes and adds some of the latest/greatest software into the mix.
-
Linux Mint 21 is Now Available
The latest iteration of Linux Mint, codenamed Vanessa, has been released with a new upgrade tool and other fantastic features.
-
Firefox Adds Long-Anticipated Feature
Firefox 103 has arrived and it now includes a feature users have long awaited…sort of.
-
System76 Refreshes Their Popular Oryx Pro Laptop with a New CPU
The System76 Oryx Pro laptop has been relaunched with a 12th Gen CPU and more powerful graphics options.
-
Elive Has Released a New Beta
The Elive team is proud to announce the latest beta version (3.8.30) of its Enlightenment-centric Linux distribution.
-
Rocky Linux 9 Has Arrived
The latest iteration of Rocky Linux is now available and includes a host of new features and support for new architecture.
-
Slimbook Executive Linux Ultrabook Upgrading Their CPUs
The Spanish-based company, Slimbook, has made available their next generation Slimbook Executive Linux ultrabooks with a 12th Gen Intel Alder Lake CPU.
-
Fedora Linux is Coming to the Raspberry Pi 4
Thanks to significant work in the upstream, the upcoming release of Fedora 37 will introduce support for the Raspberry Pi 4.
-
New Linux Ultrabook from TUXEDO Computers
TUXEDO Computers has released a new 15" Ultrabook running Linux.
-
GNOME 43 To Bring Some Exciting New Features
GNOME 43 is getting close to the first alpha development release and it promises to add one particular feature that should be exciting to several users.