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.

  • git push >> fatal: no configured push destination
  • Git push branch from one remote to another?
  • Windows GUIs for git
  • git pull with “--work-tree” flag fails
  • Merge and delete branch in one step/command
  • add #*# glob to .gitignore?
  • 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.

  • Repository deployment and Composer : what workflow?
  • git bash : how to check if there's a new commit available
  • Is it possible to view git diffs using a GUI side-by-side tool on Mac?
  • Is the Heroku Git Repo Public?
  • Git library for Ruby or Python?
  • Best Git strategy for a project on multiple platforms
  • 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 origin
    git config 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

    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.