Git Solutions

It is quite common to run into interesting/annoying/challenging problems when working with git, so here are some solutions that have worked for me. However, you might find your specific circumstances mean they don't work for you.....

Changing Commit Author

I recently had an issue where a number of my commits were done with the wrong author details, so I used Changing author info - GitHub Help to fix the issue, which worked well.

Resolving Merge Conflicts

If you do a git merge and get a message saying something like "Automatic merge failed; fix conflicts and then commit the result" then you will need to do something like the following:

  • git status - this will report the list of un-merged files
  • fix the files, this means you need to deal with all the lines between the less than signs and gretaer than signs
  • git add <filename> - you'll need to do this for all the files
  • git commit - although you might want to add a message to this but a default will be prompted

You can see an example of this at 30. Resolving Conflicts.

Changing Directory Structure

if you need to change your directory structure then do the following:

  • Confirm everything is committed into Git and pushed up to the origin
  • Move your directories and files around
  • git status will reveal what you changed
  • git add --all adds the new directories
  • git commit -m "suitable descriptive message" to commit locally
  • git push to send to code up to the central server
  • All done, check the server's commit and you will see stuff has "moved"

Squashing or Merging

Sometimes when working with GitHub or other git repositories you want to merge your changes into one commit instead of several. Let me explain, in the context of GitHub.
I created a fork of a project, made my changes and submitted a pull request for the repository owner to merge my changes in. However I needed to do a bit more work on the change, so after some comments back and forth with a couple of people I ended up with a number of commits in my fork, which would then, I believe, all appear in the main repository, the owner asked if I could merge/squash these into one commit. So this is what I did:

  • In base/root of my copy execute git log -5, where I had made less than 5 commits
  • Look for the commit before mine and grab its id, c3c8f8ce568114d7aae1ae263e03390617c7c878
  • Then execute git reset c3c8f8ce568114d7aae1ae263e03390617c7c878
  • Now you can update your local repository with git add filename.ext and then git commit
  • Once you have everything checked in locally, execute git log -2 and check it is as expected
  • Next you need to force your changes up with git push --force

This of course makes the assumption there were not other conflicting commits in the mean time.

There is more information on git reset and other similar command at Git checkout | Atlassian Git Tutorial.

I have since discovered that the merging or squashing of commits can be done with git rebase, which is an option worth looking at.


If you do work on the Git server, like creating a Pull Request, merging it and then deleting the source branch you will find your local Git has an orphan branch. The first step is to switch your local Git off this orphan branch and then it is best to delete it. First execute a prune to remove remote tracking references:
git fetch -p
Then list your branches, probably with the verbose option to see more detail:
git branch -vv
Finally once you have found the name of the branch that is no longer on the server you can delete it like this:
git branch -d my-branch-to-delete
Listing the branches again will confirm it has gone. If you want to script a purge of multiple branches then see Pruning obsolete Git branches for details.

Undoing a commit

If you have made some changes and committed these changes, then you can easily undo the commit, however, this assumes you have not pushed the commit to a remote.
git reset --soft HEAD~
This command will undo the commit but also leave any other uncommitted changes intact. So if you only committed some of your changes in the commit, then you get back to where you were. If you change the --soft for --hard then any uncommitted changes will be reverted before the commit is rolled back.