Core Git command line reference

Inspired by John Nickell and Jeffrey Way I am making a renewed commitment to use Git primarily from the command line. While the command line may seem like many steps backwards from using a rich graphical interface, I am finding that it makes me much more aware of exactly what each step of Git is doing and it provides infinitely more quick repeatability of Git tasks.

This is list of Git commands I find myself using over and over. Be sure to familiarize yourself with Git aliases; they go a long way towards making the command concise and effective.

The easy way to put Git on a Windows box is to install Github for Windows and then use its Git shell. The added bonus here is you have good GUI available for basic Git operations as well.

Initialize a local repository

git init 

Stage all file changes to the index

git add .

Stage all file changes, including deletes, to the index

git add -A

Commit changes

git commit -m 'commit message here'
or
git commit -a -m 'commit message here'
which automatically stages modified and deleted files before the commit. However, new files you have not explicitly added to Git with ‘git add’ are not affected.

Discard changes

To remove unstaged changes from a specific file:

git checkout path/to/file/to/revert

To delete all unstaged changes:
git checkout -- .

Remove a file from the repository

git rm <file name>

Discard all changes since last commit

git reset --hard HEAD
See this link for more info.

Show the repository’s current status

git status

Show all commits in an abbreviated form

git log --pretty=oneline --abbrev-commit

Show the SHA1 for the current commit

git rev-parse HEAD

Change to another commit and then make it a branch

git checkout nnnnnn
where nnnnnn is the first six characters of the desired commit’s SHA1. This puts the repo in a detached HEAD state. Now make a branch out of the commit you just checked out:
git checkout -b new_branch_name
That new branch is made the current branch. You can now switch among branches.

Show all branches

git branch -v

Create a branch

git branch new_branch_name

Create a branch and immediately switch to it

git checkout -b new_branch_name

Switch to a different branch

git checkout new_branch_name

Delete a branch

git branch -d branch_name

Rename current branch

git branch -m

Rename a branch that isn’t the current branch

git branch -m

Show all modified files

git ls-files -m

Show all files

git ls-files

Create an annotated tag

git tag -a v{x.x.x} -m 'tag message'

Delete a tag

git tag -d abcde
git push origin :refs/tags/abcde

Use Sublime Text as Git’s editor on Windows

git config --global core.editor "'c:/program files/sublime text 2/sublime_text.exe' -w"

Use Sublime Text as Git’s editor on a Mac

git config --global core.editor 'sublime -n -w'
Where ‘sublime’ is a symlink in the $PATH. This Stack Overflow entry was helpful for this.

Show all aliases

git config --get-regex alias

Add a Git command alias

git config --global alias.nn 'xxxx'
where nn is the name of the alias and ‘xxxx’ is the Git command and its arguments. For example:
git config --global alias.rplog 'log --pretty=oneline --abbrev-commit'
Call this alias with this command line:
git rplog

Delete a Git command alias

git config --global --unset alias.nn

Clone a repo

git clone <uri>
From a command line, go to the parent folder where you want the repo located. Cloning the repo will create a child folder under the parent folder you selected.

List remote repos

git remote -v

Show a remote repo

git remote show origin

Remove remote repo location

git remote rm origin

Set remote repo location

git remote add origin <uri>

Update remote repo location

git remote set-url origin <uri>

Update a remote repo

git push -u origin --all or git push -u origin master

Update a remote repo with current tags

git push -u origin --tags