This command is used to switch from one branch to another.
git checkout [branch name]
Testing
$ git checkout <branchname>
Switches to the specified branch and updates the working directory
$ git checkout [branch-name]
How to find and restore a deleted file in a Git repository?
Find the last commit that affected the given path. As the file isn't in the HEAD commit, that previous commit must have deleted it.
git rev-list -n 1 HEAD -- <file_path>
Then checkout the version at the commit before, using the caret (^
) symbol:
git checkout <deleting_commit>^ -- <file_path>
Or in one command, if $file
is the file in question.
git checkout $(git rev-list -n 1 HEAD -- "$file")^ -- "$file"
git rev-list -n 1 HEAD -- <file_path>
Switch to the specified branch.
git checkout main
Create and switch to a new branch.
git checkout -b feature-x
Check out a specific commit (detached HEAD).
git checkout abc123
Discard changes in a file, restoring it to the last commit.
git checkout -- index.html
Copy a file from another branch to the current branch.
git checkout main -- index.html
Switch to the previously checked-out branch or commit.
git checkout -
Create and track a remote branch.
git checkout --track origin/feature-x
Create a new branch with no commit history.
git checkout --orphan new-branch
Force checkout, discarding uncommitted changes.
git checkout -f main
During a merge conflict, keep the current branch’s version of a file.
git checkout --ours index.html
During a merge conflict, use the other branch’s version of a file.
git checkout --theirs index.html
Merge conflicting changes in a file during checkout.
git checkout --merge index.html
Show merge conflicts in a specific style (merge or diff3).
git checkout --conflict=merge index.html
Quiet mode, suppress feedback during checkout.
git checkout -q main
Force progress reporting, even in non-interactive terminals.
git checkout --progress main
Disable guessing the branch name when ambiguous.
git checkout --no-guess feature
Update submodules when switching branches.
git checkout --recurse-submodules main
Do not update submodules when switching branches.
git checkout --no-recurse-submodules main
Switch branch or restore files.
git checkout main