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 .
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/
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'
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 commityou 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/
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
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 -- .
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
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"
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
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
git rebaseon 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 -von their local main branch
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
Apply the changes introduced by some existing commits
# import a commit from another branch git cherry-pick <commit-id>
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