User Tools

Site Tools



git init                               # initialize a Git repo in the current directory
git clone  # clone the code.git repository together with history
git add <filename>                     # add a single file to staging
git add -u                             # add all updated or deleted files to staging (not untracked new files)
git add -A                             # add all files including untracked ones
git add . --all                        # the same as above
git commit -m "comment"                # commit all staged files
git commit --amend --no-edit           # add more changes to your last commit and keep the current comment
git commit -a -m "comment"             # stage and commit all changes in your working directory i.e., skip 
                                       # the 'git add' step
git mergetool                          # use a merge tool to resolve merge conflicts
git diff                               # show differences between all changed files and unchanged ones
git diff efd22..611ce                  # show differences between the commit efd22 (older) and 611ce (newer)
git diff HEAD~1..HEAD                  # show diff between one commit back (HEAD~1) and the latest commit (HEAD)
git diff HEAD~1..                      # the same as above; HEAD is assumed by default
git diff --cached                      # compare the repository to the staging area
git diff --staged                      # compare comitted changes with the last staged snapshot
                                       # - shows staged changes ready to be comitted
                                       # - shows not staged changes
git diff --staged --no-renames         # as above except does not show similarity index 100% i.e., info that
                                       # a file has the same contents which means that it was just renamed
git checkout <filename>                # checkout a file from the repository to revert changes in your working
                                       # directory
git reset --hard                       # remove all the current changes - modifications and deletions
git reset --soft HEAD~1                # move all the changes from the last commit back to staging
git reset --hard HEAD~1                # remove the last commit and discard all the changes
git clean -n                           # list files that would be removed by 'git clean' (untracked files)
git clean -f                           # remove the files
git --version
git status                             # show status, untracked files, and the branch you are on
git status --short                     # show short status: M - modified, A - new, ? - untracked
                                       # col1: staged, col2: modified
git status -s                          # the same as show short status
git tag                                # list versions that have been tagged
git fetch                              # pull out changes from the remote repo
git fetch <remote> <branch>            # specify a remote to fetch from (if you have multiple remotes)
git fetch origin master                # example
git merge <branch_name>                # merge from the branch branch_name to the current branch
git merge feature1                     # example: merge 'feature1' branch to the current branch
                                       # HEAD, master, feature1 are all together
                                       # it's save to delete the feature1 branch because it's been merged
git merge --abort                      # cancel the current merge
git rebase master                      # 'replay' the changes (commits) made in the current branch (myfeature)
                                       # on top of master i.e., prune off the current location of the myfeature
                                       # branch and relocate it on top of master
git checkout master                    # change the branch from myfeature to master
git merge myfeature                    # the merge is fast-forward because git just moves the 
                                       # 'myfeature' label to the top
git pull                               # = git fetch; git merge origin/master
git push                               # push the changes to the remote repo
git push <remote> <branch>             # upload local repository changes to a remote repository
                                       # i.e., transfer commits from your local repository to a remote repo
git push <remote> <remote_branch>:<local_branch>  # the same as above but when the name of the local branch 
                                       # is different than the name of the remote branch
git push <remote> --all                # push all of your local branches to the specified remote
git push origin master                 # example: remote is 'origin', branch is 'master'
git cherry-pick <sha1>                 # pick a single commit and apply it on the current branch
git cherry-pick --abort                # cancel the current cherry picking
git rm <file>                          # stop tracking a file i.e., stage the file for removal
git rm --cached <file>                 # untrack a file but do not stage it for removal
git mv <old_filename> <new_filename>   # rename a file
# Add a remote repo and name it 'origin'. It does not have to be called 'origin'. You can call it any name.
> git remote add origin
> git remote                           # show the remote repos
origin                                 # 'origin' is the remote the source code came from
> git remote -v                        # show remote repos with URLs
origin (fetch)
origin (push)
> git remote rm origin                 # remove the remote 'origin'
# Extablish a correspondence between your local branch and a remote branch.
# Cloning sets the upstream branches automatically.
# git branch --set-upstream-to <local_branch> <remote_branch>
> git branch --set-upstream-to master origin/master
Branch master set up to track remote branch master from origin.
> git pull                             # pull the updates from origin/master
# You can also specify the upstream branch explicitly
> git pull origin origin/master
# Push the current branch and set the remote as upstream
> git push --set-upstream origin origin/master
  • Rebase replays a set of commits on top of a specific base commit.
  • Interactive rebase allows you to control how the changes are replayed.
git rebase <base_commit>               # rebase on the top of <base_commit>
git rebase master                      # example: the base_commit is 'master'
git rebase --continue                  # continue rebase

