1

I added local files to git repo (git add .) and committed.

Then deleted files with git rm * and committed again (second commit). That deleted files both locally and remotely.

Hot to get files back into same local folder from git (from a first commit)?

Thanks.

6
  • But you want to move back in history? Or you want a third revision that will hold those files? Or you might even want to revert the second commit on a third commit.
    – eftshift0
    Commented Mar 28, 2019 at 15:32
  • 4
    "That deleted files both locally and remotely"—no it didn't. git rm is a purely local operation.
    – Chris
    Commented Mar 28, 2019 at 15:33
  • "That deleted files both locally and remotely." Note that this only happens if you do a git push as well. Commented Mar 28, 2019 at 15:33
  • Possible duplicate of Restore file from old commit in git
    – phd
    Commented Mar 28, 2019 at 15:37
  • 1
    Possible duplicate of Find and restore a deleted file in a Git repository Commented Mar 28, 2019 at 15:39

4 Answers 4

5

Since you committed, it doesn't even matter if the local file even exists on your filesystem at the HEAD of your branch, because it is part of the Git history. To retrieve a file from an earlier commit, you may try checking it out:

git checkout abc123 -- path/to/some/file.ext

where abc123 is the SHA-1 hash of the earlier commit. If you don't know what a SHA-1 hash is, just run git log from the bash, and find the earlier commit along with the hash for that commit.

Edit:

If you really want to revert your entire branch to some earlier commit, then a generally safe way to do that is via git revert. So, continuing with the above example, if you wanted to revert the latest commit abc123, you could try:

git revert abc123

This would add a new commit on top of your branch, which however would just functionally undo whatever that previous HEAD commit was doing. This should leave all the files in your project in their earlier state.

8
  • What is -- for? How to revert everything (not just 1 file)?
    – Joe
    Commented Mar 28, 2019 at 15:42
  • My solution was to do git log -a and do git checkout myShaKeyFromCommit. That got back all files from specific commit.
    – Joe
    Commented Mar 28, 2019 at 15:47
  • @Joe Now I'm not sure what you're after. If you just want a revert a few files, use git checkout. If you want to rollback your entire branch to the state of some earlier commit, then use git revert. Commented Mar 28, 2019 at 15:47
  • @Joe OK...but keep in mind that you're now thrown away any commits which were made since the commit you checked out. Commented Mar 28, 2019 at 15:48
  • Thanks. That is fine in my case since just wanted to get all of them back locally first and then go from there..
    – Joe
    Commented Mar 28, 2019 at 15:51
2
git reset --hard HEAD^

will get your branch back one commit and update the working tree accordingly (restoring your files).

At this point you'll only need to push to remote, using --force if you already pushed the previous state.

If you're in a detached HEAD state, though, this won't work and you'll have to check it out (see Tim's answer).

2
  • 1
    Note that this would bring back the earlier files, but would also remove the HEAD commit, and any new files which were introduced in that commit. Commented Mar 28, 2019 at 15:34
  • @Tim Fair point. It was not explicitly stated in OP's question (is there need to preserve HEAD commit?), but this is cautious, you're right. Commented Mar 28, 2019 at 15:36
2

If you only committed removed files, then you have at least two options:

  1. Create a new commit that reverts the old commit

    git revert HEAD
    git push
    
  2. Reset your branch to the previous commit.

    git reset --hard HEAD
    git push -f
    

    Note that this second option is completely destructive. If you added any other changes in the previous commit, it will undo all of those as well. Also, if you are working with another team member on the same branch, then git push -f will cause problems for them. This should only be used as a last resort or if you are sure that you won't mess things up for other people.

1

To go back a commit git reset HEAD~1then push it to origin git push -f origin branch_name

3
  • Force pushing a branch with a rewritten history is not desirable, at least most of the time. If this is what the OP really wants, then you should suggest using git revert. Commented Mar 28, 2019 at 15:34
  • A git revert will appear in the history. The force push gets rid of the mistake. I guess it depends how long ago the delete was pushed. If it was only a couple min I would rewrite history. Commented Mar 28, 2019 at 15:36
  • 1
    Whenever suggesting git reset, include a warning about the dangers. You are right that a force push gets rid of the mistake on the remote. But if any other developers have pulled the mistake, then they can easily push it back to the remote again unless they know to fix it. Commented Mar 28, 2019 at 15:57

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