Skip to content

Git tips and tricks

Eberhard Beilharz edited this page Sep 20, 2013 · 10 revisions

This page is a place to collect things we found out how to do in git

Recover from doing work before creating a feature branch

  1. Open git bash window
  2. git stash
  3. Usual process to create feature branch...checkout develop, rebase, start feature
  4. git stash pop

Usually your changes are there. If someone else modified the same files you will need to merge.

Archive temporary changes

Sometimes you make same changes but then realize that this approach won't solve the problem. Yet you wrote some nice code that you'd like to keep so that you can reuse or consult it later. In between all the changed files are some changes that you also need for the different approach. The way to do this is to commit your changes to a local branch, then reset your workspace to the state it had before the commit:

git commit ...                           # commit your changes
git checkout -b failedApproach           # create a new branch "failedApproach"
git checkout feature/originalBranch      # switch back to your topic branch
git reset HEAD^                          # undo the commit on feature/originalBranch

Handle conflicts in a merge

git mergetool

This will take you through the problem files one at a time. For each pair you will typically hit return and be thrown into your favorite gui tool for resolving conflicts.

You can configure what that tool is...even to be P4Merge. Todo: describe how.

Commit to a gerrit repo without using FW git gui tools

This is helpful if some non-flex repo is using git/gerrit, but the add-ons for git gui are not available.

(As usual, use "git branch feature/Name" to make a branch, git checkout feature/Name to start working on it, make some changes, and stage/commit them--I usually use Git gui for that.)

Finally:

git review develop feature/Name

(Replace feature/Name with the name of your branch, and if necessary "develop" with the name of the branch you are working from.)

There's an even more obscure syntax you can use if you haven't installed the git review extension.

Todo: describe how to install it.

To push for review without the review tool:

git push origin HEAD:refs/for/develop/feature/Name

(Again, replace feature/Name with the name of your branch, and if necessary "develop" with the name of the branch you are working from.)

Recover from committing changes to the wrong branch

You just realized that you committed your changes to develop instead of a feature branch...

git log

Will produce something like

commit 72265073df3c23f860a27b076cf5b085c5a82aef
Author: John Thomson <john_thomson@sil.org>
Date:   Tue Aug 27 10:44:00 2013 -0500

    LT-14852 Shorten keyboard control labels

You need to copy the SHA code (from the commit line above).

git branch feature/LT-14852

(Makes your feature branch...give it your own name, of course)

git rebase -p --onto feature/LT-14852 72265073df3c23f860a27b076cf5b085c5a82aef^ develop

(Replace feature/LT-14852 with the name of your feature, 72265073df3c23f860a27b076cf5b085c5a82aef with your SHA, and develop with the name of the branch you accidentally committed to...often develop will be right. Keep the "^" character at the end of the SHA.)

git checkout feature/LT-14852

At this point you can go into git gui and amend last commit and make sure everything is right.

Make a local branch "track" a remote one

This is the main way you get a shared branch on your system so you can check it out and work on it.

git someBranch --track branch origin/someBranch 

You don't have to use the same name for your local branch but it is much less confusing.

Often you will next want to use git checkout someBranch to start working on it

Clone this wiki locally