Search code examples
gitgit-push

Why does "git push" work after doing "git push origin --delete BRANCHNAME"?


Sometimes I want to delete the remote branch and then push again,if for instance I have amended a commit locally which had already been pushed. (don't worry, I only do this if I am the only one working on that branch).

After I do "git push origin --delete BRANCHNAME", I can simply follow that with a "git push".

If the branch has been deleted, why does "git push" actually work?
Why do I not need to do a "git push -u origin BRANCHNAME" again?


Solution

  • To illustrate larsks' comment, here is a repo with a local branch tracking an upstream branch:

    C:\Users\vonc\prog\b2dpres>git br -avv
    * prez                  13a1c36 [origin/prez] 20150923
    

    Pushing the deletion of the branch:

    C:\Users\vonc\prog\b2dpres>git push origin --delete prez
    To https://VonC@github.com/VonC/b2d.git
     - [deleted]         prez
    

    That doesn't change the local config:

    C:\Users\vonc\prog\b2dpres>git config --local --get-regexp branch.prez
    branch.prez.remote origin
    branch.prez.merge refs/heads/prez
    

    Nor does it delete the local branch:

    C:\Users\vonc\prog\b2dpres>git br -avv
    * prez                  13a1c36 [origin/prez: gone] 20150923
    

    (Note the "gone")

    A simple git push will know what to push (the local current branch prez) where (to origin) and to which branch (to its tracking branch origin/prez)

    C:\Users\vonc\prog\b2dpres>git push
    Counting objects: 10, done.
    Delta compression using up to 4 threads.
    Compressing objects: 100% (9/9), done.
    Writing objects: 100% (10/10), 763.97 KiB | 0 bytes/s, done.
    Total 10 (delta 2), reused 1 (delta 0)
    To https://VonC@github.com/VonC/b2d.git
     * [new branch]      prez -> prez
    

    It recreates the branch on the remote side.