Notes:
SVN is centralized. Commits are shared immediately, which makes it difficult to commit unless a feature is complete. Because branches are cumbersome and your changes must be rebased onto any remote changes, development is basically linear. By default, all changes are committed.
Git is decentralized. Commits are local until pushed. You must be explicit about which changes to commit.
$ git init
---
$ git clone [email protected]:<project/path>
Notes:
Copies entire repository to your local machine. You cannot clone a subdirectory. That's why it's important to keep repos small.
$ git add file_to.add or_folder_to_add/
$ git commit
Notes:
Files to be committed are added explicitly. Commit
opens an editor for entering a commit message.
Notes:
By default, no files in a git repository are tracked. The first time you add a file to git, it is immediately staged for commit. Unmodified files are identical to their corresponding file in the HEAD
commit, while modified files differ from the snapshot of their corresponding file.
Notes:
Each commit contains a complete snapshot of the files that have changed and a pointer to it's parent commit.
Notes:
Branching: If pipes can do it, so can you.
$ git branch <newbranch>
$ git checkout <newbranch>
---
$ git checkout -b <newbranch>
Notes:
Creating branches is lightening quick, because git just creates a new pointer to the current commit.
Notes:
Why should I use branches? Suppose you discover a critical bug while you are in the middle of working on a feature. If you are working on master, you must work around your partial feature or complete it before you can fix the bug.
Notes:
If you created a branch before starting work on the feature ...
$ git checkout master
$ git branch hotfix
$ git commit -a
Notes:
... you checkout master and create the hotfix ...
N.B.: commit -a
adds all modified files
$ git checkout master
$ git merge hotfix
Notes:
... and merge the hotfix into master.
Since hotfix
is a descendant of master
, git simply moves the master
pointer forward when you merge. This a called a "fast-forward" merge.
$ git checkout iss53
$ git commit -a
Notes:
With the hotfix released, you can continue working on the feature you had started.
$ git checkout master
$ git merge iss53
Notes:
This time master
is not an ancestor of the branch you are trying to merge, so git performs the merge by looking at three commits: the tips of the two branches and their common ancestor.
Notes:
Merge creates a so-called merge commit, which unlike a normal commit points to more than one parent.
$ git push
---
$ git push <remote> <branch>
Notes:
By default, push sends local commits on the current branch to the remote tracking branch. In most cases, the remote tracking branch is a branch of the same name on origin
which is the repository you originally cloned from.
$ git push
To [email protected]:user/project.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to '[email protected]:user/project.git'
$ git pull
---
$ git fetch <remote> <branch>
$ git merge <remote>/<branch>
Notes:
Pull
changes before you start working; it will reduce the chance of your push
being rejected.
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean
Notes:
Shows untracked, modified and staged files.
If you are using git on the commandline, you will be using this ALL the time. If you are using a GUI, it will be run ALL the time for you.
$ git diff
$ git diff --staged
Notes:
Shows a unified diff of modified or, in the case of the staged flag, staged files compared to committed files in the HEAD
commit of your repository.
$ git add -p
$ git reset -p HEAD
$ git checkout -p HEAD
Notes:
The patch flag allows you to stage/unstage/discard partial files. It can also be used to review the changes that are being staged/unstaged/discarded.
$ git difftool [--staged]
Notes:
Get one and set it up so the git commandline tool can use it.
- xplat: P4Merge, DiffMerge
- OS X: FileMerge (xcode), Kaleidoscope
Notes:
First line (subject) completes the sentence, "This commit will...". The body explains why, not how. Provide your future self with your current context.
- Warholian Pipe Branches by Angie Harms (CC BY-ND 2.0)
- Git diagrams from git-scm.org (CC BY 3.0)