1

I've got a local repository (no remote) as I've been building up a migration from subversion.

In there are lots of tags and everything is fine, i.e. I've been running git add . -A, followed by a commit, then a git tag -a, etc.

What I wanted to do was bring back the code in each tag just to compare with my base in another folder, so I did what I would have done in subversion and deleted the files/folders, switched to the tag I wanted (git checkout v1.0.0, etc) followed by doing an equivalent to an update to bring back v1.0.0, etc. into the working folder.

After I did this I realized it thinks I now want to update my tag with a load of deletes.

So, long story short, how do I get my files back, i.e. restore files as they should be like you would do with a subversion 'Update' :)

I'm doing this because I wanted to start with a clean folder every time, rather than doing a checkout into the folder full of the current version.

Thanks.

Neil Walker
  • 6,400
  • 14
  • 57
  • 86

4 Answers4

4

so I did what I would have done in subversion and deleted the files/folders, switched to the tag I wanted (git checkout v1.0.0, etc) followed by doing an equivalent to an update to bring back v1.0.0, etc. into the working folder.

Git is not Subversion. Switching from Subversion to Git is a process of relearning what version control is.

None of that deleting and updating is necessary in Git. Simply git checkout v1.0.0. It's perfectly normal in Git to checkout old versions. What is Git? in the Pro Git book might be helpful to read.

After I did this I realized it thinks I now want to update my tag with a load of deletes.

Assuming you didn't do a git add or git rm, do a git checkout . to restore all your changed and deleted files to their committed versions.

A more all-purpose way to clear out all changes is git reset --hard HEAD. This will restore both your working directory and clear your staging area.

Schwern
  • 153,029
  • 25
  • 195
  • 336
  • Thanks. I have been rewiring my brain for git, it just takes time to understand the three trees and all the exotic stuff :) – Neil Walker Aug 29 '19 at 17:23
  • Because I only had a local repository I just wanted to checkout every tag and check, and figured having a blank folder would be best to ensure moved/deleted folders and files would all be ok. – Neil Walker Aug 29 '19 at 17:26
  • @NeilWalker Git will handle that for you. – Schwern Aug 30 '19 at 03:31
3

When in doubt, run git status because it will give you hints about what you can do next.

# Changes not staged for commit:
#   (use "git add/rm <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       deleted:    my-important-file
#       deleted:    another-important-file
#

All in one command, you could do that as

git checkout -- my-important-file another-important-file

Git likes to descend recursively into directories.

git checkout -- .

Sometimes you want to let the big dog bark.

git ls-files -d -z | xargs -0 --no-run-if-empty git checkout --
Greg Bacon
  • 134,834
  • 32
  • 188
  • 245
  • Thanks. As above, I did actually try 'git checkout --' but I forgot to put '.' at the end. In the end the other chap's suggestion of 'git checkout .' worked. From reading, I believe '--' is just to differentiate between a branch and a file so just '.' was ok for me. – Neil Walker Aug 29 '19 at 17:45
  • @NeilWalker Correct, see the [`git config` documentation](https://git-scm.com/docs/git-checkout#_argument_disambiguation). Using `--` is a good habit to develop with git. – Greg Bacon Aug 29 '19 at 17:49
2

First of all, unless you are using git <2.0.X you don't need to use git add . -A this is the default behavior since git 2.0.0

In order to "get back" to your previous content read this answer and use git reset with git reflog

How to move HEAD back to a previous location? (Detached head) & Undo commits


I'm doing this because I wanted to start with a clean folder every time, rather than doing a checkout into the folder full of the current version.

So why not checking out from the required tag?

git checkout -b <new branch> <tag>
CodeWizard
  • 128,036
  • 21
  • 144
  • 167
  • All I get is an empty folder and git status just says 'deleted: xxx'. I tried git revert and git checkout --. btw, what I mean is I was in my working folder, deleted the files in file explorer (no git add/commit). I did this to then bring back all the files for a specific tag to ensure deleted/moved files in each tag were ok. – Neil Walker Aug 29 '19 at 17:19
  • 1
    Thanks for the tip too, I'm on 2.22 – Neil Walker Aug 29 '19 at 17:27
0

You've already got good answers for the immediate question about restoring the files, but I want to point out the root of the problem...

What I wanted to do was bring back the code in each tag just to compare with my base in another folder, so I did what I would have done in subversion and deleted the files/folders, switched to the tag I wanted (git checkout v1.0.0, etc) followed by doing an equivalent to an update to bring back v1.0.0, etc. into the working folder.

You certainly can look at differences between different versions of your project by putting both versions on your machine somewhere and comparing them by hand or with a diff tool, but since git likely has the versions in its repository already, you don't usually need to do that. You can use git diff to compare between commits, branches, tags, individual files, or across time periods. I'd show some examples, but there are already plenty at the bottom of the doc page (or just type git diff --help and scroll to the bottom). And if the versions you want to compare are stored in Github, you can easily compare them without even needing to pull those versions down to your machine.

Caleb
  • 124,013
  • 19
  • 183
  • 272
  • Thanks. I did actually do a diff on everything using the command line and git gui. I was just trying to blank the folder and retrieve everything back as an exercise in itself. – Neil Walker Aug 29 '19 at 22:39