
If you specify "HEAD" as the revision, you will restore the last committed version of the file, effectively undoing any local changes that you current have in that file: $ git checkout HEAD index. If, in one go, you also want to create a new local branch, you can use the "-b" parameter: $ git checkout -b new-branchīy using the "-track" parameter, you can use a remote branch as the basis for a new local branch this will also set up a "tracking relationship" between the two: $ git checkout -b new-branch -track origin/developĪnother use case for "checkout" is when you want to restore an old revision of a file: $ git checkout 8a7b201 index.html If the branch does not exists on the remote side it will be automatically created. This will make the given branch the new HEAD branch. As you see it pushes to a branch named dev as well on the remote side. In its simplest (and most common) form, only the name of an existing local branch is specified: $ git checkout other-branch If you want to restore a specific earlier revision you can provide that revision's SHA-1 hash. By providing HEAD as the revision, you can restore the last committed version of a file - effectively undoing any local changes that happened since then. Restores a historic revision of a given file. when unpushed commits in the local branch or unpulled commits in the remote exist). This allows you to more easily see when the two aren't in sync (i.e. This way, the new local branch has a tracking relationship with its remote counterpart. This can be used as a shortcut instead of the following two commands:Ĭreates a new local branch - and sets up an "upstream" configuration. b Ĭreates a new local branch and directly switches to it. By specifying the name of a local branch, you will switch to this branch and make it the current "HEAD" branch.

The name of a local branch that you want to switch to. Thereby, you can reset single files to earlier revisions - while keeping the rest of the project untouched. The most common use case for "checkout" is when you want to switch to a different branch, making it the new HEAD branch.Īnother use case for "checkout" is when you want to restore a historic version of a specific file. Being a good convention, there's rarely the need to change this.The "checkout" command can switch the currently active branch - but it can also be used to restore files. Checkout is the process of switching from one branch to another, so you dont make changes to the wrong version of your site. Note that, by default, Git uses the same name for the local branch. The syntax for making git checkout "remote-ready" is rather easy: simply add the "-track" flag and the remote branch's ref like in the following example: $ git checkout -track origin/newsletterīranch newsletter set up to track remote branch newsletter from origin.īased on the remote branch "origin/newsletter", we now have a new local branch named "newsletter". But it is also possible to create a new Git branch and switch in this branch using only one git checkout command with -b option. Using the "git checkout" command, you can then create a local version of this branch - and start collaborating! After you have created a branch, you need to switch in this branch using a git checkout command.In order to see this newly published branch, you will have to perform a simple "git fetch" for the remote.
#GIT CHECKOUT FILE FROM ANOTHER BRANCH UPDATE#
to update what will be committed) (use git checkout -pulling the remote master into your local feature branch.

Copies a file from another branch to the current branch.git restore -source feature/B - utils.js. Copy a file from another branch Angelos Chalaris
Switch to the branch where you want to checkout the file. You probably knew that a branch can be checked out with the command git checkout#GIT CHECKOUT FILE FROM ANOTHER BRANCH FOR FREE#
Download Now for Free Collaborating with Branches Here is the process to follow to get a file from another branch: 1.
