5 git commands you should know
Tags: git, github
Published: 2021-01-16 01:22:59

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](https://dev-to-uploads.s3.amazonaws.com/i/djaxbqtckjzm07t9yzo3.png)

- 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](https://dev-to-uploads.s3.amazonaws.com/i/5tsrcitdzariaus38v92.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/itjk3valepalmluhldqj.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/20l7csiyja7tv08qlh7g.png)

### 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](https://dev-to-uploads.s3.amazonaws.com/i/sqzoxr0npftj064ogpkn.png)

### 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](https://dev-to-uploads.s3.amazonaws.com/i/et1kzd165gfms41wokz9.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/6w1wimpxgo2lq94tavf4.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/i4m3rb8rg8xjwx5hsia0.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/gozj4khj2lw53th1jn9t.png)

- 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](https://dev-to-uploads.s3.amazonaws.com/i/95acnrb6hrdqqyv86421.png)

- remove changes haven't you commit

`git stash` and then `git stash drop`

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/lwaw7gklz23e1057cd8t.png)

### 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.