Most Frequently Used Git Commands

Dilanka Muthukumarana
4 min readJun 27, 2019

--

As a developer, we are using version control systems for code maintenance. Most of the companies are using svn version control system and nowadays it is fast moving to the GIT.

As a developer that I used svn as version control long time and it is also using Tortoise SVN GUI tool, which is very user friendly and no need of using command line commands to do the task related to the SVN such as view history, commit, retrieve updates, revert the changes, edit the commit message, etc.. But recently we have moved from SVN to GIT. Initially, I was addicted to GUI tool which was SVN used and with the GIT also I was eager to use GUI tool, but after trying different tools, I realize that command line tool is easy and clear to understand with GIT related operations. So when using git commands, there was a problem to remember all commands and was searching what are the commands which help to achieve day to day GIT related functions. Finally, by usage, I have listed down some of the commands and I thought to share them with other developers as well.

Git command cycle

git command cycle

Create a repository

  1. Create local repository
#git init <your project name>

2. Clone a repository

#git clone <reporsitory url>

Touch with repository

  1. Check new files or modified files
#git status

2. Show the changes in files which are not committed.

#git diff

3. Show the changes in staged files

#git diff --cached

4. Show all staged/unstaged files

#git diff HEAD

5. Show the changes between two commits

#git diff commit_id1 commit_id2

6. Show the change information of a file

#git blame <full file name>

7. Show the file changes for a specific commit

#git show <commit_id>:<filename>

8. Show full change history

#git log

9. Show the history of a file or directory with diffs

#git log -p <file / directory>

10. View the Last n commits messages

#git log --pretty=oneline --abbrev-commit –n

11. View your all commit in GUI form

#gitk

Play with branches

  1. List your all local branches
#git branch

2. List all local and remote branches

#git branch -av

3. Switch branch ( This will update the working directory as well)

#git checkout <your_branch_name>

4. Create a new branch

#git branch <new_branch_name>

5. Delete branch

#git branch -d <delete_branch>

6. Merge branch to another branch

Let's assume that branch_A needs to merge into a branch_B

#git checkout branch_B
#git merge branch_A

7. Making Tag and Tag operations

#git tag <tag_name>

It is better to create a tag before you do any modification to a branch. You can revert back to the Tag if something goes wrong.

#git tag -a <tag_name> -m “Tag Description”

Push tags to the remote repository

#git push --tags

Delete a tag

#git tag — delete <tag_name> // don’t forget to push after deleting to make reflection in remote reposiroty

Make changes in your repository

  1. Add new changes to commit (stage)
#git add <file_name>  // for spesific file
#git add . // for all changed files

2. Commit all staged files

#git commit -m "commit description"

3. Commit all tracked files.

#git commit -am "commit description"

4. Revert or unstage the file changes

#git reset <file_name>

5. Revert or unstage all to the last commit

#git reset --hard

6. Revert the working directory to remote branch

#git reset --hard upstream/<branch_name>Ex: #git fetch origin
#git reset --hard origin/master

7. Amend the last comment

#git commit --amend

Synchronize with the remote repository

  1. Get latest changes from remote repository/branch (NO MERGE)
#git fetch

2. Get the latest changes from remote repository/branch (MERGE)

#git pull

3. Get the latest changes from remote and rebase

#git pull --rebase

4. Push the commit changes to remote

#git push <branch_name>  // branch_name not mandatory

5. Push the committed changes to a new branch in a remote repository

#git push origin <current_branch>:<new_branch>

6. Abort rebase

#git rebase --abort

7. get commit from another branch

#git cherry-pick <commit_id>  // you need to checkout and cherry-pick from the branch that you need to merge the commit

Configurations

  1. List git configurations
#git config --list --show-origin

2. Set editor for GitBash

#export EDITOR=notepad2.exe //any preferred editor

Finally, git help is the most useful resources to follow;

#git command --help

More resource:

Thank you for reading the article and this article will be updated frequently. Appreciate your suggestions and correct me if I am wrong with the content.

--

--

Dilanka Muthukumarana
Dilanka Muthukumarana

Written by Dilanka Muthukumarana

TOGAF® Enterprise Architecture Practitioner | Consultant For Services: https://devinsights.tech/ Buy me a coffee: https://buy.stripe.com/8wMbMpdvO31ycsUbII

Responses (1)