Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undo several commits in git which have not pushed to remote [duplicate]

Tags:

git

github

I have run git status and

# On branch master
# Your branch is ahead of 'origin/master' by 4 commits.
#   (use "git push" to publish your local commits)
#
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   app/views/layouts/_header.html.erb
#
no changes added to commit (use "git add" and/or "git commit -a")

I want undo all the 4 commits and changes not staged for commit before commiting to my remote repository. How can i do this?

like image 424
Wasswa Samuel Avatar asked May 29 '13 18:05

Wasswa Samuel


People also ask

How do you revert multiple commits in git?

Thus, we can use the git revert command with the --no-commit option. The syntax of the command is git revert --no-commit <commit> . Thus, to revert the three commits of the bug fixes done, we need to do as follows. Thus, the repository in Git is now in the state before committing the bug1 fixed .

How do I undo the last few commits?

Hard Reset Git commit This is the purpose of the “–hard” option. In order to undo the last commit and discard all changes in the working directory and index, execute the “git reset” command with the “–hard” option and specify the commit before HEAD (“HEAD~1”).


2 Answers

You can also run the following to reset to the remote's HEAD:

git reset --hard <REMOTE>/<BRANCH_NAME>

Ex:

git reset --hard origin/master
like image 114
Nicholas Terry Avatar answered Oct 23 '22 00:10

Nicholas Terry


This will throw away all local changes in the working tree and the four latest commits:

git reset --hard HEAD~4
like image 33
Klas Mellbourn Avatar answered Oct 23 '22 01:10

Klas Mellbourn