This document provides an overview of version control using Git. It discusses the basics of Git including the three states of files, getting a Git repository, recording changes, undoing changes, working with remotes and tagging. It also covers Git branching, merging, and resolving conflicts. Remote branches are explained as well as how to push and pull from remote repositories. The key benefits of Git's lightweight branching model and fast merging are highlighted.
2024: Domino Containers - The Next Step. News from the Domino Container commu...
Git and Version Control at Atlogys
1. Version Control using Git
Anoop Kumar Malav
Atlogys Technical Consulting
anoop@atlogys.com
February 8, 2018
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 1 / 65
2. Git: Original Author
Linus Torvalds
https://en.wikipedia.org/wiki/Linus_Torvalds
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 2 / 65
3. Overview
1 About Version Control
2 Git Basics
The Three States
Getting a Git Repository
Recording Changes to the Repository
Viewing Your Staged and Unstaged Changes
Committing Your Changes
Undoing Things
Working with Remotes
Tagging
3 Git Branching
Introduction
Branch Merging
Remote Branches
4 Git Tools
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 3 / 65
4. About Version Control
Version control is a system that records changes to a file or set of files
over time so that you can recall specific versions later.
It allows you to
revert selected files back to a previous state,
revert the entire project back to a previous state,
compare changes over time,
who last modified something that might be causing a problem,
who introduced an issue and when,
and more.
Using a VCS also generally means that if you screw things up or lose
files, you can easily recover.
In addition, you get all this for very little overhead.
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 4 / 65
5. About Version Control: Local VCS
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 5 / 65
6. About Version Control: Centralized VCS
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 6 / 65
7. About Version Control: Distributed VCS
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 7 / 65
8. Git Basics: The Three States
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 8 / 65
9. Git Basics: Getting a Git Repository
You typically obtain a Git repository in one of two ways:
You can take a local directory that is currently not under version
control, and turn it into a Git repository, or
You can clone an existing Git repository from elsewhere.
In either case, you end up with a Git repository on your local machine,
ready for work.
Example (Initializing a Repository in an Existing Directory)
$ cd /home/user/my_project
$ git init
$ git add *.c; $ git add LICENSE
$ git commit -m ’initial project version’
Example (Cloning an Existing Repository)
$ git clone https://github.com/libgit2/libgit2 mylibgit
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 9 / 65
10. Git Basics: Getting a Git Repository
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 10 / 65
11. Git Basics: Getting a Git Repository
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 11 / 65
12. Git Basics: Recording Changes to the Repository
The lifecycle of the status of your files
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 12 / 65
13. Git Basics: Recording Changes to the Repository
Checking the Status of Your Files
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 13 / 65
14. Git Basics: Recording Changes to the Repository
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 14 / 65
15. Git Basics: Recording Changes to the Repository
Tracking New Files
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 15 / 65
16. Git Basics: Recording Changes to the Repository
Staging Modified Files
- Modify previously tracked file called CONTRIBUTING.md
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 16 / 65
17. Git Basics: Recording Changes to the Repository
Staging Modified Files
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 17 / 65
18. Ignoring Files
→ Class of files that you don’t want Git to automatically add or even show you as being
untracked.
→ Setting up a .gitignore file for your new repository before you get going is generally a
good idea so you don’t accidentally commit files that you really don’t want in your Git
repository.
→ In the simple case, a repository might have a single .gitignore file in its root directory,
which applies recursively to the entire repository.
→ However, it is also possible to have additional .gitignore files in subdirectories.
The rules for the patterns you can put in the .gitignore file are as follows:
Blank lines or lines starting with # are ignored.
Standard glob patterns work, and will be applied recursively throughout the entire working tree.
You can start patterns with a forward slash (/) to avoid recursivity.
You can end patterns with a forward slash (/) to specify a directory.
You can negate a pattern by starting it with an exclamation point (!).
Note: GitHub maintains a fairly comprehensive list of good .gitignore file examples for
dozens of projects and languages at https://github.com/github/gitignore
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 18 / 65
19. Viewing Your Staged and Unstaged Changes
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 19 / 65
20. Viewing Your Staged and Unstaged Changes
diff options –staged and –cached are synonyms.
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 20 / 65
21. Committing Your Changes
- The simplest way to commit is to type git commit. Doing so launches your editor of
choice.
- Alternatively, you can type your commit message inline with the commit command by
specifying it after a -m flag, like this:
$ git commit -m "Story 182: Fix benchmarks for speed"
- If you want to skip the staging area, Git provides a simple shortcut.
- Adding the -a option to the git commit command makes Git automatically stage every
file that is already tracked before doing the commit, letting you skip the git add part:
$ git commit -a -m ’added new benchmarks’
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 21 / 65
22. Removing Files
- To remove a file from Git, you have to remove it from your tracked files and then
commit.
- The git rm command does that, and also removes the file from your working directory
so you don’t see it as an untracked file the next time around.
- To keep the file in your working tree but remove it from your staging area, use the
–cached option:
$ git rm –cached README
- This is particularly useful if you forgot to add something to your .gitignore file and
accidentally staged it.
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 22 / 65
23. View Commit History
The most basic and powerful tool to do this is the git log command.
git log branchname
git log filename
git log -n
git log -p
git log –author
git log –grep=pattern
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 23 / 65
25. View Commit History
There are many graphical interface to view commit history. One of these is gitk.
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 25 / 65
26. Git Basics: Undoing Things
Commit Amend (Redo commit)
One of the common undos takes place when you commit too early and possibly forget to
add some files, or you mess up your commit message.
If you want to redo that commit, make the additional changes you forgot, stage them,
and commit again using the –amend option:
$ git commit –amend
It’s important to understand that when you’re amending your last commit, you’re not so
much fixing it as replacing it entirely with a new, improved commit that pushes the old
commit out of the way and puts the new commit in its place.
Effectively, it’s as if the previous commit never happened, and it won’t show up in your
repository history.
Do not amend commit if you have already pushed your code on remote.
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 26 / 65
27. Git Basics: Undoing Things
Unstaging a Staged File
Let’s say you’ve changed two files and want to commit them as two separate changes,
but you accidentally type git add * and stage them both.
How can you unstage one of the two? The git status command reminds you:
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 27 / 65
28. Git Basics: Undoing Things
Unmodifying a Modified File
Let’s say you don’t want to keep your changes to the CONTRIBUTING.md file.
It’s important to understand that git checkout – <file>is a dangerous command.
Any changes you made to that file are gone.
git checkout command is used in two context. The first is to revert changes as in above.
But git checkout is also used to switch to a branch.
To avoid ambiguity, it is best practice to put in the double dash to indicate that we are
not switching branch and that we want to stay on current branch.
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 28 / 65
29. Git Basics: Working with Remotes
To be able to collaborate on any Git project, you need to know how to manage your
remote repositories.
Remote repositories are versions of your project that are hosted on the Internet or
network somewhere.
Showing Your Remotes
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 29 / 65
30. Git Basics: Working with Remotes
Adding Remote Repositories
git remote add <shortname><url>
Fetching and Pulling from Your Remotes
Pushing to Your Remotes
git push <remote><branch>
Renaming and Removing Remotes
git remote rename <oldname><newname>
git remote remove <shortname>
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 30 / 65
31. Git Basics: Tagging
→ Git has the ability to tag specific points in history as being important.
→ Typically people use this functionality to mark release points.
Creating Tags
Git supports two types of tags: lightweight and annotated.
A lightweight tag is very much like a branch that doesn’t change - it’s just a pointer to a
specific commit.
git tag <tagname>
Annotated tags, however, are stored as full objects in the Git database. They contain the
tagger name, email, and date; have a tagging message.
git tag -a <tagname>-m <message>
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 31 / 65
32. Git Basics: Tagging
Listing Your Tags
git tag
Sharing Tags
git push origin <tagname>
If you have a lot of tags that you want to push up at once, you can also use the –tags option to
the git push command.
git push origin –tags
Checking out Tags
If you want to view the versions of files a tag is pointing to, you can do a git checkout, though
this puts your repository in “detached HEAD“ state.
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 32 / 65
33. Git Branching: Killer Feature
The way Git branches
is incredibly lightweight,
making branching operations nearly instantaneous, and
switching back and forth between branches generally just as fast.
→ A branch in Git is simply a lightweight movable pointer to one of the
commits.
→ The default branch name in Git is master.
→ As you start making commits, you’re given a master branch that points
to the last commit you made.
→ Every time you commit, it moves forward automatically.
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 33 / 65
34. Git Branching: Killer Feature
How does Git know what branch you’re currently on? It keeps a special
pointer called HEAD.
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 34 / 65
39. Branch Merging: Merge Commit
When merging a Branch, make sure your working directory is clean. There
is a chance to loosing your uncommited work.
In case of merge conflict people make mistake to commit changes which
are not present in either of the branch.
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 39 / 65
42. Branch Merging: Rebase then Fast Forward
git rebase <onbranch>
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 42 / 65
43. Branch Merging: Rebase then Fast Forward
git checkout master; git merge experiment
git checkout experiment; git rebase master
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 43 / 65
44. Branch Merging: Rebase then Fast Forward
git checkout experiment; git rebase master
git checkout master; git merge experiment
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 44 / 65
45. Branch Merging: Rebase vs. Merge
The Perils of Rebasing
Ahh, but the bliss of rebasing isn’t without its drawbacks, which can be summed up in a single
line:
Do not rebase commits that exist outside your repository.
If you follow that guideline, you’ll be fine.
In general the way to get the best of both worlds is to rebase local changes you’ve made but
haven’t shared yet before you push them in order to clean up your story, but never rebase
anything you’ve pushed somewhere.
Rule of thumb:
When pulling changes from origin/develop onto your local develop use rebase. (git pull
–rebase origin develop)
When finishing a feature branch merge the changes back to develop. (git merge –no-ff
feature)
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 45 / 65
51. Staging Patches
It’s also possible for Git to stage certain parts of files and not the rest. For example, if
you make two changes to your simplegit.rb file and want to stage one of them and not
the other, doing so is very easy in Git.
git add -p -A
You have a lot of options at this point. Typing ? shows a list of what you can do:
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 51 / 65
52. Staging Patches
Generally, you’ll type y or n if you want to stage each hunk, but staging all of them in
certain files or skipping a hunk decision until later can be helpful too.
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 52 / 65
53. Git Stashing and Cleaning
Often, when you’ve been working on part of your project, things are in a messy state
and you want to switch branches for a bit to work on something else.
The problem is, you don’t want to do a commit of half-done work just so you can get
back to this point later.
git stash
Apply Stash
git stash apply
Apply and Drop Stash
git stash pop
Cleaning your Working Directory
Dry Run
git clean -d -X -n
Cleanup
git clean -d -X -f
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 53 / 65
59. Git Revert
$ git revert 8d78c0f
Given one or more existing commits, reverts the changes that the related patches
introduce, and record some new commits that record them.
This requires your working tree to be clean (no modifications from the HEAD commit).
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 59 / 65
63. Debugging with Git: File Annotation
If you track down a bug in your code and want to know when it was introduced and why, file
annotation is often your best tool.
It shows you what commit was the last to modify each line of any file.
The following example uses git blame to determine which commit and committer was
responsible for lines in the top-level Linux kernel Makefile and, further, uses the -L option to
restrict the output of the annotation to lines 69 through 82 of that file:
Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 63 / 65