This tutorial will help you get started with Git.
Audience of this tutorial are beginners so don't expect advanced concepts.
Before we start make sure git --version
command successfully returns. If the output is something like command not found then please make sure Git is properly installed on your machine. My machine output is shown below. Output on your machine might be different.
$ git --version
git version 2.5.4 (Apple Git-61)
Commands that a user should type on his/her terminal are prefixed with the shell prompt symbol
$
. The output of command follows the command. Also, you don't have to type$
on your terminal.
Make sure you have configured your user with Git. This information will be used by Git.
$ git config --global user.name "Your name"
$ git config --global user.email "Your email"
The command shown above populate a file named .gitconfig
in your user home directory with configuration information.
$ cat ~/.gitconfig
[user]
name = Anshul Sonpure
email = anshul.sonpure@outlook.com
[push]
default = simple
- Why version control system?
- What the heck is Git?
- Git basics
- git init
- git status
- git add
- git commit
- git rm
- git log
- .gitignore files
- git diff
- git diff commits
- Working with branches
- git branch
- git checkout
- git merge
- Working with remote repositories
- git remote
- git pull
- git push
- git clone
- Github basics
- Why and What Pull Requests?
- Advanced topics
- git rebase
- git cherry-pick
- git stash
- git alias
- Using Git from within your IDE(Eclipse)
- Using a Git GUI tool
- Useful Git commands
A version control system is a kind of database for storing your software project source code. It lets you save a snapshot of your complete project at any time you want. When you later take a look at an older snapshot (let's start calling it "version").
You can use version control system to store:
- Software source code
- Text files
- Writing books
- Sharing datasets
You should use version control because:
- Collaboration
- Storing Versions
- Restoring Previous Versions
- Understanding What Happened
- Backup
Git is a distributed version control system. It was developed by Linus Torvalds in 2005 for linux developers.
Every Git working directory is a full-fledged repository with complete history and full version-tracking capabilities, independent of network access or a central server.
Distributed or Decentralized VCS allows many software developers to work on a project without requiring them to share a common network.
Let's start by creating an empty directory git-playground
. Navigate to a convenient location on your local file system and create a new directory.
$ mkdir git-playground && cd git-playground
To make any repository a Git managed repository, you will type the following command. These commands should be typed from inside the git-playground
directory.
$ git init
Initialized empty Git repository in ~/git-playground/.git/
This will create a directory named .git
inside the git-playground
directory.
The .git
directory structure would look like as shown below.
To use
tree
command, you have to install it on your machine. Thetree
command is not required for this tutorial. If you are on mac, then you can use package manager like brewbrew install tree
.
$ tree -a .git
.git
|-- HEAD
|-- config
|-- hooks
| `-- post-commit
|-- objects
| |-- info
| `-- pack
`-- refs
|-- heads
`-- tags
7 directories, 3 files
.git
is a directory where Git stores all the data. Don't mess up with .git
directory. The only file that you should change is config
.
$ cat .git/config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
If you want to override user for this repository then you can add user section by either editing .git/config
file or using the git config
command.
$ git config --local user.name "anshul-sonpure"
If you view contents of the .git/config
file now, then you will see your change.
$ cat .git/config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
[user]
name = anshul-sonpure
Please revert this change else your commits will be committed using this user name.
Time and again we will need to know status of our Git repository. By status it means, what needs to be added to the index, what needs to be committed, is there any change that we have to commit, etc. This is the command that you will use most.
$ git status
On branch master
Initial commit
nothing to commit (create/copy files and use "git add" to track)
Let's create a new file README.md
and add a simple stupid message.
$ echo "# Git Playground" >> README.md
It is a good practice to create a file named README.md in your repository root that tells purpose of the repository.
Now, check the status of the Git repository.
$ git status
On branch master
Initial commit
Untracked files:
(use "git add <file>..." to include in what will be committed)
README.md
nothing added to commit but untracked files present (use "git add" to track)
git add stage the changes so they are picked by the next commit. In Git, you can't commit something until you make it trackable.
$ git add README.md
Now, check the status of your Git repository.
$ git status
On branch master
Initial commit
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: README.md
If you have multiple files then you can use
git add <file1> <file2> .. <filen>
orgit add --all
to stage them in one command.
Once you have a smallest working change, you should commit it to your version control system. Smallest working change could be a test case, a small piece of functional code, a line or paragraph in your text file, etc. You should commit often and daily.
After staging your changes, next step is to commit them into your local Git repository. To do that, we will use commit command as shown below.
$ git commit -m "first commit"
[master (root-commit) 26afc7f] first commit
1 file changed, 1 insertion(+)
create mode 100644 README.md
Staging and committing are two different steps in Git. You can't commit a change until it has been staged. If you are working with a tracked file, then you can do both of these steps in a single command.
$ echo "This is my Git playground." >> README.md
Check the status of your repository. This time it will say it is a modified change.
$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: README.md
no changes added to commit (use "git add" and/or "git commit -a")
To stage and commit a change in a tracked file, you can use following command.
$ git commit -am "second commit"
Let's suppose we added a bad file to the Git index that we want to remove.
$ echo "my stupid file" >> stupid.txt
$ git add stupid.txt
To remove a bad file from the Git index, we can use Git rm
command.
$ git rm -f stupid.txt
rm 'stupid.txt'
To view history of your commit logs, use the following command.
$ git log
commit abae2503d91f6ac2907bd0193ba323e10e73077c
Author: Shekhar Gulati <shekhargulati84@gmail.com>
Date: Thu Jan 14 05:11:19 2016 +0530
second commit
commit bb2b56961e5f2a52e74af316fdb074592b0dbf16
Author: Shekhar Gulati <shekhargulati84@gmail.com>
Date: Thu Jan 14 05:11:06 2016 +0530
first commit
If you want to see a commit in one line, then use oneline
option.
$ git log --oneline
To make it look a bit better
$ git log --oneline --decorate
Searching through logs
$ git log --oneline --grep "first"
Searching by author
$ git log --oneline --decorate --author Shekhar
You can format the way you want by using a formatter. Learn more about formatter from here https://git-scm.com/docs/pretty-formats
$ git log --pretty=format:"%h %aN %ar" --date=short
There are many more options. You can refer to help git help log
for more details.
Every Git repository should have another configuration file .gitignore
present in the root i.e. directly inside the git-playground
directory. This file is used to specify files and file patterns that you want to ignore.
$ echo "*.log" >> .gitignore
$ echo "target/" >> .gitignore
$ echo "dummy.txt" >> .gitignore
- The first pattern says any file with extension
log
will ignored. - The second pattern says directory with name
target
will be ignored. - The third pattern means file with name
dummy.txt
will be ignored.
Now add and commit the .gitignore
to your Git repository.
$ git add .gitignore
$ git commit -m "third commit. Adding .gitignore file"
To view unstaged changes since last commit.
$ echo "I will learn Git today." >> README.md
$ git diff
diff --git a/README.md b/README.md
index 6b8025a..fcb7db5 100644
--- a/README.md
+++ b/README.md
@@ -1,2 +1,3 @@
# Git Playground
This is my Git playground.
+I will learn Git today.
Let's commit the change.
$ git commit -am "fourth commit"
Now, if you execute git diff
command, you will see nothing.
To compare head with the previous commit you can use the following command.
$ git diff HEAD~1..HEAD
You can also use commit ids.
$ git diff <commit_id_1> <commit_id_2>
git clean
command helps us to remove untracked files and directories from the working tree.
$ touch abc.txt
$ mkdir tmp
$ touch tmp/def.txt
Forcefully remove untracked files
$ git clean -f
Forcefully remove directory
git clean -d
Forcefully remove untracked file and directory
$ git clean -df
To also remove files in the .gitignore
file, you can specify -x
option.
$ echo "logging" >> abc.log
git clean
will not do anything
$ git clean -f
$ → ll
total 16
-rw-r--r-- 1 shekhargulati staff 83B Jan 14 05:59 README.md
-rw-r--r-- 1 shekhargulati staff 8B Jan 14 06:30 abc.log
You can clean up .gitignore
files using the -x
option.
$ git clean -f -x
Removing abc.log
A branch represents an independent line of development. You use branch for following:
- Develop a new feature. Once you are done with the feature it will be merged into your main branch.
- Fix a bug.
- Your experiment playground.
By default, every git repository has one branch called master. When you create a new branch, you get a new development workspace. Any change that you make to the new working directory has no impact on your previous working directory.
git branch command lets you work with Git branches.
To view all the branches, you execute following command.
$ git branch
* master
branch marked with a * is the current branch.
Let's suppose we have to implement some new functionality. To work on new functionality, we create a new branch called feature1
.
$ git branch feature1
You can view branch list again.
$ git branch
feature1
* master
One thing that is important to understand here is Git branches are just pointers to commits. When you create a branch, all Git needs to do is create a new pointer—it doesn’t change the repository in any other way.
$ git log --oneline --decorate
79a81e4 (HEAD -> master, feature1) fourth commit
c9a8f2d third commit. Added .gitignore file
abae250 second commit
bb2b569 first commit
To switch to a branch, you use checkout command.
$ git checkout feature1
The git checkout
command lets you navigate between the branches created by git branch.
To create a new branch and checkout it in one command
$ git checkout -b feature1
Let's add our new feature now.
$ echo "I will read a Git tutorial today to make sure I understand Git thoroughly." >> README.md
Commit it to the feature1
branch.
$ git commit -am "fifth commit. Read tutorial"
Once we are done with our feature, we can merge it back to master.
$ git checkout master
git merge command allows you to merge an independent development line created by git branch
into a single branch.
git merge will merge into the current branch.
To merge feature1
branch into master
branch, execute the following command.
$ git merge feature1
$ git log --oneline --decorate
5ce434c (HEAD -> master, feature1) fifth commit. Read tutorial
79a81e4 fourth commit
c9a8f2d third commit. Added .gitignore file
abae250 second commit
bb2b569 first commit
the default merge algorithm is ff i.e fast forward. When the merge resolves as a fast-forward, only update the branch pointer, without creating a merge commit. This is the default behavior.
A fast-forward merge can occur when there is a linear path from the current branch tip to the target branch.
Now that we are done with feature1
branch, let's delete it.
$ git branch -d feature1
Create another new branch feature2
and add a new commit.
$ git checkout -b feature2
$ echo "I will watch a Git video by Linus Torvalds https://www.youtube.com/watch?v=4XpnKHJAok8" >> README.md
$ git commit -am "sixth commit. Watch video."
Now checkout master again.
$ git checkout master
The --no-ff
algorithm always generate a merge commit (even if it was a fast-forward merge)
$ git merge --no-ff feature2
View logs of master
$ git log --oneline --decorate
0039442 (HEAD -> master) Merge branch 'feature2'
904ab35 (feature2) sixth commit. Watch video.
5ce434c fifth commit. Read tutorial
79a81e4 fourth commit
c9a8f2d third commit. Added .gitignore file
abae250 second commit
bb2b569 first commit
The interesting bit is that it created a merge commit. Git uses Three-way merge algorithm.
- the
MERGE_HEAD
commit i.e. the modification that we want to merge - the
HEAD
commit i.e. the branch in which theMERGE_HEAD
will be merged i.e. the branch on which the git merge command is called - the
ORIG_HEAD
commit i.e. the best common ancestor ofMERGE_HEAD
andHEAD
that will serve as the reference.
Delete the feature branch feature2
$ git branch -d feature2
Create another new branch feature2
and add a new commit.
$ git checkout -b feature3
$ echo "I will try Git using tutorial https://try.github.io/" >> README.md
$ git commit -am "seventh commit. Try Git."
Checkout master branch and edit an existing line.
$ git checkout master
Change I will read a Git tutorial today to make sure I understand Git thoroughly. to I will read a Git tutorial https://www.atlassian.com/git/tutorials/.
$ cat README.md
# Git Playground
This is my Git playground.
I will learn Git today during the XKE.
I will read a Git tutorial https://www.atlassian.com/git/tutorials/.
I will watch a Git video by Linus Torvalds https://www.youtube.com/watch?v=4XpnKHJAok8
Now, commit the change to master branch.
$ git commit -am "fixed Git tutorial bullet point"
Now merge the feature3
branch. This will create a merge commit.
$ git merge feature3
View the log graph.
$ git log --oneline --graph
* b182cc7 Merge branch 'feature3'
|\
| * 6f04457 seventh commit. Try Git.
* | fd759a7 fixed Git tutorial bullet point
|/
* 0039442 Merge branch 'feature2'
|\
| * 904ab35 sixth commit. Watch video.
|/
* 5ce434c fifth commit. Read tutorial
* 79a81e4 fourth commit
* c9a8f2d third commit. Added .gitignore file
* abae250 second commit
* bb2b569 first commit
In this section, we will look at scenario where there is a merge conflict. Merge conflict happens when you‘re trying to merge a branch that has changed the same part of the same file as master. Let's create a new branch feature4
, make a change, and commit.
$ git checkout -b feature4
$ echo "I will create a Github account today." >> README.md
$ git commit -am "eighth commit. Signup for Github."
Now, checkout master, make a change, and then commit it.
$ git checkout master
$ echo "I will create my first repository today." >> README.md
$ git commit -am "eighth commit. Create repository on Github."
If you try to merge the feature4
branch, you will get merge conflict as shown below.
$ git merge feature4
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.
To merge the conflict, open the file in your favorite editor. I like Atom.
$ cat README.md
# Git Playground
This is my Git playground.
I will learn Git today during the XKE.
I will read a Git tutorial https://www.atlassian.com/git/tutorials/.
I will watch a Git video by Linus Torvalds https://www.youtube.com/watch?v=4XpnKHJAok8
I will try Git using tutorial https://try.github.io/
<<<<<<< HEAD
I will create my first repository today.
=======
I will create a Github account today.
>>>>>>> feature4
The merged output is shown below.
# Git Playground
This is my Git playground.
I will learn Git today during the XKE.
I will read a Git tutorial https://www.atlassian.com/git/tutorials/.
I will watch a Git video by Linus Torvalds https://www.youtube.com/watch?v=4XpnKHJAok8
I will try Git using tutorial https://try.github.io/
I will create a Github account today.
I will create my first repository today.
To make the merge resolved, execute git add
command.
$ git add README.md
Commit it
$ git commit -am "Resolved merged conflict with feature4 branch"
Output of git log
command is shown below as well.
$ git log --oneline --decorate --graph
* 4da527c (HEAD -> master) Resolved merged conflict with feature4 branch
|\
| * 84ee6f1 (feature4) eighth commit. Signup for Github.
* | 0af5877 eighth commit. Create repository on Github.
|/
* b182cc7 Merge branch 'feature3'
|\
| * 6f04457 seventh commit. Try Git.
* | fd759a7 fixed Git tutorial bullet point
|/
* 0039442 Merge branch 'feature2'
|\
| * 904ab35 sixth commit. Watch video.
|/
* 5ce434c fifth commit. Read tutorial
* 79a81e4 fourth commit
* c9a8f2d third commit. Added .gitignore file
* abae250 second commit
* bb2b569 first commit
In Git, every developer has their own local copy of the repository. It has all the repository history and branches. A developer works on his local copy and when she is done with her work, she pushes her changes to a remote repository. A local repository can point to 0 or n remote repository. A remote repository could be on Github or any other cloud VCS provider or even it could be on your own machine.
Let's start by creating a bare repository. Navigate to any convenient location on your filesystem and run the following command.
$ git init --bare git-playground-remote.git
This will create a directory git-playground-remote.git
on your filesystem.
The git remote command lets you create, view, and delete connections to other repositories.
To view all the remotes, execute the following command.
$ git remote
As we have not added any remote yet so command will not print any output.
To add a new remote, execute the following command.
$ git remote add local ../git-playground-remote.git
Please use correct path to your remote repository.
To push our repository changes to this remote repository we can run the following command. This command will be executed from inside the git-playground
directory.
$ git push local master
Counting objects: 37, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (32/32), done.
Writing objects: 100% (37/37), 3.41 KiB | 0 bytes/s, done.
Total 37 (delta 11), reused 0 (delta 0)
To ../git-playground-remote.git
* [new branch] master -> master
Now, you can create multiple local copies of the remote repository using clone
command.
$ git clone git-playground-remote.git git-playground-1
Cloning into 'git-playground-1'...
done.
Change directory and view the contents.
$ cd git-playground-1
You can see history of the repository using git log --oneline --decorate
command.
Let's make a change in git-playground-1
repository.
$ echo "I will read Git in Practice book" >> README.md
$ git commit -am "ninth commit. Read book."
$ git push local master
To get changes in our first repository i.e. git-playground
, you will use pull
command.
$ git pull local master
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 1), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From ../git-playground-remote
* branch master -> FETCH_HEAD
4da527c..b38de01 master -> local/master
Updating 4da527c..b38de01
Fast-forward
README.md | 1 +
1 file changed, 1 insertion(+)
Github is a collaborative code management platform for open source and private projects. It has social features just like any other social website like Facebook, Twitter, etc.
You use Github to store your code on a remote Git repository. You will use Github for following reasons:
-
Storing your data in a cloud solution which will make sure code is backed up and available 24X7.
-
Collaborative code reviews
-
Easily manage teams within organizations.
-
Syntax highlighted code & rendered data
Read more on Github https://github.com/features.
You can sign up for Github at https://github.com/join.
There are alternatives to Github like Bitbucket but Github is the leader.
If you don't already have setup SSH keys then you should refer to https://help.github.com/articles/generating-ssh-keys/
Create a new repository by clicking https://github.com/new.
Give it name git-playground
and choose defaults.
From inside your git-playground
, execute the following command.
$ git remote add origin git@github.com:shekhargulati/git-playground.git
Push the changes to Github.
Fork the repository https://github.com/shekhargulati/git-the-missing-tutorial. Forking repository give you your own copy of the repository.
- Clone your clone on your local machine.
- Write a message in the
testimonials.md
- Commit and push the changes to your fork
- Create a pull request
This section will talk about some of the advanced concepts that might be useful.
Rebasing is the process of moving a branch to a new base commit. It is used to keep Git history linear so you will not have merge commits.
$ git checkout -b feature5
$ echo "I will prefer rebase over merge to avoid merge commit." >> README.md
$ git commit -am "tenth commit. Prefer rebase over merge"
Work on master branch.
$ git checkout master
$ echo "I will prefer merge over rebase as it is easy to understand." >> README.md
$ git commit -am "tenth commit. Merge over Rebase"
Rebase command
$ git rebase feature5 master
Fix merge conflicts.
$ git add README.md
$ git rebase --continue
Now view the logs using
$ git log --oneline --decorate --graph
Delete the branch
$ git branch -d feature5
Applying a commit from one branch to another
Temporarily save your current state without creating a commit.
- Make a change to README.md
- Run
git stash
command - List all stashes
git stash list
- Apply the stash using
git stash apply
orgit stash pop
Allows you write your own commands or shortcuts. Like we have been using git log --oneline --decorate --graph
.
$ git config --global alias.mylog "log --oneline --decorate --graph"
TODO
I mostly use command-line terminal. For Mac, I find gitx useful http://gitx.frim.nl/
-
Most useful 20 git commands
git help everyday
-
To view help of any git command
git help <command>
for examplegit help commit
-
To refer to all the Git terms
git help glossary
-
Overwrite your changes with the changes in the remote
git fetch --all && git reset --hard origin/master
-
Viewing unstaged changes since last commit
git diff
-
List all branches that are already merged into master
git branch --merged
-
List all branches and their upstreams, as well as last commit on branch
git branch -vv
-
Deleting a branch
git branch -d <branch_name>
-
Delete a remote branch
git push origin --delete <remote_branch_name>
-
Undo local changes with last content in head
git checkout -- .
orgit checkout -- <filename>
-
Revert last commit
git revert HEAD
orgit revert HEAD~n
orgit revert <commit_id>
. This creates a new commit with undo -
Reset: Discard last commit. Last commit content is lost.
git reset --hard HEAD
orgit reset --hard HEAD~n
-
Reset: Discard last commit. Last commit is unstaged.
git reset --soft HEAD
orgit reset --soft HEAD~n
-
List all remotes
git remote
-
List all local and remote branches
git branch -a
-
See all commits made since forking from master
git log --no-merges --stat --reverse master..
-
Saving current state of tracked files without committing
git stash
-
Show list of all saved stashes
git stash list
-
Apply last stashed state and delete it from stashed list
git stash pop
-
Forcefully remove untracked files
git clean -f
-
Forcefully remove untracked directory
git clean -d
-
Forcefully remove untracked file and directory
git clean -df
-
Rename a branch
git branch -m <new_branch_name>
-
Always rebase instead of merge on pull
git config --global branch.autosetuprebase always