Push A New Local Branch To A Remote Git Repository And Track It Too
Maybe your like
- Create a new branch: git checkout -b feature_branch_name
- Edit, add and commit your files.
- Push your branch to the remote repository: git push -u origin feature_branch_name
It’s as simple as that!
What’s going on here? Git Branch explained in more detailGit Branch
Git’s branching functionality lets you create new branches of a project to test ideas, isolate new features, or experiment without impacting the main project.
View Branches
To view the branches in a Git repository, run the command:
git branchTo view both remote-tracking branches and local branches, run the command:
git branch -aThere will be an asterisk (*) next to the branch that you’re currently on.
There are a number of different options you can include with git branch to see different information. For more details about the branches, you can use the -v (or -vv , or --verbose ) option. The list of branches will include the SHA-1 value and commit subject line for the HEAD of each branch next to its name.
You can use the -a (or --all ) option to show the local branches as well as any remote branches for a repository. If you only want to see the remote branches, use the -r (or --remotes ) option.
Checkout a Branch
To checkout an existing branch, run the command:
git checkout BRANCH-NAMEGenerally, Git won’t let you checkout another branch unless your working directory is clean, because you would lose any working directory changes that aren’t committed. You have three options to handle your changes:
- trash them (see Git checkout for details) or
- commit them (see Git commit for details) or
- stash them (see Git stash for details).
Create a New Branch
To create a new branch, run the command:
git branch NEW-BRANCH-NAMENote that this command only creates the new branch. You’ll need to run git checkout NEW-BRANCH-NAME to switch to it.
There’s a shortcut to create and checkout a new branch at once. You can pass the -b option (for branch) with git checkout . The following commands do the same thing:
# Two-step method git branch NEW-BRANCH-NAME git checkout NEW-BRANCH-NAME # Shortcut git checkout -b NEW-BRANCH-NAMEWhen you create a new branch, it will include all commits from the parent branch. The parent branch is the branch you’re on when you create the new branch.
Rename a Branch
To rename a branch, run the command:
git branch -m OLD-BRANCH-NAME NEW-BRANCH-NAME # Alternative git branch --move OLD-BRANCH-NAME NEW-BRANCH-NAMEDelete a Branch
Git won’t let you delete a branch that you’re currently on. You first need to checkout a different branch, then run the command:
git branch -d BRANCH-TO-DELETE # Alternative: git branch --delete BRANCH-TO-DELETEThe branch that you switch to makes a difference. Git will throw an error if the changes in the branch you’re trying to delete are not fully merged into the current branch. You can override this and force Git to delete the branch with the -D option (note the capital letter) or using the --force option with -d or --delete :
git branch -D BRANCH-TO-DELETE # Alternatives git branch -d --force BRANCH-TO-DELETE git branch --delete --force BRANCH-TO-DELETECompare Branches
You can compare branches with the git diff command:
git diff FIRST-BRANCH..SECOND-BRANCHYou’ll see colored output for the changes between branches. For all lines that have changed, the SECOND-BRANCH version will be a green line starting with a “+”, and the FIRST-BRANCH version will be a red line starting with a “-”. If you don’t want Git to display two lines for each change, you can use the --color-words option. Instead, Git will show one line with deleted text in red, and added text in green.
If you want to see a list of all the branches that are completely merged into your current branch (in other words, your current branch includes all the changes of the other branches that are listed), run the command git branch --merged .
Update a Branch from Remote
To update a local branch from remote:
git stash (optional, to save local changes which differs from the remote repository if any)If you weren’t already on the branch you want to work on:
git checkout my_local_branchFinally pull from the remote branch
git pullTrack a Remote Branch
If you already have a branch and you want to track a remote branch, then you use set-upstream-to command:
git branch --set-upstream-to origin/BRANCHOr you can use the -u flag (upstream) when you make your first push:
git push -u origin BRANCHHelp with Git Branch
If you forget how to use an option, or want to explore other functionality around the git branch command, you can run any of these commands:
git help branch git branch --help man git-branchTag » Add Branch Git Remote
-
Git Add Remote Branch - Stack Overflow
-
Remote Branches - Git
-
Git-remote Documentation - Git
-
How To Create A Remote Branch In Git | Learn Version Control With Git
-
How To Push Git Branch To Remote - Devconnected
-
How To Push New Git Branches To Remote Repos On GitHub Or GitLab ...
-
How To Create A Remote Branch In Git - W3docs
-
Git Checkout | Atlassian Git Tutorial
-
How To Add A New Remote To Your Git Repo - Assembla Help Center
-
How To Fetch A Remote Branch Using Git | LoginRadius Blog
-
Easily Perform Git Checkout Remote Branch [Step-by-Step]
-
Git Branches: List, Create, Switch To, Merge, Push, & Delete
-
Creating A Branch In Remote Git Repository - TecAdmin
-
Create And Push A Branch To A Remote Git Repository - Techie Delight