Git commands examples

In this article I'll tell you what the most common git commands do. I strongly recommend you to have a look on all git commands in the documentation.

git add .

Read documentation

Will add the changed file from the local directory to the staging area. It tells GIT that you want to include updates to the files in the next commit. This command will not affect the repository in any way until you run the git commit command

You can also add specific files or directories:

# add one file
git add path/to/file.ext

# add all files in a directory
git add path/to/my-directory/

git commit

Read documentation

Record changes to the repository. Usually you need to use this command after you use git add or git rm commads to save the state of the changed files that will be later updated to the repository..


# commit changed files and add a description (Required)
git commit -m 'short description of what you have changed'

# commit changed files and add them into the staging area in the same time
git commit -am 'short description of my changes'

git diff

Read documentation

This command will show the changes between commits, your commit and working tree.

The diff command will show you only the changes that were not staged. So, once you run git add and git commit you won't see the changes any more, even if you don't push them to the repository.


# show all changes, you'll have to scroll the output or use the up/down arrow keys
git diff

# show changes on only one file
git diff path/to/file.ext

# show changes in all files in a directory
git diff path/to/directory/

git status

Read documentation

Show the working tree status. There are not many things to add here for daily use of this command. Read the documentation (the above link) for more insights of the command

git checkout

Read documentation

This command will switch branches or restore working tree files.


# create a new branch with the base on the current branch
git checkout -b my-new-branch 

# switch to another branch
git checkout another-branch

# restore deleted file
git checkout -- path/to/deleted-file.ext

# cancel changes in files
git checkout -- path/to/modified-file.ext

# the same as above bit for all files in a directory
git checkout -- path/to/directory/

# cancel changes in current directory
git checkout -- .

git push

Read documentation

Push your commited changes to the repository.

You need to run this command after you run git add and git commit to upload the changes to the repository on the branch you are working on.


# run this command only the first time after you create a new branch
git push -u origin branch-name # replace "branch-name" with your branch. 

# run this command to upload commited state to the repository
git push

git pull

Read documentation

Use this command to get the changes from the remote repository.


# get the changes from the remote repository on the current branch
git pull

# rebase the current branch on top of the upstream branch after fetching and show changed files
git pull --rebase -v # please do not confuse this command with "git rebase"

git merge

Read documentation

Join two or more development histories together. You will need this command when you'll want to merge your branch into the main branch of your repository or when you'll want to synchronise the changes between your branch and the main one.


# merge main branch into my branch to have the latest changes from the repository
git merge main

# merge a branch or merge request into the main branch (you must be on main branch when you run this command)
git merge --no-ff my-branch

# merge branches fixes and enhancements on top of the current branch, making an octopus merge
git merge fixes enhancements

# merge branch obsolete into the current branch, using ours merge strategy:
git merge -s ours obsolete

# merge branch maint into the current branch, but do not make a new commit automatically:
git merge --no-commit maint

git rebase

Read documentation

Reapply commits on top of another base tip


# rebase multiple branches on top of main branch
git rebase --onto main branch-a branch-b

# rebase the current branch against main branch with interaction
git checkout main
git pull -rebase -v
git checkout my-branch
git rebase -i main # this will open an interactive screen to allow you to change the history of commits
git push -f origin my-branch # note the "-f|--force" which you must use to be able to push your rebased changes on your own branch 

NEVER run git rebase on the main branch. This will break the history for all members in the team. If you however do it, ask every team member to run git pull --rebase -v on their local main branch

git tag

Read documentation

Create, list, delete or verify a tag object signed with GPG


# list all current tags in the repository
git tag -l

# create tag and push it
git tag -a 1.1.0 -m "xyz feature is released in this tag."
  # push the current tag
  git push origin 1.1.0
  # push all tags
  git push --tags

git cherry-pick

Read documentation

Apply the changes introduced by some existing commits


# import a commit from another branch
git cherry-pick <commit-id>

git log

Read documentation

Show commit logs


# list all the commits which are reachable from foo or bar, but not from baz
git log foo bar ^baz

# show commits log and display short commit hash
git log --graph --pretty='%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit

Rollback committed files

If you made a wrong commit and you want to revert the changes on a specific files (one or more), then run the following command:

git checkout 1a2s3d4f~1 -- path/to/file1.ext path/to/file2.ext

Where : - 1a2s3d4f is the commit hash you want to revert to