19

I just started using Git on Windows and I have a small question. When I say git commit along with a message I realize that git commits the file. But where exactly does it store the commited file?

Let's say I make some changes and want to revert back to my third commit using Git GUI. How exactly do I do that?

1
  • The point of Git is that you don't need to know how Git is storing the backups and history. Git handles all of that for you Oct 4, 2018 at 15:05

5 Answers 5

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

1
  • 9
    wrong answer. this only reverts this specific commit. Mar 4, 2020 at 13:37
50

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!

4
  • after I reset how do I commit Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded.
    – Omar
    Jan 24, 2018 at 16:58
  • since we change our code to older commit, in order to push it we should force push it with this command git push -f origin <branch_name>. Otherwise git will always prompt you to take the pull first and then push. Oct 24, 2018 at 10:03
  • 1
    git push -f is NEVER a good thing to do !
    – MikeW
    Jun 27, 2019 at 13:07
  • 1
    I disagree, @MikeW. git push --force is completely unproblematic if you're working in solo on a branch and want its history to be tidy and sensible, for instance as work on a pull request against a forked repository. It's also unproblematic if you have tight coordination with people you cooperate with, they just need to force-pull your changes before they continue work on their side. Sep 28, 2021 at 10:05
0

you can use this command to reset the particular commit.

git reset <commit it>
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

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 perform git push all is good
  • reset: 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 perform git 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.