I'm new to git.
I wonder why reverting changes is so complicated.
1. I want to know how to revert from a not pushed changed (only commit)
2.How to revert from a pushed change.
3.Is there any good interface for launching all these command from linux( non shell usage)
Thank you
12 Answers
Git is quite powerful as we know it. You can see what changes/differences there are between your local copy and repository by issuing this command:
git diff If you have not yet staged or committed, you can replace your working directory with what git has by checking out into that file:
git checkout -- filename where file name could be index.html
Unstaging a file
If you have accidentally added/stageg a file you didn't want to, you can unstage that file by issuing this command:
git reset HEAD filename It essentially resets that file to the last commit stage.
Undoing Commits
This is a bit tricky and not as straightforward as the above two. Changing previous commits can be deadly and break git workflow.
Bad News: If you want to change as far back as last two commits, well, you CAN't.
Good News: If you only want to change you last commit, well you CAN.
To add a new file to your last commit:
git add filename git commit --amend -am "message goes here" To change last commit message:
git commit --amend -m "new message goes here" Contradiction
Although, I said you can't change anything other than your last commit, you can apply a fix.
One fix is to correct your mistake and commit again
The other fix is to revert the state of a file to before any previous commit. This fix needs to be done using a uniquely generated SHA key by Git for each commit. So what you want to do is grab the SHA key for the commit you want to revert back to. To get this key, you should run:
git log You should get something like:
commit b83a5b0a0bbc3bd544cb06bdecb485a83da63008 Author: Username <> Date: Mon Mar 7 18:54:38 2016 +0100 added limiting and offsetting results commit 63a0e591ca7ff9538004afdbd6d07f7a3e5c417b Author: Username <> Date: Mon Mar 7 14:12:34 2016 +0100 removed authorization for login & logout and just the commit key you want to revert to.
63a0e591ca7ff9538004afdbd6d07f7a3e5c417b You don't have to copy everything. It is so uniquely generated that you could just grab the first 10 characters and run this command:
git checkout 63a0e591ca7ff9538004a -- filename This command reverts that particular commit and put the file in your staging area. Like I showed before, if you want to remove this file from the staging area back into your working directory, you run:
git reset HEAD filename also, to remove it from your working directory, run:
git checkout -- filename Note: your last commit is the topmost listed log.
Great News
You can completely revert the changes of your last commit by using the revert command. What this command does is, it adds back deleted files/lines of code, change your modifications back to before it was added and committed. It essentially mirrors your commit.
Like seen before, you need tha SHA key to do this. Just grab the key like before and run:
git revert 63a0e591ca7ff9538004a and git will show you what the commit message would look like, probably in a text editor of your choice. You could edit the commit message if you like. Just save and close the file and your working directory should be back to before.
Note: If your changes are great (files have moved, been renamed etc) you probably would have to merge branches after this command.
There is also the reset command that is quite powerful and must be used with caution. You can read more about it here
Hope this helps.
@awoyotoyin You should not "grab the SHA key for the commit you want to revert back to"
but instead, grab the SHA key for the commit you want to revert.
The docs at show the syntax as git revert ... ...
where ... is first described as:: Commits to revert.