Git branch tracking and remote management¶
Git branch tracking and remote management involves configuring the relationship between local branches and remote repositories, as well as handling the synchronization of code between them.^[600-developer__tools__git__git-command.md]
Branch Tracking¶
Tracking branches create a direct relationship between a local branch and a remote branch, allowing Git to know which specific branch to push to or pull from by default.
Establishing Tracking¶
When creating a new branch or modifying an existing one, you can set it to track a remote counterpart:
- Create with tracking: To create a new branch that tracks a remote branch immediately, use
git branch --track [branch-name] [remote-branch].^[600-developer__tools__git__git-command.md] - Set upstream for existing branch: To configure an existing branch to track a remote branch, use
git branch --set-upstream-to=origin/master master(replacingmasterwith the relevant branch names).^[600-developer__tools__git__git-command.md] - Older syntax: A legacy command for this purpose is
git branch --set-upstream [branch-name] origin/branch-name.^[600-developer__tools__git__git-command.md]
Removing Tracking¶
To stop a local branch from tracking its remote counterpart, use the --unset-upstream option:^[600-developer__tools__git__git-command.md]
git branch --unset-upstream [branch名]
To remove a remote tracking branch reference from your local configuration (e.g., origin/master), use the -r (remote) and -d (delete) flags:^[600-developer__tools__git__git-command.md]
git branch -d -r origin/master
Remote Management¶
Managing remotes involves defining the locations of external repositories and controlling the flow of data to and from them.
Adding Remotes¶
You can add a new remote repository URL to your local project using git remote add.^[600-developer__tools__git__git-command.md] A common convention is to name the central remote origin and the original upstream repository upstream.
git remote add upstream https://github.com/otheruser/repo.git
You can verify configured remotes using git remote -v.^[600-developer__tools__git__git-command.md]
Pulling and Pushing¶
Git allows granular control over how branches are downloaded and uploaded to remotes:
- Pull to specific local branch: You can pull content from a specific remote branch into a different local branch using the syntax
git pull [remote] [remote_branch]:[local_branch].^[600-developer__tools__git__git-command.md] - Push to specific remote branch: Similarly, you can push a local branch to a specifically named branch on the remote using
git push [remote] [local_branch]:[remote_branch].^[600-developer__tools__git__git-command.md]
Deleting Remote Branches¶
To delete a branch on the remote repository, you push "nothing" to it.^[600-developer__tools__git__git-command.md]
git push origin :branch_name_to_be_deleted
Related Concepts¶
Sources¶
^[600-developer__tools__git__git-command.md]