1. Clone Target Repository

Clone the remote repository code you want to your local environment. You can find the clone repo link inside the Code button drop-down.

image

cd into the folder you want to clone the repository, then paste the HTTPS link and created a clone with the git clone command.

$ git clone {HTTPS_LINK_HERE}

After running the git clone command, you should see a new folder with the target repo code inside.

2. Create a New Branch

Now cd into the new folder created by the git clone command, and let’s create a new branch by using the git branch command.

$ git branch {NEW_BRANCH_NAME}

After you’ve created the branch, use the following git commands to check if your new branch has been created, and then switch to your new branch.

# check if {NEW_BRANCH_NAME} is listed locally as your branch
$ git branch

# switch to new branch
$ git checkout {NEW_BRANCH_NAME}

3. Push New Branch to Repository

We can push our new branch to the repository with the git push command. But here we also want to set the upstream branch. Every time a new branch is created, we need to set up an upstream branch so the new branch knows which remote repository to use every time it synchronizes its commit history. This step can be achieved with the --set-upstream command.

$ git push --set-upstream origin {NEW_BRANCH_NAME}

If you didn’t set up an upstream branch and push your code, the following error code will show up. Thus, please remember to do this step before pushing your code from the new branch.

fatal: The current branch has no upstream branch

After pushing your branch to the repository, you should see your branch show up on the branches drop-down list shown in the image below. image

4. Push Code Changes

Now you can push code changes into the new branch you’ve just created!

# Checkout your new branch
# make changes to the code and push to new branch
$ git add .
$ git commit -m "new code commit here..."
$ git push

Resources