Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete commits from the master branch in Git

Tags:

git

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?

enter image description here

like image 300
polydegmon Avatar asked Jul 02 '15 19:07

polydegmon


People also ask

Can I remove commits from branch?

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.

How do I remove a commit from a remote master?

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.

Can we delete the commits in git?

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.

How do I remove a commit from git repository?

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.


2 Answers

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

  1. 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}

    • {SHA} is the commit ID
  2. Create the branch and publish it to the server (You risk losing your work if you skip this step)

  3. Perform a hard reset to the commit that you want to keep as the HEAD - git reset --hard {SHA}

    • This will result in all the commits that were done after the commit you reset to, will be pending as an incoming sync - Do not sync
  4. To nuke the incoming commits perform a force push using the command : git push -f

like image 126
polydegmon Avatar answered Oct 03 '22 06:10

polydegmon


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.

like image 37
David Deutsch Avatar answered Oct 03 '22 06:10

David Deutsch