Skip to content

Instantly share code, notes, and snippets.

@skarabasakis
Last active August 29, 2015 14:21
Show Gist options
  • Save skarabasakis/accc19cede7ed0f9e4e1 to your computer and use it in GitHub Desktop.
Save skarabasakis/accc19cede7ed0f9e4e1 to your computer and use it in GitHub Desktop.
Git Training

Introduction

git commit - Commit under HEAD and make HEAD point to the new commit

git branch <branchname> - Point to HEAD
git branch <branchname> <commit> - Point to
If <branchname> already points somewhere, use -f to force moving the pointer

git checkout <branchname> - Attach HEAD to
When HEAD is attached to , they move together. In this case we call <branchname> the active branch.

git checkout -b <branchname> - Point to HEAD and attach HEAD to <branchname>

git merge <branchname> - Merge into HEAD.
If necessary, a new commit will be created under HEAD, otherwise a fast forward will occur and no new commit will be created.

git rebase <branchname> - Copy and merge all active branch commits under <branchname>
git rebase <ref> <branchname> - Copy and merge all <branchname> commits under <ref>
Rebase merges and serializes the history from two parallel branches, copying the commits of one right after the other

Ramping Up

Relative references can be used in git commands to reference commits without having to look up their hashes.
<ref>^ - References the parent (1st ancestor) of
<ref>~N - References the N-th ancestor of

git checkout <ref> - Detach HEAD, make it point to .
When HEAD is detached all branch-related commands apply to HEAD rather than the active branch

git reset <ref> - Point HEAD to and delete all commits under it.
Reset rewrites history, so it may be impossible to push the branch to a central repo afterwords.

git revert <ref> - Generate a commit that brings the active branch to the same state as <ref>
Revert does not rewrite history

Moving Work Around

git cherry-pick <refs> -- Copy and merge all referenced commits under HEAD, in the order they are referenced
Like rebase but with selected commits rather than a whole branch.

git rebase -i <branchname> -- Rebase active branch under , interactively cherry-picking individual commits

A Mixed Bag

git commit --amend -- Modify HEAD in-place

git tag <tagname> <ref> -- Permanently tag a commit for future reference

Push & Pull -- Git Remotes

git clone <remote> - Copy a remote repo locally and make the workspace match the latest commit to master

git fetch <remote> - Synchronize remote branches from repo, i.e. bring in new commits, without affecting private commits or the workspace

git pull - Fetch, then merge remote branches under HEAD

git pull --rebase - Fetch, then rebase active branch under remote branch

git checkout -b <branchname> <remotebranchname> git branch -u <remotebranchname> Set local branch to follow remote branch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment