- upstream is where you cloned from (
origin) - downstream is any project that integrates your work
These things are equivalent and refer to the branch named master on the remote named origin:
remotes/origin/masterorigin/masterorigin- This is because
remotes/origin/HEAD -> origin/mastersays the default branch for remoteoriginisorigin/master.
- This is because
origin/master- This is a remote branch, i.e. a local copy of the branch named
masteron the remote namedorigin
- This is a remote branch, i.e. a local copy of the branch named
origin masteroriginis a remotemasteris a local branch
- Fetch
masterfrom remoteorigin, storing the results in the local copyorigin/master:
git fetch origin master
- Merge
origin/masterinto localmasterwith local commits
git merge origin/master
Now we can push new changes (local commits + remote updates) back to origin:
git push origin master
-
git remote add origin https://…creates a new connection to a remote namedoriginat urlhttps://... -
git fetch origin feature-branchfetchesfeature-branchfrom remote namedorigin -
git fetch originfetches all branches from remote namedorigin -
git pull originfetches and merges the current branch from the remote namedorigingit pull ...is shorthand forgit fetch ...git merge <retrieved branch heads> <current branch>
git pull --rebase …is shorthand forgit fetch …git rebase <retrieved branch heads> <current branch>
-
git push origin feature-branchpushes thefeature-branchbranch to the remote namedorigin, creating it if it doesn’t exist remotely
git log sarah --not master --author sarahlim
# with diffs inline:
git log -p sarah --not master --author sarahlim # On branch sarah
git diff master...git diff commit1 commit2changes fromcommit1tocommit2git diff --cachedcurrently staged, but notHEAD(last commit)git diff HEADin working directory, but notHEADgit diffin working directory, but not stagedgit diff feature-branchin working directory, but notfeature-branch
-
git checkout ea12emoveHEADpointer to commitea12e(“detachedHEAD”)- Specific commit hash is more specific than a branch
-
git checkout -- filediscards working changes tofile -
git checkout HEAD filecopiesfilefrom current commit to working directory, and stages it -
git checkout filewhat does this do? does it stage everything??
git checkout -- <file>... to discard changes in working directory
git reset HEAD fileandgit reset fileunstages a change but leaves in working directory
PaulBoxley.com – Git caret and tilde
- Caret
^iterates over parents. Usually a commit only has 1 parent, unless it was from a merge.HEAD^ngets thenth parent ofHEADHEAD^is short forHEAD^1
- Tilde
~iterates over first ancestors.HEAD~is short forHEAD~1
HEAD@{n}gets thenth entry (based on 0-indexing) in the reflog
Equivalent ways to go back 2 commits from HEAD:
HEAD^^HEAD~~HEAD~2HEAD@{3}(note zero-indexing)- NOT!!!
HEAD^2, which is the second parent of HEAD, if HEAD was a merge, otherwise illegal
git rebase base-branch feature-branch # on any branch
git rebase base-branch # on feature-branchEquivalently:
- Switch to base
git checkout base-branch - Create new branch off base
git checkout -b temp-feature-branch - Cherry pick feature commits onto new branch
git cherry-pick <feature> <commits> <not> <on> <base> - Switch to the feature branch we’re rebasing
git checkout feature-branch - Make it point to new branch
git reset --hard temp-feature-branch - Don’t need the new branch pointer anymore
git branch -d temp-feature-branch
- Don’t use this on public branches!
Squash
slim 1, slim 2 into a merge commit and push onto john branch
Rebase john 1, john 2, and john 3 on top of master and move john pointer there
- Think Like (a) Git
- Coupled with following along at Visualizing Git