I forgot to make a branch before making some code updates and now I have to revert to the initial commit and remove all the commits after the initial one. The work has been saved off somewhere so I'm not at risk of losing it.
I've used the "git reset --hard {SHA}" to reset back to where I need to be, however now I have all the commits that were performed after the initial commit waiting to come back down and sync with my master copy.
I want to delete all commits from 6/10/2015 to 7/2/2015 as they are no longer needed. Does anyone know how to do this?
You can simply remove that commit using option "d" or Removing a line that has your commit. In the latest git version there is no more option d. You need just remove lines with commits from rebase to delete them.
To delete commits from remote, you can use the git reset command if your commits are consecutive from the top or an interactive rebase otherwise. After you delete the commits locally, push those changes to the remote using the git push command with the force option.
Deleting the commit in Git must be approached in one of two ways, depending on if you have or have not pushed your changes. Please note before attempting this, running these commands will DELETE your working directory changes. Any changes to tracked files in the working tree since <commit> are discarded.
First, remove the commit on your local repository. You can do this using git rebase -i . For example, if it's your last commit, you can do git rebase -i HEAD~2 and delete the second line within the editor window that pops up.
Removing Remote Commits From Any Branch [Master is a branch]
If you commit something to the remote server that for whatever reason should not have been committed on the particular branch you can remove it using the following steps
If the branch has changes you want to keep - Start at step 1
If you don't care about the changes and simply want to revert to a specific commit - Start at step 3
Perform a hard reset and reset the HEAD to the commit you want to create the branch from using the command : git reset --hard {SHA}
Create the branch and publish it to the server (You risk losing your work if you skip this step)
Perform a hard reset to the commit that you want to keep as the HEAD - git reset --hard {SHA}
To nuke the incoming commits perform a force push using the command : git push -f
If nobody else uses this repo, do a git push -f
and the server's contents will be overwritten with your contents.
If other people are using this repository (and have pulled since you made the commits you want to remove), they will need to do a hard reset on their end as well after pulling (git reset --hard origin/master
, assuming you are on branch master
). I would suggest in this case to use git revert
to simply undo the effect of your commits instead of trying to eliminate them from the history.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With