Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I move master back several commits in git?

Tags:

git

git-reset

I have a git repository which holds a Drupal site. I spent the last day trying to build a feature using several different modules. I have given up on my current approach and have decided to try a different combination of modules. However, my repository has several commits on the master branch that contain this feature development process (I understand that I did not branch in an effective manner.) I want to get rid of the last three or four commits and set master to that point in my history (I don't want to merge my current work with anything, I just want it to go away.) How do I do this?

like image 806
Hoytman Avatar asked Apr 22 '14 18:04

Hoytman


People also ask

How do I revert multiple commits at once?

Another way of reverting multiple commits is to use the git reset command.

How do I move a master to another commit?

Use git branch <branch> to create a new branch at the tip of the current master . Use git reset HEAD~<n> --hard to rewind back <n> commits and discard changes. Use git checkout <branch> to switch to the new branch. Only works if the changes have only been committed locally and not pushed to the remote.

How do I move multiple commits from one branch to another?

You can do this with multiple commits too, just cherry pick several, then reset back to the last commit you want to keep. The process is the same if you have committed to local master by mistake - just cherry-pick to a branch, then reset master. Only ever do this if you haven't pushed the commits to origin.


1 Answers

In order to do it locally, you can do the following commands to go to master and move it to the old commit.

git checkout master git reset --hard <old_commit_id> 

If you then want to push it to the remote, you need to use the -f option.

git push -f origin master 
like image 119
merlin2011 Avatar answered Sep 21 '22 23:09

merlin2011