Skip to content

Instantly share code, notes, and snippets.

@dnase
Last active March 10, 2017 15:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dnase/dc7b27be924d82f6877016953fe7a369 to your computer and use it in GitHub Desktop.
Save dnase/dc7b27be924d82f6877016953fe7a369 to your computer and use it in GitHub Desktop.

Troubleshooting Git

Assumptions: You are operating from the root directory of your git repository. You do not have permissions to push to production. In this documentation, when you see something in tags (< >), it means to replace it with your actual value.

Reverting changes

git reflog
# you will see a list of everything you've done in git, across all branches
# each one has an index HEAD@{<index>}
# find the one where things still worked
git reset HEAD@{<index>}

Example:

drew:nginx_proxy drew.nase$ git reflog
e0ef40a HEAD@{0}: commit: cleaned up init.pp a bit, refactor
3f430b0 HEAD@{1}: commit: added example
b462311 HEAD@{2}: commit (initial): initial commit
drew:nginx_proxy drew.nase$ git reset HEAD@{1}
Unstaged changes after reset:
M	manifests/init.pp

Resetting to origin's copy of production

This is the nuclear option if git pull fails and you know that you don't have any local changes you care about.

git fetch
git reset --hard origin/production

Rebasing to production

This will allow you to merge your branch (issue a pull request) to production even if it and production have diverged. For example, if someone else has merged his or her changes into production during the time you have been making edits to your code.

git fetch
git rebase -i production

The -i flag instructs git to rebase your branch in interactive mode so that you can see what git is trying to do.

Stashing your changes locally and applying

This will allow you to temporarily store any local changes that you can then apply. Useful if you accidentally start work in the wrong branch and need to switch without losing your changes.

NOTE: you should only git checkout with the -b flag if your branch does not already exist and you want to create it.

git stash
git checkout -b <branch name>
git stash apply

Deleting old branches

It is important to do housekeeping from time to time on your git repository. Keep in mind that Code Manager will create a new environment for every branch that you push to the remote (github) repository. It is generally a bad idea to leave an enormous number of unused branches laying around. This will make file sync take much longer than it needs to and lead to system instability.

git push origin --delete <branch name>

This is safe to do after your changes have been merged into production.

Old branches can also be deleted through the Github interface in the branches section. Github automatically provides visibility into the state of a branch and groups branches based on their recent utilization. You can see branches that have not been recently used in the "stale" section.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment