Git install tracking branch offline
git push -u origin branch
links the branch to the origin / branch, so that subsequent pushes from the branch can be just git push
as far as I understand it.
Can I set up this type of tracking for a new repo (origin / branch doesn't exist yet) offline? I want to make it so that all subsequent pushes from the branch go to the start / branch without having to specify when I go online.
source to share
I use xyz
as branch name and keep origin
as remote name to avoid confusion. You already mentioned the online method for pushing and installing upstream simultaneously.
git push --set-upstream origin xyz
Get online, set up upstream offline
The preferred way would be to get the branch before going offline and with the option --set-upstream-to
git branch
to set the upstream without pushing.
git branch --set-upstream-to origin/xyz
This results in the following lines being added to .git/config
, which is also the result of the online method described earlier.
[branch "xyz"]
remote = origin
merge = refs/heads/xyz
Completely autonomous approach
If you don't want to get a remote branch first, you can edit .git/config
manually or use a tool git config
to do the same.
git config branch.xyz.remote origin
git config branch.xyz.merge refs/heads/xyz
When you fetch a remote branch, you should achieve the same result as --set-upstream-to
with the remote branch already checked out.
Alternative standalone approach
Instead, you can simulate an offline checkout by setting the remote branch binding to a specific commit, eg. to the same commit as your local branch.
git update-ref refs/remotes/origin/xyz xyz
git branch --set-upstream-to origin/xyz
The idea of ββdirectly updating the ref is borrowed from Decave's answer. Use with care and read git-fetch(1)
notes on fast forward / no forward.
source to share
Okay, let him hack himself git
.
The place where it git
stores links to remote branches is in .git/refs/remotes/<remote_repo>
. Let's say we just created a new repo and added a remote source. Then we go offline and create a new branch named new_branch
.
If you look in .git/refs/remotes/origin
, you should see:
decave@demo~$ls /home/decave/demo/.git/refs/remotes/origin
HEAD
There are no branches; which is to be expected, given that we haven't pushed or pulled branches from the remote repo. Now let's say new_branch
pointed to a commit with a hash e81d5ea59b5f7ab81de4662f7dca5de86e230d92
. If we add a file in .git/refs/remotes/origin
under the name new_branch
with its contents set to e81d5ea59b5f7ab81de4662f7dca5de86e230d92
, then this is the same as telling your local repo "hey new_branch
is the branch by origin that points to commit e81d5ea59b5f7ab81de4662f7dca5de86e230d92
". This can be done using 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 be configured successfully git
to do what you want.
Note that it is quite a terrible practice to start crawling your own repository this way. The correct way to do this is to just use a flag -u
as you said.
source to share