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.....
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.
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 filesgit add <filename>
- you'll need to do this for all the filesgit commit
- although you might want to add a message to this but a default will be promptedYou can see an example of this at 30. Resolving Conflicts.
if you need to change your directory structure then do the following:
git status
will reveal what you changedgit add --all
adds the new directoriesgit commit -m "suitable descriptive message"
to commit locallygit push
to send to code up to the central serverSometimes 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:
git log -5
, where I had made less than 5 commitsgit reset c3c8f8ce568114d7aae1ae263e03390617c7c878
git add filename.ext
and then git commit
git log -2
and check it is as expectedgit 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.
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.