$$ \newcommand{\problemdivider}{\begin{center}\large \bf\ldots\ldots\ldots\ldots\ldots\ldots\end{center}} \newcommand{\subproblemdivider}{\begin{center}\large \bf\ldots\ldots\end{center}} \newcommand{\pdiv}{\problemdivider} \newcommand{\spdiv}{\subproblemdivider} \newcommand{\ba}{\begin{align*}} \newcommand{\ea}{\end{align*}} \newcommand{\rt}{\right} \newcommand{\lt}{\left} \newcommand{\bp}{\begin{problem}} \newcommand{\ep}{\end{problem}} \newcommand{\bsp}{\begin{subproblem}} \newcommand{\esp}{\end{subproblem}} \newcommand{\bssp}{\begin{subsubproblem}} \newcommand{\essp}{\end{subsubproblem}} \newcommand{\atag}[1]{\addtocounter{equation}{1}\label{#1}\tag{\arabic{section}.\alph{subsection}.\alph{equation}}} \newcommand{\btag}[1]{\addtocounter{equation}{1}\label{#1}\tag{\arabic{section}.\alph{equation}}} \newcommand{\ctag}[1]{\addtocounter{equation}{1}\label{#1}\tag{\arabic{equation}}} \newcommand{\dtag}[1]{\addtocounter{equation}{1}\label{#1}\tag{\Alph{chapter}.\arabic{section}.\arabic{equation}}} \newcommand{\unts}[1]{\ \text{#1}} \newcommand{\textop}[1]{\operatorname{#1}} \newcommand{\textopl}[1]{\operatornamewithlimits{#1}} \newcommand{\prt}{\partial} \newcommand{\pderi}[3]{\frac{\prt^{#3}#1}{\prt #2^{#3}}} \newcommand{\deri}[3]{\frac{d^{#3}#1}{d #2^{#3}}} \newcommand{\del}{\vec\nabla} \newcommand{\exval}[1]{\langle #1\rangle} \newcommand{\bra}[1]{\langle #1|} \newcommand{\ket}[1]{|#1\rangle} \newcommand{\ham}{\mathcal{H}} \newcommand{\arr}{\mathfrak{r}} \newcommand{\conv}{\mathop{\scalebox{2}{\raisebox{-0.2ex}{$\ast$}}}} \newcommand{\bsm}{\lt(\begin{smallmatrix}} \newcommand{\esm}{\end{smallmatrix}\rt)} \newcommand{\bpm}{\begin{pmatrix}} \newcommand{\epm}{\end{pmatrix}} \newcommand{\bdet}{\lt|\begin{smallmatrix}} \newcommand{\edet}{\end{smallmatrix}\rt|} \newcommand{\bs}[1]{\boldsymbol{#1}} \newcommand{\uvec}[1]{\bs{\hat{#1}}} \newcommand{\qed}{\hfill$\Box$} $$
Tags:

Branching

Track new remote branch:

Pull de novo:

git checkout --track <remote>/<branch>

Point existing remote to new local branch:

git branch --track <new local branch> <remote>/<remote branch>

Show commits unique to a given branch

git log --walk-reflogs <branch>

Show braches that a commit is on

git branch --contains <commit>

Merge conflicts

Format is

<<<<<<< their hash
...
<their code>
...
=======
...
<our code>
...
>>>>>>> our hash

(what does it mean if conflicts are nested?)

Resolve with git add. Finish merge with git commit, or more recently, git merge --continue. If conflict arises during rebase or cherry-pick, resume with git {rebase,cherry-pick} --continue.

Stash conflict

If git stash pop doesn’t apply cleanly, and you want to revert, simply git checkout -f [branch]. This is especially useful if you stashed changes in order to switch branches and unsucessfully pop them on the new branch:

$ git checkout somebranch
error: Your local changes to the following files would be overwritten by checkout:
        file1
        file2
Please, commit your changes or stash them before you can switch branches.
Aborting
$ git stash
Saved working directory and index state WIP on <original branch>: <hash> <commit message>
HEAD is now at <hash> <commit message>
$ git checkout somebranch
Switched to branch 'somebranch'
[jh1 canine deloc_hash:11]$ git stash pop
Auto-merging file1
CONFLICT (content): Merge conflict in file2

Of course, you can directly resolve this merge conflict, but sometimes you realize that this isn’t the correct branch to be on. In that case, the pop can easily be resolved by forcing a checkout back to the original branch, and popping again:

$ git checkout -f <original branch>
Switched to branch '<original branch>'
$ git add file2
$ git stash pop

and we are back! Note that you need to explicitly re-add the conflicting file, as it will have implicitly been dropped.

Rebasing

Take range of commits A..B, apply to commit C

git rebase --onto C A^ B

Note that this will create a detached head downstream of C if you are not already on branch C.

Submodules

To remove a submodule

git submodule deinit <path_to_submodule>
git rm <path_to_submodule>

Pull submodules after cloning

git clone <remote> <path> && cd <path>
git submodule update --init --recursive

Questions

  • What’s the definitive way to move a repo containing submodules, given that submodules’ paths are stored absolutely by default?

Miscellany

Selective revert (equivalent of git revert --patch)

git revert --no-commit
git reset --patch