I read on here and searched a lot but didn't find the answer, so Is there a way to switch between commits like you do with branches.Let's say I have these commits: a;b;c where c is my last commit, can I switch back to commit a? Or you have to do a git diff and modify the files manually?
- 72,968
- 25
- 171
- 229
- 9,628
- 24
- 90
- 154
-
1A branch _is_ a commit (with a symbolic name, but a commit nevertheless) – fge Jan 07 '12 at 22:00
-
1possible duplicate of [How can I switch my git repository to a particular commit](http://stackoverflow.com/questions/4940054/how-can-i-switch-my-git-repository-to-a-particular-commit) – Jul 20 '13 at 20:18
3 Answers
Just type git checkout a. Or perhaps more usefully, git checkout -b mybranch a, to checkout a as a new branch mybranch.
If you want to revert b and c, you can use git revert, or to remove them entirely from your current branch's history, you could git rebase -i a and throw them out.
Even if you were going to use git diff, you wouldn't have to do anything manually. Check out git format-patch, git apply, and git am to automate creating and applying patches.
- 219,201
- 40
- 422
- 469
-
So I guess that it's not possibile to switch between commits, only if you create branches and than switch between them? I'm right? – Uffo Jan 07 '12 at 19:37
-
-
1
-
@svick - good idea, much better than mine I think... must have slipped my mind while writing. – Carl Norum Jan 07 '12 at 20:06
-
1@Uffo - yes that's correct. But depending on what you want to do, that may or may not be a good plan. – Carl Norum Jan 07 '12 at 20:06
You can create a branch from the revision you want to work from. The revision number can be seen using
git log
Branch out from the previous revision
git branch -f branchname rev
- 2,337
- 1
- 16
- 14
-
This is good just in case you have created branches, but not when you have only the master branch and you want to switch between commits I guess – Uffo Jan 07 '12 at 19:35
-
You can force your master branch to point to older revision. Use the master as the branchname – Shraddha Jan 07 '12 at 19:44
git uses the definition of a commitish. git defines a commitish as:
commit-ish
Indicates a commit or tag object name. A command that takes a commit-ish argument ultimately wants to operate on a commit object but automatically dereferences tag objects that point at a commit.
This seems slightly incomplete as branches are also often treated as commit-ish.
Basically, you can checkout anything that has a sha-1 hash.
- 18,272
- 3
- 32
- 37