Nerveware


Cheat sheet

Create a new master

The problem here is that a repository is made in which you've commited changes to the master branch, but now a pull request must be made in order to get the code reviewed. This section will cover how to rename master to development and create an empty master branch.

First navigate to the project root and create some backups.

$ cd /path/to/project $ git push $ cp -r ../project ../project-old $ git checkout master

Next, create the master branch.

$ git branch -m development $ git branch --orphan tmp $ git reset $ git add .gitignore $ git commit -m 'Initial commit' $ git push -f origin master

Finally, we need to synchronize the history. Otherwise, a merge will fail with "fatal: refusing to merge unrelated histories".

$ git checkout development $ git merge master --allow-unrelated-histories $ git branch development -u origin/development $ git branch --set-upstream-to=origin/master master $ git push

In the code above, it has been chosen to put all the source code into a single pull request because is was just a small application. For larger applications I do recommend to create a feature branch per component from the development branch and create a pull request for those braches instead.

Tags

Because I always forget the syntax.

$ git tag -a R01 -m "Release 1" $ git push origin R01

Git subtrees

Subtrees can be added if the branch is clean (no uncommited changes).

$ git remote add some-repo git@gitlab.remote.com:path/to/some-repo $ git subtree add --prefix=some-dir/ some-repo master

Pull subtree once master updated.

$ git subtree pull --prefix=some-dir/ some-repo master

Check subtree and make branch if changed.

read -p "branch name? " A for i in ${A[@]}; do git diff origin/master HEAD -- $i [[ $? != 0 ]] || continue git subtree push --prefix $i/ $i $A; done

Recover overwritten branch

I accidentally overwritten one repository with another and thus ended up with two repositories with the same content.

$ git reflog $ git checkout -b new c174cafd3 $ git branch -D master $ git branch -m master $ git branch --set-upstream-to=origin/master master $ git push -f origin master

Exclude commits from PRs

Cherry-pick commits from branch to master, merge master in branch.

$ git checkout master $ git cherry-pick # from branch "branch" $ git push $ git checkout branch $ git merge -X ours master

Upstream and origin

$ git remote add upstream $(git remote get-url origin) $ git remote set-url origin git@some-url.git $ git checkout master $ git push --set-upstream origin master $ git push origin --tags

Remove a file/direcotry from the history of a single branch

Soon