Git commands

In this page

  1. Cloning
  2. Commiting files
  3. Erasing files
  4. Discarding changes and recovering files
  5. Talking to the server
  6. Tags
  7. History and Comparisons

1. Cloning

Unless you are populating the server, the first thing to do is to clone a repository.

$ git clone git@<server>:/repo/git/proj.git   # Copy proj.git to local project

2. Commiting files

Add (Stage)

After you modify or create a file you need to commit it to the local repository. This is done in two steps. First add the file. This command has two purposes: (i) add a modified file to the list to be commited; or (ii) start tracking a file and add it to the commit list.

$ git add <files>   # Add only <files>. Wildcards accepted.
$ git add .         # Add all files (not listed in .gitignore)


If a file was added to the commit list but you changed your mind before committing, unstage it:

$ git reset HEAD <files>    # Unstage files


Adding a file does not put it in the local copy of the repository. You have to commit it. A commit is still local.

$ git commit -m "message"   # Update local repository with added (staged) files

Add & Commit

You can add all files and commit them in a single step.

$ git commit -a -m "message"


You added a commited a few files only to find out that you forgot one file or that missed a small modification in a just committed file. In that case, add the faulty file and amend the last commit.

$ git commit --amend        # Add newly staged files to previous commit

3. Erasing files

Move & remove

Always use the git versions of erase and moving files as it makes commits easier:

$ git rm <file>            # Erase file 
$ git mv <file> <dest>     # Move file to dest


To remove a file from tracking but not from the folder also use the rm command:

$ git rm --cached <file>         # Remove file from tracking but not from folder

Real erase

Ok. Now you screwed up big time and commited a file that was supposed to be really private (such as your credit card PIN). The command below runs through every branch and tag history changing all commits that involved <file>. Commits that become empty are removed.

$ git filter-branch --force --index-filter \
   'git rm --cached --ignore-unmatch <file>' \
    --prune-empty --tag-name-filter cat -- --all

$ git push origin master --force      # And overwrites remote server

3. Discarding changes and recovering files

Unstaged file

If you modified a file by mistake you can reverse to its previous version with the command below:

$ git checkout <file>            # Restore previous version
$ git checkout [rev_key] <file>  # Restore version from revision rev_key

Finding the commit where a file was deleted and restoring it

$ git log -- <file>                # Full commit history of a deleted (--) file
$ git rev-list -n 1 HEAD -- <file> # Last commit involving the deleted file
$ git checkout [commit]^ <file>    # Restore version from 'commit' parent (^)

5. Talking to the server

Push & Pull

The easy way to exchange information with the server is

$ git push [name] [branch]  # Push commits to branch (def: current) in name (def: origin)
$ git pull                  # Pull changes from server for current branch

Fetch & Merge

If you want more control in the recovery of information from the server, it is better to use fetch instead of pull.

$ git fetch [remote_name]  # Get latest changes from remote_name (def: origin)
$ git fetch --all          # Fetches all remotes
$ git mergetool            # Run program to merge conflicting changes 
$ git merge origin         # Merge changes into local repo

Server names

To see remote servers short and long names:

$ git remote [-v]     # -v is to show the long names

Resetting local repo

If you screw up your local repo, you can always force it to match the server:

$ git reset --hard origin/master    # Reset master branch to what was just fetched

6. Tags


Tags are pretty useful to mark important developement milestones.

$ git tag -a <TAG> [CHKSUM] -m "MSG"  # Create annotated tag for commit CHKSUM (or latest)
$ git tag -d <TAG>                    # Delete tag which was not pushed
$ git tag                             # Show all tags

Tags and the server

Tags are not pushed and pulled automatically from the server. You have to do it manually. Why? I have no clue.

$ git push --tags       # Send the tags to the remote server
$ git fetch --tags      # Bring tags from remote server

7. History and Comparisons


To see current status of local repository:

$ git status            # Show current state of affairs


To view and inspect the previous commits:

$ git log [--pretty=oneline] [-<n>]  # Show [last <n>] commits [with one liners]
$ git show <TAG or [CHKSUM]>         # Show details of one particular commit


To compare different versions of files:

$ git diff                      # Modifications made but not staged (nor commited)
$ git diff --cached             # Compares staged files with last commited state
$ git difftool --tool=meld origin master    # To see the diff's in meld