Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
367 views
in Technique[技术] by (71.8m points)

git - The current branch does not track a remote branch

I must stress my usage of GIT (via VS2019 Team Explorer) has so far been fairly basic: 2 users, often working in sequence on the Master branch. Very happy to be suggested some reading for a deeper introduction to the concepts of GIT.

We wanted to start using GIT through a more business-like workflow. The idea is to keep the Master for core updates/upgrades, and to fork new branches based on past commits when publishing mini hot fixes: we pick the commit of the last publish, and create a new branch based on this commit; let's we call it HotFix. We do our fix, commit/publish/push, and then merge with the Master.

So we just started: Yesterday Mike created the HotFix branch, fixed a bug, committed, published the update, and pushed the commit to the remote. No merging with the Master was done yet. Today, Bob started Visual Studio, selected the HotFix branch, did not pull, and started to work on a second bug. Now he tries to push, but GIT is not happy and says:

Updates were rejected because the tip of your current branch is behind its remote counterpart. Integrate the remote changes before pushing again.

We understand this is because since he did not pull before starting to work, he is 1 commit behind Mike's/the Remote's version of the same branch. When this happens on the Master, we typically pull and merge/resolve any conflicts, and then push the work. But here, we cannot pull. There also is a message that says:

The current branch does not track a remote branch.

We are a bit confused, because VS documentation says you need to push the branch so that it tracks the remote, but we cannot push because we are 1 commit behind the remote and should pull first, which requires pushing.. is this a catch 22 situation?

I read here that $ git branch -u origin/dev will make "a tracking relationship for your current HEAD branch". I also feel that this post describe a similar scenario to ours; however the UX flow described in the most popular answer differs from what we have, I guess because we already have a remote repository, only the specific branch is problematic.

We understand the concepts of GIT and incremental versioning, but we know so little about its subtleties that I am scared of doing something irreversible. Could anyone confirm we are on the right track?

question from:https://stackoverflow.com/questions/66052848/the-current-branch-does-not-track-a-remote-branch

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Into the Branches panel of the Team Explorer, right click on your repository and choose "Open Command Prompt", then type:

git branch -u origin/HotFix

This will make the current local branch to track origin/HotFix.

Alternatively,

git branch -u origin/HotFix LocalName

Will make the LocalName branch to track origin/HotFix. Usually, it is recommended to have LocalName being the same name as the upstream branch it tracks (upstream refers to the branch on the server). As suggested in the comments, in the OP case this means git branch -u origin/HotFix HotFix.

Under the hood, tracking will result in Git updating locally a clone of the upstream branch. Then Git can compare the local branch with this local copy of the server branch.

It is then possible to Pull the commits of the upstream branch. If you have already committed on the branch, whilst some commits were pushed to the server by others, a merging operation is required before being able to push your own commits.

Some interesting reading: https://git-scm.com/book/en/v2/Git-Branching-Remote-Branches


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...