How can I revert back to a Git commit?
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzling Curiosities
--
Chapters
00:00 How Can I Revert Back To A Git Commit?
00:28 Answer 1 Score 54
01:21 Accepted Answer Score 6
01:44 Answer 3 Score 0
01:54 Answer 4 Score 0
02:09 Answer 5 Score 0
03:32 Thank you
--
Full question
https://superuser.com/questions/523963/h...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#git
#avk47
ANSWER 1
Score 54
The above answer is not quite correct - git revert <ID>
does not set your repository to that commit -- git revert <ID>
creates a new commit that undoes the changes introduced by commit <ID>
. It's more or less a way to 'undo' a commit and save that undo in your history as a new commit.
If you want to set your branch to the state of a particular commit (as implied by the OP), you can use git reset <commit>
, or git reset --hard <commit>
The first option only updates the INDEX, leaving files in your working directory unchanged as if you had made the edits but not yet committed them. With the --hard option, it replaces the contents of your working directory with what was on <commit>
.
A note of warning that git reset
will alter history -- if I made several commits and then reset to the first commit, the subsequent commits will no longer be in the commit history. This can cause some serious headaches if any of those lost commits have been pushed to a public repository. Make sure you only use it to get rid of commits that haven't been pushed to another repository!
ACCEPTED ANSWER
Score 6
Git commit only saves it to the stage, which is locally on your computer. Use Push to update it to a remote server (Like github).
Use git revert <ID>
to revert back to a previous commit. each commit has an identifying code.
See here for more details on revert
ANSWER 3
Score 0
you can use this command to reset the particular commit.
git reset <commit it>
ANSWER 4
Score 0
A simple way to do it is to reset to the last good commit. There will also be no mention of this "revert". The last good commit will be the head.
git push -f origin last_good_commit_hash:name_branch_that_you_are_edit
As example if: name_branch_that_you_are_edit = master and last_good_commit_hash = 150e73abb4344fsde499e98880b0bc96fdca4398, then the command will be:
git push -f origin 150e73abb4344fsde499e98880b0bc96fdca4398:master
ANSWER 5
Score 0
to answer your 1st question: after you run the commit
command the file gets saved on your hard disk a s a normal file, but git
also saves the diff of the change in the .git folder in the repository and assigns a hash to that change. That is the way git keeps track of your changes (and you can check it with the log
command, or in the GUI tree view of the history, not sure how its called since i dont use GUI for git)
regarding the part where you asked to get back to a older state there are 2 ways:
revert
: creates a new commit which is basically the opposite diff of the one created by the commit you are reverting (you'll see it in the automatic commit message). this means you create a new node in the commit tree, i.e. you have not changed the history of the project but added to it => when you performgit push
all is goodreset
: you will move the state of your repository history back in time and possibly lose all the changes that were made on top of that. i.e. you are changing the history of the project => when you performgit push
you need to use the--force
/-f
flag. this is DANGEROUS operation so be careful if you use it, you can change the state for other users of the repo.
now for the GUI question you had, you can prob right click on the commit you want to get back to (reset
) or remove the changes that one commit made (revert
) by right clicking a commit in the history tree or maybe theres a button to do it.
the main thing is to understand the difference between revert
and reset
.