Domanda

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>?

È stato utile?

Soluzione

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.

Altri suggerimenti

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)\""

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.

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

This should do the trick:

git alias fpush "push --force origin"

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

To automatically force-push to the branch that is tracked (regardless of its name and upstream), I've devised this alias:

fbrpush=!git push $(git rev-parse --abbrev-ref=loose --symbolic-full-name @{upstream} \
                    | sed 's:/: +:')

(line is broken for readability)

(based on another SO answer)

The current branch name can also be inferred automatically.

I use a small shell script to do the force push to the current branch:

git branch | grep '*' | awk '{print $2}' | xargs -I % git push origin % -f

The command git branch tags the current branch with * symbol, so we can get the relevant line with grep '*' and parse it to get the branch name.

Then the shortcut command can be defined as an alias in .bashrc or .zshrc:

alias gfp=/path/to/script
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top