Before starting make sure you commit and push your code (see rebase --abort and hard reset in case you mess up and just want to start over). You should also create a backup
git branch backup-your-branch-name
git rebase --interactive HEAD~20NOTE: on
HEAD~20this is the number of commits into the past you will look at while inside the --interactive rebase.
Locate the commit in question
All commits will have the word
picknext to them in your text editor. Change the
picknext to chosen commit to
edit. Save and exit. (in Nano this is ctrl-x then Y then enter). NOTE: Now if you type
git logyou will see that you are sitting at the place in time right after you made the commit you want to change.
git commit --amendwill allow you to make changes to the previous commit
git logto check and make sure your target commit is at the top of the log list.
Make your changes and run
git add .to add those changes.
git commit --amendto open the commit message in your editor, save it, and exit. This will squash your changes into the target commit. This will change the commit hash code.
git rebase --continueand you should get the message
Successfully rebased and updated refs/heads/<your_branch_name>.
Check to make sure everything seems normal and then do
git push -fto replace the remote copy of your branch with your fixed local copy.
git fetch is the command that tells your local git to retrieve the latest meta-data info from the original (yet doesn't do any file transferring. It's more like just checking to see if there are any changes available)
git pull on the other hand does that AND brings (copies) those changes from the remote repository.
git rebase rewrites the commit history in order to produce a straight, linear succession of commits. Basically what it does is take the fetched commits and your commits and reorders them in the correct way (it is actually creating new commits to do that but for a basic understanding we won't get into any of that).
The reason we do a rebase is so that when you push up your code and merge it with the
master branch you won't rewrite anything anyone else added before you.
First, try to do
git rebase --abort this will cancel the rebase and let you start over. If this doesn't fix your issue you can do a hard reset.
A hard reset will replace your local branch with the branch that is stored remotely. !!!BE CAREFUL!!! only do this if you are 100% sure your local branch doesn't have important new info you don't have stored remotely. You can make a backup of your branch if you're concerned.
git reset --hard origin/<your-branch>