Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git set tracking branch while offline

Tags:

git

git push -u origin branch

binds branch with origin/branch so that subsequent pushes from branch can be just git push, as far as I understand.

Can I set up this kind of tracking for a brand new repo (origin/branch doesn't exist yet) while offline? I want to make it so all subsequent pushes from branch go to origin/branch without me having to specify that when I go online.

like image 782
PSkocik Avatar asked Oct 22 '25 13:10

PSkocik


1 Answers

Alright, let's hack ourselves some git.

The place where git stores references to remote branches is in .git/refs/remotes/<remote_repo>. Let's say that we had just created a new repo, and added remote as an origin. Then we go offline and create a new branch called new_branch.

If you take a look in .git/refs/remotes/origin you should see:

decave@demo~$ls /home/decave/demo/.git/refs/remotes/origin
HEAD

There are no branches in there; which is to be expected given that we haven't pushed or pulled any branches from the remote repo. Now, let's say new_branch was pointing at a commit with hash e81d5ea59b5f7ab81de4662f7dca5de86e230d92. If we add a file to .git/refs/remotes/origin called new_branch with its contents set to e81d5ea59b5f7ab81de4662f7dca5de86e230d92, then it would be equivalent to telling your local repo "hey, new_branch is a branch on origin that points to the commit e81d5ea59b5f7ab81de4662f7dca5de86e230d92". This can be accomplished with the git plumbing command git update-ref:

git update-ref refs/remotes/origin/new_branch e81d5ea59b5f7ab81de4662f7dca5de86e230d92

Then, if you run the command

git branch --set-upstream-to origin/new_branch

It should successfully configure git to do what you want.

Note that this it's pretty terrible practice to start plumbing around your own repository like this. The proper way to make this happen would just be to use the -u flag like you said.

like image 195
DIMMSum Avatar answered Oct 25 '25 09:10

DIMMSum



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!