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.

Romain Valeri
  • 19,645
  • 3
  • 36
  • 61
Joe
  • 11,983
  • 31
  • 109
  • 183
  • 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 Mar 28 '19 at 15:32
  • 4
    "That deleted files both locally and remotely"—no it didn't. `git rm` is a purely local operation. – ChrisGPT was on strike Mar 28 '19 at 15:33
  • "That deleted files both locally and remotely." Note that this only happens if you do a `git push` as well. – Code-Apprentice Mar 28 '19 at 15:33
  • Possible duplicate of [Restore file from old commit in git](https://stackoverflow.com/questions/6624036/restore-file-from-old-commit-in-git) – phd Mar 28 '19 at 15:37
  • https://stackoverflow.com/search?q=%5Bgit%5D+restore+files – phd Mar 28 '19 at 15:37
  • 1
    Possible duplicate of [Find and restore a deleted file in a Git repository](https://stackoverflow.com/questions/953481/find-and-restore-a-deleted-file-in-a-git-repository) – Code-Apprentice Mar 28 '19 at 15:39

4 Answers4

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.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • What is `--` for? How to revert everything (not just 1 file)? – Joe Mar 28 '19 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 Mar 28 '19 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`. – Tim Biegeleisen Mar 28 '19 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. – Tim Biegeleisen Mar 28 '19 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 Mar 28 '19 at 15:51
  • @Joe Note that `git checkout myShaKeyFromCommit` now puts you in what is called "detached HEAD state". This means that you are not on a branch and that if you do `git commit` it is just dangling in the aether and will be lost the next time you do a `git checkout`. To fix this, you can create a new branch with `git checkout -b new-branch`. – Code-Apprentice Mar 28 '19 at 15:52
  • @Code-Apprentice LOL I was literally just typing out the same comment. If the OP wants to fix that situation, he would have to branch off from that point, but this then risks dropping all commits from the branch which happened in the future. I don't like that solution. – Tim Biegeleisen Mar 28 '19 at 15:53
  • @TimBiegeleisen Yes, this way lurks several dangers. It depends on what other commits exist after `myShaKeyFromCommit`. – Code-Apprentice Mar 28 '19 at 15:55
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).

Romain Valeri
  • 19,645
  • 3
  • 36
  • 61
  • 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. – Tim Biegeleisen Mar 28 '19 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. – Romain Valeri Mar 28 '19 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.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
1

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

EncryptedWatermelon
  • 4,788
  • 1
  • 12
  • 28
  • 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`. – Tim Biegeleisen Mar 28 '19 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. – EncryptedWatermelon Mar 28 '19 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. – Code-Apprentice Mar 28 '19 at 15:57