Labels (aka tags)

git tag                     # show all tags
git tag -a <tag_name> -m "<tag_comment>" <commit_SHA1>  # create an annotated tag
git show <tag_name>         # show a specific tag
git push origin <tag_name>  # push a tag to origin


Branches are labels on SHA1 hashes of individual commits. When you create a new branch all uncommitted (i.e., staged) changes are under this new branch.

Show info about branches:

git branch                             # show local branches
git branch -r                          # show remote branches
git branch -a                          # show all (local and remote) branches
git branch -a | grep integ             # show branches which the name that contains 'integ'
git branch -vv                         # show tracking branches and local branches with information if your local
                                       # branches are ahead, behind or both

Create a new branch, checkout an existing branch:

git checkout <branch_name>             # checkout an existing branch i.e., move HEAD to the branch
git checkout -b <branch_name>          # create a new branch and check it out
git branch <branch_name>               # create a new branch without checking it out
git push -u origin <branch_name>       # push a new local branch to a remote repository
git push origin HEAD:<remote_branch_name> # push the current local branch and give it a new remote branch name
                                       # this creates a new remote branch

Delete a branch:

git branch -d <branch_name>            # delete a branch; if there are any not merged commits the branch
                                       # won't be deleted; it's safe to delete a branch after it's been merged
git branch -D <branch_name>            # force deletion of a branch with all the commits
git push origin :<remote_branch_name>  # remove the remote branch

Recover a commit:

git branch <branch_name> <commit_SHA1> # recover a commit

Rename a branch:

git branch -m <old_name> <new_name>    # rename (move) a branch

Example: Create a new branch and switch to it:

ltara@WBS MINGW64 /c/Projects/Test (origin/master)
$ git branch feature1
ltara@WBS MINGW64 /c/Projects/Test (origin/master)
$ git checkout feature1
Switched to branch 'feature1'
ltara@WBS MINGW64 /c/Projects/Test (feature1)
$ git log --graph --oneline --all --decorate
* 77f6a47 (HEAD -> feature1, origin/master, master) Change README
* dded7b8 Fix typo in README.txt

Example cont'd: Modify a file and create a commit:

ltara@WBS MINGW64 /c/Projects/Test (feature1)
$ echo "Feature1" >> README.txt
ltara@WBS MINGW64 /c/Projects/Test (feature1)
$ git commit -am "Add feature1"
[feature1 b7ef1be] Add feature1
 1 file changed, 1 insertion(+)
ltara@WBS MINGW64 /c/Projects/Test (feature1)
$ git log --graph --oneline --all --decorate
* b7ef1be (HEAD -> feature1) Add feature1
* 77f6a47 (origin/master, master) Change README
* dded7b8 Fix typo in README.txt

Example cont'd: Switch back to master:

ltara@WBS MINGW64 /c/Projects/Test (feature1)
$ git checkout master
Switched to branch 'master'
ltara@WBS MINGW64 /c/Projects/Test (master)
$ git log --graph --oneline --all --decorate
* b7ef1be (feature1) Add feature1
* 77f6a47 (HEAD -> master, origin/master) Change README
* dded7b8 Fix typo in README.txt

Example cont'd: Create another branch (feature2) and check it out:

ltara@WBS MINGW64 /c/Projects/Test (master)
$ git checkout -b feature2
Switched to a new branch 'feature2'
ltara@WBS MINGW64 /c/Projects/Test (feature2)
$ git log --graph --oneline --all --decorate
* b7ef1be (feature1) Add feature1
* 77f6a47 (HEAD -> feature2, origin/master, master) Change README
* dded7b8 Fix typo in README.txt

Example cont'd: Modify the same file as in feature1 and create a commit:

ltara@WBS MINGW64 /c/Projects/Test (feature2)
$ echo "Feature2" >> README.txt
ltara@WBS MINGW64 /c/Projects/Test (feature2)
$ git commit -am "Add feature2"
[feature2 eca0562] Add feature2
 1 file changed, 1 insertion(+)
ltara@WBS MINGW64 /c/Projects/Test (feature2)
$ git log --graph --oneline --all --decorate
* eca0562 (HEAD -> feature2) Add feature2
| * b7ef1be (feature1) Add feature1
* 77f6a47 (origin/master, master) Change README
* dded7b8 Fix typo in README.txt

Commit History

git log                                # list the commit history together with their SHA1 in the reverse 
                                       # chronological order
