Ben Ryder

Uncommon Common Git Operations

A collection of git operations I find myself searching for surprisingly often.

This aims to be a collection of "Uncommon common" git operations, meaning they are relatively uncommon operations that I find myself searching for often enough to write them down somewhere.
This is in no way meant to be an exhaustive list, just a reference for myself that might be useful to others.


Apply the latest gitignore changes #

If you make gitignore changes it's possible that your repository will need to be "refreshed" for all rules to apply, especially if you add rules for files that are currently being tracked by git.
To ensure your repository is up to date with the latest gitignore changes you can run the following commands:

git rm -r --cached .
git add .
git commit -m "Updating repo with gitignore changes."  

Note: It's important that you commit or stash all work prior to running these commands as you will lose all untracked changes.

Moving a commit to a different branch #

Every now and again it's possible you may commit directly to the master or develop branch forgetting to create a feature branch first. Luckily - assuming you haven't pushed the changes yet - it's quick and painless to move that commit to a feature branch.

If <base-branch> contains 1 rogue commit you want to move to <new-feature-branch> then you can run the following:

git checkout -b <new-feature-branch>
git reset --hard HEAD~1 <base-branch>

If you have more than one commit to undo you can change HEAD~1 to use a different number.
This operation rewrites the base branch history which may cause issues if the branch has already been pushed. In these cases you could use the --force flag to force overwrite history however if there are other developers on the project you should avoid that and use git revert or similar instead.

View more articles