Git checkout: Difference between revisions
(15 intermediate revisions by the same user not shown) | |||
Line 2: | Line 2: | ||
* [[Git_Commands#Local_Repository_Manipulation|Git Commands]] | * [[Git_Commands#Local_Repository_Manipulation|Git Commands]] | ||
* [[git rev-list]] | |||
* [[git switch]] | |||
* [[git restore]] | |||
=Overview= | =Overview= | ||
Either switch to a specific branch or reset files to certain revisions. | |||
To switch to a branch: | |||
<syntaxhighlight lang='bash'> | |||
git checkout <branch-name> | |||
</syntaxhighlight> | |||
To reset files to a certain revision: | |||
<syntaxhighlight lang='bash'> | |||
git checkout -- <path-to-file> | |||
</syntaxhighlight> | |||
Dedicated commands have been introduced to clarify the behavior: | |||
* [[git switch]] to switch to a branch. | |||
* [[git restore]] to reset files to a certain revision. | |||
=Options= | |||
==--== | |||
Do not interpret any more arguments as options. | |||
=Check Out the HEAD of a Branch= | =Check Out the HEAD of a Branch= | ||
Line 18: | Line 42: | ||
git checkout RESTEASY_JAXRS_2_3_2_FINAL | git checkout RESTEASY_JAXRS_2_3_2_FINAL | ||
=Checkout a Tag into a New Branch= | |||
<syntaxhighlight lang='bash'> | |||
git checkout <tag> -b <branch-name> | |||
</syntaxhighlight> | |||
<syntaxhighlight lang='bash'> | |||
git checkout v1.4.0 -b of/1.4.0-experimental | |||
</syntaxhighlight> | |||
==Reposition the local repository on the HEAD of a branch== | ==Reposition the local repository on the HEAD of a branch== | ||
Line 47: | Line 80: | ||
=Reverting Individual File(s) to a Specific Past Commit= | =Reverting Individual File(s) to a Specific Past Commit= | ||
git checkout <''commit-id''> <''pathspec1''> [<''pathspec2''> ...] | git checkout <''commit-id''> [--] <''pathspec1''> [<''pathspec2''> ...] | ||
git checkout 7065a46a0fee1198fb11a2cd7c1adda197a5d5b0 [--] src/main/java/com/example/Example.java | |||
=Check Out at A Specific Date= | |||
<syntaxhighlight lang='bash'> | |||
git checkout 'HEAD@{Sep 01 2020}' | |||
</syntaxhighlight> | |||
If the date goes to long in the past, you'll get: | |||
<syntaxhighlight lang='bash'> | |||
warning: Log for 'HEAD' only goes back to Wed, 16 Sep 2020 18:02:00 -0700. | |||
</syntaxhighlight> | |||
Alternatively, find the SHA of the commit closest to the date with: | |||
< | git rev-list -10 --before="Sep 01 2020" develop --format medium | ||
=Check Out at a Relative Time Interval in the Past= | |||
<syntaxhighlight lang='bash'> | |||
commit_id=(git rev-list -1 --before="@{2.days.ago}" ${branch}) | |||
git checkout ${commit_id} | |||
</syntaxhighlight> | |||
For more details see [[Git_Commit_Limiting#--until.2C_--before|git rev-list --before]] | |||
git checkout | =Check Out a Remote Branch= | ||
<font color=darkkhaki>Verify</font> | |||
<syntaxhighlight lang='bash'> | |||
git checkout -b somebranch --track origin/somebranch | |||
</syntaxhighlight> |
Latest revision as of 21:05, 10 September 2024
Internal
Overview
Either switch to a specific branch or reset files to certain revisions.
To switch to a branch:
git checkout <branch-name>
To reset files to a certain revision:
git checkout -- <path-to-file>
Dedicated commands have been introduced to clarify the behavior:
- git switch to switch to a branch.
- git restore to reset files to a certain revision.
Options
--
Do not interpret any more arguments as options.
Check Out the HEAD of a Branch
git checkout <branch-name>
This has the semantics of switching to the designated branch.
Checkout a Tag into a Detached HEAD
The following command checks out a specific tag - technically the commit that corresponds to the given tag - into a detached HEAD branch.
git checkout <tag-name|commit-id>
git checkout RESTEASY_JAXRS_2_3_2_FINAL
Checkout a Tag into a New Branch
git checkout <tag> -b <branch-name>
git checkout v1.4.0 -b of/1.4.0-experimental
Reposition the local repository on the HEAD of a branch
git checkout <other-existing-branch-name>
git checkout master
Convert a Detached HEAD into a Named Branch
While being in a detached HEAD situation:
git checkout -b <name-of-a-new-branch>
Create and Check Out a New Branch in One Operation
git checkout -b <new-local-branch-name'> [root-commit]
This creates a new local branch and simultaneously switches to it. Optionally, you can explicitly specify a root commit. If not specified, it's the HEAD of the current branch. Note that the operation does not create a corresponding branch in the origin repository, for that you must explicitly publish the local branch in the remote repository.
Note that if the base branch has modifications, they'll show up on both base branch and the new head branch.
Overwrite a File in the Working Tree
Discards a local modification in working tree:
git checkout <pathspec>
Reverting Individual File(s) to a Specific Past Commit
git checkout <commit-id> [--] <pathspec1> [<pathspec2> ...]
git checkout 7065a46a0fee1198fb11a2cd7c1adda197a5d5b0 [--] src/main/java/com/example/Example.java
Check Out at A Specific Date
git checkout 'HEAD@{Sep 01 2020}'
If the date goes to long in the past, you'll get:
warning: Log for 'HEAD' only goes back to Wed, 16 Sep 2020 18:02:00 -0700.
Alternatively, find the SHA of the commit closest to the date with:
git rev-list -10 --before="Sep 01 2020" develop --format medium
Check Out at a Relative Time Interval in the Past
commit_id=(git rev-list -1 --before="@{2.days.ago}" ${branch})
git checkout ${commit_id}
For more details see git rev-list --before
Check Out a Remote Branch
Verify
git checkout -b somebranch --track origin/somebranch