git log --oneline                      # list commits, one per line
git log --oneline | wc -l              # count the number of commits in the repo
git log --oneline --graph              # show the commit history on the current branch as a graph
                                       # with branches and merges
git log --oneline --graph --all        # show the commit history on all branches
git log --oneline --graph --all --decorate # additionally apply labels to the commits such as tags, local branches
                                       # and remote branches
git log --format=short                 # list commits showing only the author and the commit message
git log <branch_name>                  # list commits from the given branch
git log origin/master                  # example: branch_name is 'origin/master'
git log --stat                         # show detailed commit info
git log --patch                        # show full diff of changes in each file; similar to git diff
git log <remote>/<branch_name>         # list the commit history on the remote branch; <remote> is 'origin'
git l <local_branch> <remote_branch> --graph            # show commits on the local banch and the remote branch
git l feature/mybranch origin/feature/mybranch --graph  # example
git shortlog                           # list commit messages by authors and the number of commits
git shortlog -sne                      # list commit messages and include: s - summary, n - order by the number
                                       # of commits decreasing, e - include the user's email
git show HEAD                          # show the last commit
git show HEAD~1                        # show the commit before the last one
git show <SHA1>                        # show the commit with the given SHA1
git reflog                             # show the history of where HEAD pointed


git stash                            # record the current state of the working directory
git stash list                       # show the stashes
git stash show                       # show stashed files
git stash apply
git stash pop                        # bring the changes back to the working directory
git stash drop
git stash drop stash@{0}
git stash branch <branch_name>       # create a new branch and apply the stash to it

Git Configuration

System-level configuration applies to the entire computer Git is installed on.

  • Linux: /etc/gitconfig
  • Windows: C:\Program Files\Git\mingw64\etc

User-level configuration:

  • Linux: ~/.gitconfig
  • Windows: C:\Users\USERNAME\.gitconfig -or- C:\Users\USERNAME\.gitconfig.d

Repository-level configuration

  • Stored in .git/config in each repo
git config --system [action]   # access system config file (system-level)
git config --global [action]   # access global config file (user-level)
git config --local [action]    # access repository config file (repo-level)
git config --local --list      # list the repo-level configuration
# remove a setting, for example help.autocorrect
git config --global --unset help.autocorrect
git config --global "Mike McFerry"
git config --global ""
# for editing commit messages and viewing diffs
git config --global core.editor notepad 
# enable autocorrecting misspelled git commands, for example 'git statsu' --> 'git status'
git config --global help.autocorrect 1
# use colors when showing information (only in terminal)
git config --global color.ui auto
git config --global core.autocrlf true   # convert crlf to lf before storing in repo and then back to crlf when
                                         # pulling from repo; good for Windows development
git config --global core.autocrlf input  # convert crlf to lf before storing in repo but don't do anything when
                                         # pulling from repo; good for Linux and Mac
git config --global core.autocrlf false  # do not convert crlf; Windows development only

Example: Define an alias:

> git config --global alias.lga "log --oneline --graph --all --decorate"
> git lga
* 77f6a47 (HEAD -> origin/master, master) Latest changes to README
* dded7b8 Fixed typo in README.txt
* 977c77f Fixed bug#123
* 48bcb3c Added cool new feature
* 611ce6b Another update
* efd22b4 Added README.txt
> cat ~/.gitconfig
        lga = log --oneline --graph --all --decorate

General Info

VCS stands for Version Control System

Types of VCS:

  • Local VCS: The version db is stored locally on your computer. Each change is stored as a version patch in the db.
  • Centralized VCS: The version db is located on a central server. Collaborators have a snapshot of a single file they are currently working on.
  • Distributed VCS: The version db resides on each contributor's computer i.e., each client has a full backup of the entire project.

Git checksums:

  • Each version of a file in Git has a unique 40-chars long checksum.
  • If the checksum is the same, the files must be the same.
  • The checksums are generated by SHA1 hashing algorithm.

The three stages of a tracked file in Git:

  • Committed (stored in the local Git database)
  • Modified (work in progress)
  • Staged (marked for commit)

The three states of a Git project:

  • Working Directory (Updated) - This is a copy of a single version of the project from Origin. It contains updated files that have not been added to the staging area yet.
  • Staging Area (Index) - It contains one or more updated files staged for the next commit.
  • .git Repository (Origin) - These are the files cloned from a remote server and stored locally as the Git database and metadata. It contains committed files.
command effect
git checkout Origin –> Working Directory
git add Working Directory –> Staging
git commit Staging –> Origin
notes/tools/git.txt · Last modified: 2019/01/30 by leszek