Version 9 (modified by andar, 14 years ago) (diff)


Git Tips


  1. Apply a commit to multiple branches
  2. Ignore changes in a tracked file
  3. Stashing untracked files
  4. Merging a remote branch

This page is a collection of useful git tips.

Apply a commit to multiple branches

  1. Checkout master branch
    git checkout master
  2. Make commit
    git commit -m "Fixed bug"
  3. Checkout stable branch
    git checkout stable
  4. Apply the last commit from master to this branch
    git format-patch -k --stdout -1 master | git am -3 -k
  5. Repeat steps 3-4 as necessary
  6. PROFIT!

Cherry-pick alternative

After you make your first commit, you can then cherry pick this change in other branches.

git checkout stable
git cherry-pick <commit id>

Ignore changes in a tracked file

To ignore:

git update-index --assume-unchanged <tracked-file>

To stop ignoring:

git update-index --no-assume-unchanged <ignored-tracked-file>

Stashing untracked files

At times you will be working on a branch with untracked files (new files) and you'll need to checkout master to apply a bug fix.

First, add the untracked files to the index:

git add <untracked-files>

Now do a regular stash:

git stash

At this point you can checkout whatever branch you want and do what you need to do. When you're done, checkout your original working branch again and pop the changes from your stash:

git stash pop

You can remove the files you indexed from the index if you want, as to not pollute your next commit:

git rm --cached <files>

Merging a remote branch

First, add the repo:

git remote add <reponame> <location>

Now fetch in the changes:

git fetch <reponame>

You'll want to do a diff against your branch to see what will be merged:

git diff master <reponame>/<branch>

If you're happy with the changes, go ahead and merge it:

git merge <reponame>/<branch>