10 Git Commands That a Developer Should Know

Original author: Jeff Hale
  • Transfer


In this article, we will discuss various Git commands that may be useful to a developer or Big Data specialist. You will learn how to check, delete, and tidy up your code. We’ll also look at ways to exit Vim and save time using Bash aliases and the configuration of the Git editor.

Skillbox recommends: Practical course "Mobile Developer PRO" .

We remind you: for all readers of “Habr” - a discount of 10,000 rubles when registering for any Skillbox course using the “Habr” promo code.

We check everything and everything




  • git diff - View all file changes locally. If you specify a file name, the changes will be shown only for it.
  • git log  - View commit history. Can also be used for a file with git log -p my_file. Type q to exit.
  • git blame my_file - See who, what, and when changed in my_file.
  • git reflog  - Show the change log in the local repository header. A great option for finding lost data.

These commands allow you to quickly check everything you need, without any problems. If something is wrong, Git provides a large number of options for deleting or rolling back commits and changes to files.

Return as it was


  • git reset, git checkout and git revert are commands that are used to undo any actions. But they are not so simple, they must be able to handle.
  • git reset, git checkout can be used for both commits and regular files.
  • git revert is used only for commits.

If you work with your own local commits, which are in no way related to team work, you can use all of the listed commands without problems.

If you work in a team and commits are common, then your choice is git revert.



Each team has a whole set of options. Here are the most used ones:
git reset --hard HEAD - cancel indexed and non-indexed changes since the last commit.

We specify a specific commit instead of HEAD in order to undo the changes that occurred after it. --hard both types of changes mentioned above are discarded.

Do not forget to make sure that you do not cancel the commit from the published branch, on which other team members depend.

git checkout my_commit- discard changes my_commit.

HEAD is often used for my_commit to undo changes to your local working directory since the last commit.

checkout is best used for local undoes. In this case, the commits from the remote branch that your colleagues depend on will not be affected!

If you use checkout with a branch instead of a commit, HEAD switches to the specified branch, and the working directory is updated to match the changes. This is the most common use of this command.

git revert my_commit - undo the consequences of changes to my_commit. revert executes a new commit after the change is rolled back.

revert is safe for general projects because the team does not overwrite changes that other branches may depend on.



Sometimes you just want to delete untracked files in your local directory. For example, by running some kind of code that created many different types of files that you do not need. Unfortunately. Clean helps to remove them instantly!

git clean -n - delete untracked files in the local working directory.

-n - flag for test run, nothing is deleted.
-f - flag for deleting files.
-d - flag for deleting untracked directories.

By default, untracked .gitignore files will not be deleted, but this can be changed.



Putting orders


git commit --amend - add phased changes to the last commit.

If nothing is indexed, the command allows you to edit the last message of the commit. Use the command only if the commit has not been merged with the remote master branch.

git push my_remote --tags - send local tags to a remote repository. A good option for assigning versions to changes.

Help, I'm stuck in Vim and can't get out!


Git in some cases opens a Vim editor session. And if you are not too familiar with him, you may find yourself in a difficult situation. And not only you - for example, on Stack Overflow more than 4 thousand users want to know how to get out of Vim.



Here is a four-step plan to help close Vim and save changes:

  • Click i.
  • Enter the commit message in the first line.
  • Esc.
  • Enter: x.

Everything, you are free!

Change the default editor.

You can get rid of Vim altogether if you change the default editor. Here are the commands for working with popular editors. An example of selecting another editor, in our case Atom:

git config --global core.editor "atom --wait"

Shortcuts for Git Commands


And here is a method that allows you to add shortcuts to Git commands for your .bash_profile.

alias gs='git status '
alias ga='git add '
alias gaa='git add -A '
alias gb='git branch '
alias gc='git commit '
alias gcm='git commit -m '
alias go='git checkout '

More information about .bash_profile can be found here .

As for the method above, now you can use gs instead of git status.

Actually, that's all for today. If possible, indicate in the comments which Git commands you use and why.
Skillbox recommends:


Also popular now: