4

I have already looked through: Temporarily switch working copy to a specific Git commit Is it possible to switch between GIT commits? Git switching between commits However, I would like to sort out a specific topic:

I do not work in a team at present,

I use Git to save my project history locally (no GitHub or Bitbucket) and have only master branch,

I don't PUSH my commits outside .git in my working directory until end of project. My question is simple:

how can I safely return to one of my previous commits (kinda time machine) not having in mind to break existing tree indexes?

how can I ensure that files added after this specific commit will not be present in the working directory during this experiment?

how can I then safely return to HEAD of tree and once again - ensure that files deleted after that specific commit in the past (long time ago, not this time) will not be present in the working directory?

The reason I am asking about this is simple: I am learning now and therefore would like from time to time get back for a while to see (read-only) - what happened in the past (say a month ago).

Thank you.

Community
  • 1
  • 1
Mike
  • 465
  • 1
  • 4
  • 8
  • How does `git checkout` not meet your needs? – Chris Martin Sep 05 '14 at 06:51
  • it doesn't remove files in working directory that were added after this specific commit, AFAIK – Mike Sep 05 '14 at 06:58
  • @Mike: it does. It will not remove _untracked_ files, but tracked files are removed if they did not exist in earlier commits. – knittl Sep 05 '14 at 07:05
  • OK. - Noted. - And what about files like .gitignore , .idea , netbeans , etc. that I mark not to be tracked? - No trolling, simply question. – Mike Sep 05 '14 at 07:18
  • @Mike .gitignore is tracked. The other, if untracked, won't be touched by default (or you can apply Chris Martin's solution) – VonC Sep 05 '14 at 07:24
  • @VonC .gitignore isn't special here. It may be tracked, or may not be, and will behave just like all those other files on `git checkout`. –  Sep 05 '14 at 07:55

2 Answers2

3

I think this does what you're looking for.

git stash --include-untracked
git checkout <commit>

git checkout master
git stash pop --index

For a completely clean working tree, you can replace --include-untracked with --all to stash ignored files as well.

Chris Martin
  • 28,558
  • 6
  • 66
  • 126
  • 1. does means what I get as a result of "git reflog" ? - like "24b7ecb" ? 2. does the result of "git stash --include-untracked" becomes HEAD of tree ? – Mike Sep 05 '14 at 07:32
  • Sorry, "refspec" was the wrong word (that means [something else](http://git-scm.com/book/en/Git-Internals-The-Refspec)). Changed it to "commit". It can be a commit hash (like what you see in `git reflog`, yes, but more commonly in `git log`), a tag name, or a branch name. – Chris Martin Sep 05 '14 at 07:39
  • @Mike No, stashing doesn't create a commit. I think the [man page](http://git-scm.com/docs/git-stash) can explain what the stash is better than I can. "Use git stash when you want to record the current state of the working directory and the index, but want to go back to a clean working directory. ..." – Chris Martin Sep 05 '14 at 07:41
  • I have just looked at what stashing does and this is probably not what I want. - Imagine: the project has been finished a month ago. There is nothing to change or add or commit. - I simply want to get back to a specific state, see what it all looked like and return to HEAD. - As a result, after all the experiments, no changes must be applied to working directory and git tree. - I understand that cloning locally is the safest solution, but this requires a bit of extra effort. - And I hope that someone can help me with a bypass. – Mike Sep 05 '14 at 07:57
  • +1. I like the stash approach. I would prefer the clone approach I mention in my answer (less headache that way when you play in a copy of your repo.) – VonC Sep 05 '14 at 07:59
  • @Mike It is very weird to feel so strongly about the preservation of stuff that you have no intention of committing. But that *is* what stash does. What does it do wrong? – Chris Martin Sep 05 '14 at 08:09
  • I see kinda misunderstanding here. - I really don't want to offend anyone. - I simply do not have anything that I want to commit later, but right now I have to PULL. - This is not the case. - I want to jump between working directory snapshots. - And to not care about files, which were created or deleted between a previous snapshot and the current one. That's about it. – Mike Sep 05 '14 at 08:16
  • You pull? I thought you didn't have any remotes! I'm way more confused now. – Chris Martin Sep 05 '14 at 08:29
  • Wrong example. Sorry. Will continue practice of cloning my working dir and experimenting there with "git checkout " . Seems I want from Git what it cannot do. Thanks to everyone participated and have a good weekend. – Mike Sep 05 '14 at 08:42
0

One simple solution is to:

  • clone again your repo locally
  • experiment in that new clone

That way:

  • your original repo remains on master
  • you can do git checkout anOldCommitSHA1 as you want in the second repo

You can remains in the main repo and checkout any older commit you want but:

  • do a git stash before (to make sure any work in progress is stash away)
  • do a git clean -d -f -x after (when checkout master again) to clean any local file you might have generated while using the old commit working tree.
Community
  • 1
  • 1
VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283
  • Thanks. - I am currently using this approach. - But I am kinda lazy to create new entries in vhosts and hosts files for cloned project and would like to know whether or not there is something shorter inside Git, fengshui-way... – Mike Sep 05 '14 at 06:50
  • I don't use repo, I only have a working directory with .git inside – Mike Sep 05 '14 at 07:00
  • 1
    @Mike a working tree with a `.git` folder inside *is* a git repo: you are using a repo. A local repo. A non-bare repo (as opposed to a bare repo, which has no working tree: http://stackoverflow.com/a/24115534/6309). – VonC Sep 05 '14 at 07:01
  • @Mike which means the first solution I mentioned (cloning your local repo) doesn't involve vhost and other network setup: you can clone a local repo (local folder) into another local folder. – VonC Sep 05 '14 at 07:14
  • OK. - Noted. - I am a newbie in Git. - But AFAIK stashing can be applied to "tracked files" only. - And what about files like .gitignore , .idea , netbeans , etc. that I mark not to be tracked? – Mike Sep 05 '14 at 07:23
  • @Mike did you read Chris's answer, especially the `--include-untracked` part? (http://git-scm.com/docs/git-stash) – VonC Sep 05 '14 at 07:24
  • @Mike Jeeze, I didn't think you wanted to touch ignored files... But stash can do that too, if you use the `--all` flag. – Chris Martin Sep 05 '14 at 07:47