Git is a distributed version control system. Branching and merging is far easier than with traditional VCS’s.

$ man git
$ man gittutorial
$ man git-help
$ git help
$ git help [commit add etc]

Create a new repo:

$ cd myproject
$ git init
$ git add .
$ git status
$ git commit -m "Initial import."

Changes are added to a cache/staging area before being committed. Files can be added individually before being committed as a second step, or the add-all-commit can be done as one step:

$ git add foo.txt
$ git status
$ git commit
$ git commit -am 'Committed all changes.'

Keep the first line of the commit message short — fewer than fifty characters. Include an additional details in subsequent paragraphs separated by a blank line. The -e flag invokes the editor to compose a commit message:

$ git commit -e

Unstage a file added to the cache but not yet committed:

$ git rm --cached foo.txt

Revert a changed or deleted file:

$ git checkout HEAD -- foo.txt

(A bare double-dash “ – ” avoids ambiguity by separating options from a list of arguments.)

Discard all local working changes:

$ git reset --hard HEAD

Remote Repositories

Create a remote origin repo:

$ ssh mkdir ~/repo/myproject
$ ssh git init --bare ~/repo/myproject
$ cd myproject
$ git remote add origin ssh://
$ git push --set-upstream origin master

Clone a remote repo:

$ git clone ssh://

Pull updates from a remote:

$ git pull [remote]

Additional remotes:

$ git remote -v
$ git remote add mynewremote ssh://
$ git push mynewremote

Add Github as an additional remote:

$ curl -u 'pgorman' -d '{"name":"myproject","description":"This is my project."}'
$ git remote add github
$ git remote -v
$ git push github master

Reviewing Logs and Comparing Changes

$ git log --pretty=oneline

Review commit logs, and compare two commits:

$ git log --stat [master origin foo etc]
$ git diff e274 47c9

Commits are identified by 40-hexadigit hashes. We can refer to a commit by the first few characters of the hash, where there’s no ambiguity.

Show changes over time for a particular file:

$ git log -p myfile

Diff changed files have not yet been staged/cached:

$ git diff

Diff staged/cached files:

$ git diff --cached

Diff HEAD with the previous commit:

$ git diff @~..@

or: $ git diff HEAD\^ HEAD

Branches and Merging

Branches are named. The default branch is ‘master’. The tag ‘HEAD’ refers to the tip (latest commit) of the current branch.

$ git branch
$ git branch --list
$ git branch testbranch
$ git checkout testbrach
$ git checkout master
$ git branch --delete testbranch

If we want to switch branches without committing the work-in-progress in our current branch:

$ git stash
$ git stash list

Restore stashed files once we switch back to the original work-in-progress brach:

$ git stash list
$ git stash apply [stash@{n}]

Merge a branch back into master:

$ git checkout master
$ git merge testbranch

Resolve merge conflicts:

$ git mergetool

or specify the tool:

$ git mergetool --tool=gvimdiff

Config Files

~/.gitconfig has per-user settings.

/etc/gitconfig is global for the box.

Show effective settings:

$ git config -l

~/repo/myproject/.git/config has per-project settings.

~/repo/myproject/.gitignore sets per-project list of files to ignore. Wildcards can be used.

Reviewing a Github merge request

  1. Under the repository name, click Pull requests.
  2. Select a pull request from the list of pull requests.
  3. Near the bottom of the pull request, in the merge box, click “command line instructions”. Follow the instructions.

For example:

Check out a new branch and test the changes:

git checkout -b makhidkarun-master master
git pull master

Merge the changes and update GitHub:

git checkout master
git merge --no-ff makhidkarun-master
git push github master

Not Covered

rebase, tags, blame, pickaxe, bisect