I am working on a regression in the source code. I'd like to tell Git: "checkout the source based on a parameterized date/time". Is this possible?
I also have staged changes in my current view that I don't want to lose. Ideally, I would like to toggle back and forth between the current source, and some version I'm interested in based on a previous date.
New BranchesGit checkout works hand-in-hand with git branch . The git branch command can be used to create a new branch. When you want to start a new feature, you create a new branch off main using git branch new_branch . Once created you can then use git checkout new_branch to switch to that branch.
With branch specifier only ( git checkout branch ) it will switch current working directory to specified branch, keeping local changes if possible and failing otherwise. If you already are on branch , it will do nothing at all.
Indeed, git fetch pulls down the latest code from remote to origin/branch at local. If you have no local branch with the same name, then git checkout 2.1.
You can keep your work stashed away, without commiting it, with git stash
. You would than use git stash pop
to get it back. Or you can (as carleeto said) git commit
it to a separate branch.
You can checkout a commit by a specific date using rev-parse
like this:
git checkout 'master@{1979-02-26 18:30:00}'
More details on the available options can be found in the git-rev-parse
.
As noted in the comments this method uses the reflog to find the commit in your history. By default these entries expire after 90 days. Although the syntax for using the reflog is less verbose you can only go back 90 days.
The other option, which doesn't use the reflog, is to use rev-list
to get the commit at a particular point in time with:
git checkout `git rev-list -n 1 --first-parent --before="2009-07-27 13:37" master`
Note the --first-parent if you want only your history and not versions brought in by a merge. That's what you usually want.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With