简体   繁体   English

强制推送当前分支

[英]Force push current branch

I often rebase feature branches and then want to force push them to the server.我经常 rebase 功能分支,然后想强制将它们推送到服务器。

git push --force origin feature-mongodb-support

Is there any shortcut for git push --force origin <current branch> ? git push --force origin <current branch>有捷径吗?

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.并且只使用git fpush将你宝贵的工作推送到上游。

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.如果您需要经常这样做,那么您的工作流程肯定有问题

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:在阅读了这些答案并阅读了相关问题的答案( https://stackoverflow.com/a/18782415/586 )之后,我创建了这个别名以根据当前分支名称强制推送到origin

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

You can change the default behaviour by setting the push.default property:您可以通过设置push.default属性来更改默认行为:

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 :这是来自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.定义 git push 在命令行上没有给出 refspec 时应该采取的操作,远程中没有配置 refspec,并且命令行上给出的任何选项都没有暗示 refspec。 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. upstream - 将当前分支推送到其上游分支。

  • tracking - deprecated synonym for upstream. tracking - upstream 的弃用同义词。

  • current - push the current branch to a branch of the same name. current - 将当前分支推送到同名分支。

If you use oh my zsh you can simply do如果你使用oh my zsh你可以简单地做

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 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.这将使您可以使用git fpush作为更短的选择。

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 ) (基于另一个 SO 答案

The current branch name can also be inferred automatically.当前分支名称也可以自动推断。

I use a small shell script to do the force push to the current branch:我使用一个小的 shell 脚本来强制推送到当前分支:

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.命令git branch*符号标记当前分支,因此我们可以用grep '*'获取相关行并解析它以获取分支名称。

Then the shortcut command can be defined as an alias in .bashrc or .zshrc :然后可以将快捷命令定义为.bashrc.zshrc中的别名:

alias gfp=/path/to/script

Update.更新。

Since Git 2.22 (see this answer: stackoverflow.com/a/6245587/1326411 ) it can be simplified to:自 Git 2.22(参见此答案:stackoverflow.com/a/6245587/1326411)起,它可以简化为:

alias git-fp='git branch --show-current | xargs -I % git push origin % --force-with-lease'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM