DEV Community

loading...

Git: push to a new remote branch

chanyayun profile image fiona Updated on ・1 min read

It is important to specify <remote> <branch> in every pull and push in the scenario. If not specified, would be default master.

  • branch
# list all branches, including local and remote
git branch -a

# create new local branch
git branch <new_branch_name>

# switch to another branch
git checkout <another_branch>

# switch no to new local branch (combine the steps above)
git checkout -b <new_branch_name>

# delete a branch, use -D for force delete
# need to checkout to another branch before delete
git branch -d <branch>
Enter fullscreen mode Exit fullscreen mode
  • clone
# clone a repository in specific remote branch
git clone --branch <branch_name> <remote_repo_url>
git clone -b <branch_name> <remote_repo_url>

# clone a specific remote branch 
# and does not track other remote branch
git clone -b <branch_name> --single-branch <remote_repo_url>
Enter fullscreen mode Exit fullscreen mode
  • pull (fetch+merge)
git pull origin <branch>
Enter fullscreen mode Exit fullscreen mode
  • push
# push to a new remote branch not yet created
git push --set-upstream <remote> <branch_name>
git push -u <remote> <branch_name>

# push to remote branch
git push <remote> <branch_name>

# delete a remote branch
git push <remote> --delete <remote_branch_name>
Enter fullscreen mode Exit fullscreen mode

Usually <remote> would be origin unless working with another repository (eg. deploying app to heroku)
origin stands for the remote repository that a project was originally cloned from.


  • overwrite local repository
git fetch --all
git reset --hard origin/master
Enter fullscreen mode Exit fullscreen mode

replace master with whatever branch originally wanted

Discussion (0)

pic
Editor guide