git checkout's Examples

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>

DevOpsSchool
Typically replies within an hour

DevOpsSchool
Hi there 👋

How can I help you?
×
Chat with Us