Skip to content

Instantly share code, notes, and snippets.

@sliminality
Created November 19, 2017 23:48
Show Gist options
  • Save sliminality/8b5d88ae8663641bbc462a94e87c02b6 to your computer and use it in GitHub Desktop.
Save sliminality/8b5d88ae8663641bbc462a94e87c02b6 to your computer and use it in GitHub Desktop.
messy notes from learning git

Remotes

Upstream vs downstream?

  • upstream is where you cloned from (origin)
  • downstream is any project that integrates your work

Origin

These things are equivalent and refer to the branch named master on the remote named origin:

  • remotes/origin/master
  • origin/master
  • origin
    • This is because remotes/origin/HEAD -> origin/master says the default branch for remote origin is origin/master.

origin/master vs origin master

  • origin/master
    • This is a remote branch, i.e. a local copy of the branch named master on the remote named origin
  • origin master
    • origin is a remote
    • master is a local branch

Example: git pull manually

  1. Fetch master from remote origin, storing the results in the local copy origin/master:
git fetch origin master
  1. Merge origin/master into local master with local commits
git merge origin/master

Now we can push new changes (local commits + remote updates) back to origin:

git push origin master

Commands

  • git remote add origin https://… creates a new connection to a remote named origin at url https://...

  • git fetch origin feature-branch fetches feature-branch from remote named origin

  • git fetch origin fetches all branches from remote named origin

  • git pull origin fetches and merges the current branch from the remote named origin

    • git pull ... is shorthand for
      • git fetch ...
      • git merge <retrieved branch heads> <current branch>
    • git pull --rebase … is shorthand for
      • git fetch …
      • git rebase <retrieved branch heads> <current branch>
  • git push origin feature-branch pushes the feature-branch branch to the remote named origin, creating it if it doesn’t exist remotely

Diffs and jumping between changes

Show my commits on sarah since master

git log sarah --not master --author sarahlim

# with diffs inline:
git log -p sarah --not master --author sarahlim 

Diffs on sarah since master

# On branch sarah
git diff master...

git diff

  • git diff commit1 commit2 changes from commit1 to commit2
  • git diff --cached currently staged, but not HEAD (last commit)
  • git diff HEAD in working directory, but not HEAD
  • git diff in working directory, but not staged
  • git diff feature-branch in working directory, but not feature-branch

git checkout

  • git checkout ea12e move HEAD pointer to commit ea12e (“detached HEAD”)

    • Specific commit hash is more specific than a branch
  • git checkout -- file discards working changes to file

  • git checkout HEAD file copies file from current commit to working directory, and stages it

  • git checkout file what does this do? does it stage everything??

git checkout -- <file>... to discard changes in working directory

  • git reset HEAD file and git reset file unstages a change but leaves in working directory

Referencing commits

PaulBoxley.com – Git caret and tilde

  • Caret ^ iterates over parents. Usually a commit only has 1 parent, unless it was from a merge.
    • HEAD^n gets the nth parent of HEAD
    • HEAD^ is short for HEAD^1
  • Tilde ~ iterates over first ancestors.
    • HEAD~ is short for HEAD~1
  • HEAD@{n} gets the nth entry (based on 0-indexing) in the reflog

Equivalent ways to go back 2 commits from HEAD:

  • HEAD^^
  • HEAD~~
  • HEAD~2
  • HEAD@{3} (note zero-indexing)
  • NOT!!! HEAD^2, which is the second parent of HEAD, if HEAD was a merge, otherwise illegal

Rebasing

git rebase

git rebase base-branch feature-branch  # on any branch
git rebase base-branch                 # on feature-branch

Equivalently:

  1. Switch to base git checkout base-branch
  2. Create new branch off base git checkout -b temp-feature-branch
  3. Cherry pick feature commits onto new branch git cherry-pick <feature> <commits> <not> <on> <base>
  4. Switch to the feature branch we’re rebasing git checkout feature-branch
  5. Make it point to new branch git reset --hard temp-feature-branch
  6. Don’t need the new branch pointer anymore git branch -d temp-feature-branch

Danger

  • Don’t use this on public branches!

Rebase vs. Merge

Start

Two branches diverged

After git merge master

After merging Squash slim 1, slim 2 into a merge commit and push onto john branch

After git rebase master

Result of rebasing

Rebase john 1, john 2, and john 3 on top of master and move john pointer there

Helpful reading

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