Git set tracking branch while offline

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.

  • Keep Git Reflog Indefinitely?
  • Does git-fetch copy down the entire repository?
  • Git not ignoring certain Xcode files in .gitignore
  • Push new branch to google.com git repository
  • TeamCity to GitHub Connection Error: session is down
  • Best way to avoid stale *.pyc files?
  • 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.

  • git cherry-pick branchless commit on remote
  • git-p4 submission failure
  • How to find git log messages with more than one line?
  • git - removing rails development log
  • svn to git conversion
  • Groovy bootstrap error using GIT BASH on Windows 7
  • 2 Solutions collect form web for “Git set tracking branch while offline”

    I’ll use xyz as the name of the branch and keep origin as the name of the remote to avoid confusion. You already mentioned the online method to push and set upstream at the same time..

    git push --set-upstream origin xyz
    

    Fetch online, set upstream offline

    The preferred way would be to fetch the branch before going offline and using --set-upstream-to option of git branch to set upstream without pushing.

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

    That results in adding the following lines to .git/config which is also the result of the online way described before.

    [branch "xyz"]
        remote = origin
        merge = refs/heads/xyz
    

    Fully offline approach

    When you don’t want to fetch the remote branch first, you can either edit .git/config by hand or use the git config facility to perform the same action.

    git config branch.xyz.remote origin
    git config branch.xyz.merge refs/heads/xyz
    

    When you then fetch the remote branch, you should have reached the same result as with --set-upstream-to with remote branch already fetched.

    Alternative offline approach

    You can instead mimic the fetch offline by setting the remote branch ref to a specific commit, e.g. 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 direct ref updating is borrowed from Decave’s answer. Use with care and read git-fetch(1) notes about fast-forward/non-fast-forward fetches.

    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.

    Git Baby is a git and github fan, let's start git clone.