List differences between versions of a file

Differences in content between the working directory and the staging area:

$ git diff [files | --name-only] 

Differences in content between the last commit and the staging area:

$ git diff --staged [files | --name-only]

Differences in content between the working directory and the last commit:

$ git diff HEAD [files | --name-only]

NOTES ON OPTIONAL PARAMETERS:

1. 'files' is a space separated list of files; if none are specified it shows all files that meet the query

2. '--name-only' shows only the name of the files that meet the query, if not set, it show the actual differences


Show all the modification made in the last commit

$ git show [--name-only] HEAD

Replace the last commit

If you want to modify the last commit you made without creating an additional commit, you have to execute:

$ git commit --amend [-m message]

This command allows you to replace existing files and/or add new ones. In case you want to add a file to the last commit, add the file to the staging area and the execute the command specified above.

If you specify a message, this new message will also replace the one specified in the last commit.


Undo the last commit

In order to undo the last commit and move back all the modifications to the staging area, execute the following command:

$ git reset --soft HEAD^ 

To undo the last commit and discard all the modifications:

$ git reset --hard HEAD^

Remove files from the staging area

$ git reset [files]

NOTE:
This command does not affect the working area or any previous commit; it’s basically the opposite of the command: 'git add [files]'


Discard all changes in the working directory

Execute the following:

$ git checkout HEAD [files] 

Discard unstaged modifications

To discard a specific file:

$ git checkout file

To discard ALL unstaged files (IMPORTANT!: notice the dot at the end of the command):

$ git checkout -- . 

Go back to a previous commit

This command puts everything back to the commit specified and deletes all commits performed after that commit:

$ git reset <commit_sha> 

(where is the commit SHA that can be viewed with ‘git log’)


Show remote repositories

$ git remote -v 

Share!