Git

  

Workflow

Feature branch flow

  1. Checkout the target branch
    git checkout targetbranch
    git pull --rebase origin/targetbranch
  2. Create pull request branch
    git checkout -b feature/prbranch
  3. Merge and squash working branch
    git merge --squash working/sourcebranch
  4. Initiate a Gerrit review
    git review

Change branch parent

git rebase --onto <new-base> <old-base> <head> Move everything after <old-base> in the current branch to sit on top of <new-base> instead.

Change the upstream branch

git branch --set-upstream-to=origin/dev

Pull down a review branch

git review -d c13da21e-ee0a-5712-95e1-5c9c26e5dee5

Verify a Gerrit review

Comment “recheck” on the review or find the job in Alfred and run retrigger.

Clean-up

Replace remote branch

git push origin :<branch>
git push origin <branch>

Revert ALL local commits

git reset --hard origin/<branch>
git clean -fd

Remove remote file and keep local

git rm --cached <filename>

Remove remote directory and keep local

git rm --cached -r <directory>

Merge local commits

git rebase --interactive HEAD~2

Revert

Revert ALL local commits

git reset --hard origin/<branch>
git clean -fd

Revert a single file

git checkout -- <filename>

Replace a single file from another branch

git checkout <branch> -- <filename>

Undo rebase

git reset --hard ORIG_HEAD

Git Reporting

Git reporting requires the my-get.ps1 module.

Branch Report (ps1)

git fetch -p
Get-Branches -NotMerged | where { $_.Username -like "Aaron*" } | Format-Table -AutoSize
Get-Branches -NotMerged | where { $_.Branch -NotLike "origin/release*" -And $_.Branch -NotLike "origin/master" -And $_.Branch -NotLike "origin/dev" } | Format-Table -AutoSize

Search commit history

git log --oneline | Select-String -Pattern ".*voice.*"

Show commit changes

git show fd1a7f61

Sub-Modules

Clone with submodules

git clone --recursive mypath

Update submodules

git submodule update --init --recursive

Patches

Create a patch from the last commit

git format-patch -n HEAD^

Apply patch

git apply my-changes.patch