Version 16 (modified by Cas, 10 years ago) (diff)

update tips

Git Tips


  1. Merge a feature branch
  2. Ignore changes in a tracked file
  3. Stash untracked files
  4. Merge a remote branch
  5. Undo the last commit
  6. Delete the last commit
  7. Delete a remote branch
  8. Apply a commit to multiple branches with cherry-pick

This page is a collection of useful git tips.

Merge a feature branch

In this example we assume that your feature branch, myfeature, is based off develop and that you are currently in your feature branch.

  1. Squash/reword/edit any commits if need be
    git rebase -i develop
  1. Rebase your feature branch on top of current develop
    git rebase develop
  2. Change to develop
    git checkout develop
  3. Fast-forward merge your feature branch into develop
    git merge myfeature
  4. Delete your local feature branch (see above tip for remote deletion)
    git branch -d myfeature

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>

Stash 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>

Merge 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>

Undo the last commit

Changes are put back into staged

git reset --soft HEAD~1

Delete the last commit

All changes are lost''

git reset --hard HEAD~1

Delete a remote branch

git push <repo> :<branch>


git push --delete <repo> <branch>

Apply a commit to multiple branches with cherry-pick

This technique uses cherry-pick to apply commits to branches that differ too much to use merge.

  1. Checkout master branch
    git checkout master
  2. Make commit
    git commit -m "Fixed the bug that caused issue xyz"
  3. Checkout stable branch
    git checkout stable
  4. Apply the last commit from master to this branch
    git cherry-pick master