Force push current branch

59,311

Solution 1

You can use aliases to shorten the command. Use it like this:

git config --global alias.fpush "push --force origin"

Now to push your branch just type:

git fpush feature-mongodb-support

Or you can even hardcode the branch name into the command:

git alias fpush "push --force origin feature-mongodb-support"

and use only git fpush to push your precious work into the upstream.

However, non-fast-forward updates are dangerous since you will basically overwrite all the history on server that occurred between the last merge/rebase into your local branch and the forced push. If you need to do them often there is definitely something wrong in your workflow.

Solution 2

After reading these answers and reading this answer to a related question (https://stackoverflow.com/a/18782415/586), I created this alias to force push to origin based on the current branch name:

fp = "!git push -f origin \"$(git rev-parse --abbrev-ref HEAD)\""

Solution 3

You can change the default behaviour by setting the push.default property :

git config --global push.default current

then:

git push -f

will force a push to you current branch.

Here is a copy/paste from http://schacon.github.io/git/git-config.html:

push.default

Defines the action git push should take if no refspec is given on the command line, no refspec is configured in the remote, and no refspec is implied by any of the options given on the command line. Possible values are:

  • nothing - do not push anything.

  • matching - push all matching branches. All branches having the same name in both ends are considered to be matching. This is the default.

  • upstream - push the current branch to its upstream branch.

  • tracking - deprecated synonym for upstream.

  • current - push the current branch to a branch of the same name.

Solution 4

If you use oh my zsh you can simply do

ggfl

which will do this for you

git push --force-with-lease origin <your_argument>/$(current_branch)

https://github.com/robbyrussell/oh-my-zsh/wiki/Cheatsheet

Solution 5

This should do the trick:

git alias fpush "push --force origin"

Which will let you use git fpush as a shorter alternative.

Share:
59,311

Related videos on Youtube

iblue
Author by

iblue

casting the runes One of ICL's most talented systems designers used to be called out occasionally to service machines which the field circus had given up on. Since he knew the design inside out, he could often find faults simply by listening to a quick outline of the symptoms. He used to play on this by going to some site where the field circus had just spent the last two weeks solid trying to find a fault, and spreading a diagram of the system out on a table top. He'd then shake some chicken bones and cast them over the diagram, peer at the bones intently for a minute, and then tell them that a certain module needed replacing. The system would start working again immediately upon the replacement. – The Jargon File, v4.4.7

Updated on August 19, 2022

Comments

  • iblue
    iblue over 1 year

    I often rebase feature branches and then want to force push them to the server.

    git push --force origin feature-mongodb-support
    

    Is there any shortcut for git push --force origin <current branch>?

    • mpontillo
      mpontillo almost 12 years
      I always set up the config/tracking such that git push alone pushes to the default remote tracking branch. If you can do that, you could get down to git push -f? (just curious: what's your backup plan in this workflow if you push a bad rebase? I assume you're the only one working on these feature branches?)
    • AndreKR
      AndreKR over 9 years
      Isn't this question specifically about how to get rid of the current branch name in the often used git push -f origin <current branch name> construct? Then the accepted answer completely misses the point and @Mike's comment should be the accepted answer.
    • orad
      orad over 8 years
      I think the best answer is to set it as default: git config --global push.default current (from here).
  • iblue
    iblue almost 12 years
    I am the only one working on a feature branch. I just want to keep the feature branche fast-forwardable against the master branch to make my life easier. Is there anything wrong with that?
  • Sergey K.
    Sergey K. almost 12 years
    Yep, it is risky. You will overwrite your branch with just one command. A better point will be to interactively rebase your feature-branch on top of the master branch once the feature is done. You will still have a clean history in your master and there will be no risk regarding all these overwrites.
  • iblue
    iblue almost 12 years
    I do this, because I need to incorporate new changes from the master branch to test if the new feature still works with the new master code. Where's the risk?
  • Sergey K.
    Sergey K. almost 12 years
    The risk is on overwriting remote branch (together with its history) with some buggy code and loosing your previous work. If this is of no concern to you - just use the solution from the answer.
  • iblue
    iblue almost 12 years
    Oh, sure. But if I just do a git rebase master in the feature branch, then nothing bad can happen. Am I right?
  • Sergey K.
    Sergey K. almost 12 years
    It can. Depends on the actual changes in master. So it is up to you to decide whether the risk is acceptable :)
  • Kzqai
    Kzqai almost 11 years
    The git rebase master at least allows you to back out if the rebase doesn't happen cleanly (via git rebase --abort)
  • user1021726
    user1021726 about 9 years
    How would this work if the remote branch (that we push to) does not have the same name as the local branch?
  • jlleblanc
    jlleblanc about 9 years
    @user1021726 it doesn't.
  • beporter
    beporter over 8 years
    If you follow this advice and include the remote name (typically origin) in your git alias, you may have problems with bash [tab] completion still trying to add the remote instead of skipping straight to offering branch names. To fix this, define your own custom replacement function in ~/.profile after sourcing bash_completion: _git_fpush() { _git_branch; } (where "fpush" matches the name of your git alias.) This will cause bash to treat your alias the same as the branch command.
  • Damien
    Damien over 6 years
    This is the answer that cover the whole question... with the limitation that the local branch and remote branch need to have the same name.
  • Ben Bucksch
    Ben Bucksch over 2 years
    You say frequent force pushes are wrong. There are 2 ways to update long-living branches: merge and rebase. When you choose the rebase method, you need to force push. How else would you do that, if the requirement is to have a clean, straight history on the branch?
  • Ben Bucksch
    Ben Bucksch over 2 years
    Works beautifully, and does exactly what the OP asked. Thank you!
  • Ben Bucksch
    Ben Bucksch over 2 years
    Unless I'm mistaken, this pushes all branches which happen to have local changes, including potentially master, or completely unrelated branches, not just the current branch. Combined with the force push, this is extremely dangerous.
  • Sergey K.
    Sergey K. over 2 years
    @BenBucksch imagine you have 10 developers working on this branch and between your rebase and forced push someone else has pushed his payload to that branch. The result will be you just overwrite their work with your forced push. Keeping history streamlined this way is ok for long-lasting private branches, and here by private I mean situations when there's only a few developers on that branch. For branches where tens of developers are collaborating, forced pushes will wreck havoc.
  • Ben Bucksch
    Ben Bucksch over 2 years
    Agreed, force push works only when there's a single developer. (Or manual coordination, which is to be avoided.)
  • Krakowski mudrac
    Krakowski mudrac about 2 years
    there are easier ways: stackoverflow.com/a/6245587/1326411
  • Vladimir Vagaytsev
    Vladimir Vagaytsev about 2 years
    Thank you so much! That looks much more elegant :)