Mike
Mike

Reputation: 755

Git - temporarily switch between

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.

Upvotes: 4

Views: 933

Answers (2)

Chris Martin
Chris Martin

Reputation: 30736

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.

Upvotes: 3

VonC
VonC

Reputation: 1324837

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.

Upvotes: 0

Related Questions