![]() |
When collaborating on projects using Git, you’ll often need to switch between different versions of the codebase stored in remote repositories. These versions are managed through branches. To work on a specific version (branch) hosted remotely ( on GitHub), you need to check it out in your local Git repository. Table of Content Approach 1: Detaching the HEADThis approach checks out the remote branch directly without creating a corresponding local branch. Your local HEAD pointer is attached to the remote branch. Step 1: Fetch the Remote Branch (if necessary) : If your local repository doesn’t have information about the remote branch, use `git fetch` to download information: git fetch origin # Fetches all branches from the default remote "origin"
To fetch only a specific remote branch: git fetch origin <branch_name> # Replace `<branch_name>` with the actual branch name
Step 2: Checkout the Remote Branch Use the following command, specifying the remote branch name prefixed with `origin/`: git checkout origin/<branch_name>
Example: To checkout the remote branch `feature/new-design` from the remote repository `origin`: git checkout origin/feature/new-design
![]() Approach 2: Creating a Local Tracking Branch (Recommended)This method creates a new local branch that tracks the remote branch. This allows you to work on the remote branch locally and easily synchronize your changes with the remote repository. Step 1: Fetch the Remote Branch (if necessary) – Same as previous approach Step 2: Checkout and Create a Local Branch Use the `git checkout -b` command to create a new local branch named `<local_branch_name>` that tracks the remote branch `<branch_name>`. git checkout -b <local_branch_name> origin/<branch_name>
Example: Create a local branch named `my-feature` to track the remote branch `feature/new-design`: git checkout -b my-feature origin/feature/new-design
![]() Step 3: Verification After checking out the remote branch using either approach, use `git branch` to verify: git branch
The currently checked-out branch will have an asterisk (*) next to its name. ![]() Choosing the Right Approach
Additional Considerations
|
Reffered: https://www.geeksforgeeks.org
Git |
Related |
---|
![]() |
![]() |
![]() |
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 14 |