DEV Community

Cover image for 5 git commands you should know
Rogelio Samuel
Rogelio Samuel

Posted on

5 git commands you should know

Here I'm going to list some git commands you should know, explain what they do, and use cases

  • git diff
  • git checkout
  • git reset
  • git stash
  • git commit --amend

git diff

git diff shows changes between commits, files, commit, etc.

  • See what changes have you done since the last commit

you can use git diff (these changes have to be untracked)

Alt Text

  • See changes between 2 commits

git diff <commit id> <commit id> recommend you to put the commitid's in this order: <older commit> <newer commit>

Alt Text

Alt Text

Alt Text

Use cases

  • See what has changed between 2 commits
  • compare what changes you have done since the last commit

git checkout

git checkout allows you to move through the history of your repository, read, write, and come back, also you can use it to change between branches

  • go to a specific commit

git checkout <commit id> to come back you just need to write git checkout <actual branch>, if you are in the development branch, you use git checkout development and you are on the HEAD of your branch

Alt Text

Use cases

  • see the state of your project in a specific commit

git reset

git reset allows you to move through the history of your repository, like git checkout but you can't come back

  • go to a specific commit hard

git reset --hard <commit id> this is going to change your HEAD position to your commit id, removing all commits above that.

Alt Text

Alt Text

Alt Text

you can use it on your computer and see that the commits above are no longer.

Use cases

  • delete changes that you did that have a bug quickly

git stash

git stash, stash the changes in a dirty working directory away, you can use it when you want to remove changes haven't you commit or just see how the directory was without that changes

Alt Text

  • see how the directory was without that changes

git stash to save you changes in a temporary space and to bring them back use git stash pop

Alt Text

  • remove changes haven't you commit

git stash and then git stash drop

Alt Text

Use cases

  • delete changes have you done since the last commit

git commit --amend

git commit --amend allows you to add changes to the last commit without making another one and change the commit message.

git add . to add your changes to staging and the git commit --amend after this, you'll be able to change your commit message, after that, there is no new commit and your changes are committed

Use cases

  • You were wrong writing the commit message and want to change it
  • you forgot made little changes that belong to the last commit

I recommend you to prove these commands on your own computer to get more familiar with them, I hope you learned something :).

Tell us if you know another git command that could be useful and everyone should know.

Top comments (0)