Stay simple and focused

Git Essentials

File Lifecycle

untracted -> staging -> committed to local -> pushed to remote


Working locally

Init a repo:

$ git init

Checking status:

$ git status

Stage a file:

$ git add <file_name>

Stage all files including ones inside a subdirectory:

$ git add '*.txt'

Stage all files under a path:

$ git add -A <path>

Remove a file:

$ git rm <file_name>

git rm will not only remove the actual file from disk, but will also stage the removal of the files for us.


$ git commit -m 'msg...'

View commits:

git log

Working remotely

Add a remote repo:

$ git remote add <remote_name> <remote_url>
$ git remote add origin https://github.com/some-repo.git

Push to remote:

$ git push -u <remote_name> <local_branch_name>
$ git push -u origin master

-u tells Git to remember the parameters, so next time we can simply run $ git push. The local/remote repo has a default branch named master.

Pull from remote:

$ git pull <remote_name> <remote_branch_name>
$ git pull origin master

git pull is a git fetch followed by a git merge.

Replace local with remote:

$ git fetch origin
$ git reset --hard origin/master

It will drop all your local changes and commits, fetch the latest history from the server and point your local master branch at remote's origin/master.


Diff with a remote branch:

$ git diff <local branch> <remote-tracking branch>
$ git diff master origin/master

Can push to remote?:

$ git status -sb

// If contains the word `ahead` in the output, yes, there are commit to push.

Can commit locally?:

$ git diff-index --name-only --ignore-submodules HEAD --

// If returns something, yes, there are some changes to commit.

Diff working copy with last commit:

$ git diff HEAD

HEAD is a pointer to the last commit.

View staged diffs: (view changed caused by recent git add)

$ git diff --staged

Rolling back

Unstage a file:

$ git reset <staged_file>

Revert a file to its last committed status:

$ git checkout -- <changed_file>


Create a branch:

$ git branch <branch_name>

Switch to a branch:

$ git checkout <branch_name>

Merge changes from another branch to the master branch:

$ git checkout master // switch back to master
$ git merge <branch_name> // do the merge
// resolve any conflicts...
$ git add <some_resolved_files...>
$ git commit -m 'Merge changes from <branch_name> to master...'
$ git push // push changes from local to remote
$ git branch -d <branch_name> // done with the branch, delete it now

Preview diffs between two branches:

$ git diff <branch_to_merge_from> <active_branch>

Push branch to remote: (a branch only lives locally unless you push it to the remote and make it accessible by others)

$ git push <remote_name> <branch_name>
$ git push origin local-branch


It is recommended to create tags for software releases. This is a known concept which also exists in SVN.

$ git tag <tag_name> <commit_id>
$git tag 1.0.0 1b2e1d63ff

The <commit_id> stands for the first 10 characters of a commit id.


See commits by a certain author:

$ git log --author=<username>

See each commit in one line:

$ git log --pretty=oneline

See ASCII art tree of all branches:

$ git log --graph --oneline --decorate --all

See only which files have changed:

$ git log --name-status

Useful Hints

Launch built-in GUI:

$ gitk

Use colorful git output:

$ git config color.ui true

Print log one line per commit:

$ git config format.pretty oneline

To unset this flag:

$ git config --unset format.pretty

To view help:

$ git config

Use interactive adding:

$ git add -i

Syncing a fork

List the current configured remote repositories for your fork:

$ git remote -v

Specify a new remote upstream repository (e.g. the repository that you forked from) that will be synced with the fork:

$ git remote add upstream <url-to-repository>.git

Verify the new upstream repository you've specified for your fork:

$ git remote -v

Fetch the branches and their respective commit from the upstream repository. Commits to master will be stored in a local branch, upstream/master:

$ git fetch upstream

Checkout your local master branch:

$ git checkout master

Merge the changes from upstream/master into your local master branch:

$ git merge upstream/master