博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
拜托了
阅读量:2524 次
发布时间:2019-05-11

本文共 3892 字,大约阅读时间需要 12 分钟。

by Buddy Reno

由Buddy Reno

Git Please :如何在不做蠢事的情况下强行推动 (Git Please: how to force push without being a jerk)

As the size of a dev team grows, so does the likelihood of someone doing a force push and overwriting someone else’s code.

随着开发团队规模的扩大,有人进行强制推送并覆盖他人代码的可能性也在增加。

Here’s what a force push looks like in Git:

这是在Git中的强制推送的样子:

$ git push --force origin master# `--force` can also  be written as `-f`

This command can cause all kinds of problems. It basically tells Git that I don’t care what is in origin/master. What I have is correct. Overwrite it.

此命令可能会导致各种问题。 它基本上告诉Git 我不在乎源/母版是什么。 我所拥有的是正确的。 覆盖它。

So what happens if a co-worker had changes committed to a branch that you haven’t pulled down into your own repo? It gets overwritten, and your co-worker potentially has to re-do their work (or resurrect a commit or two if they still have it locally).

那么,如果同事将更改提交到您尚未拉入自己的存储库的分支中,该怎么办? 它会被覆盖,您的同事可能必须重新做他们的工作(或者如果他们本地仍然有一个提交,则复活一两个提交)。

But this whole mess can be easily avoided with a small change to how you use theforceflag. Instead of using —-force, Use --force-with-lease.

但是,只需对使用force flag的方式进行少量更改,就可以轻松避免整个混乱情况。 而不是使用—-force ,而是使用--force-with-lease

$ git push --force-with-lease origin master

To summarize Git’s , using force-with-lease tells git to check whether the remote repo is the same as the one you’re trying to push up. If it isn’t, git will throw an error instead of blindly overwriting the remote repo. This will save you from accidentally overwriting work that you don’t intend to.

总结一下Git的 ,使用force-with-lease告诉git检查远程仓库是否与您要推送的仓库相同。 如果不是,git会抛出一个错误,而不是盲目地覆盖远程仓库。 这样可以避免意外覆盖您不想要的工作。

I hate typing force-with-lease though — especially because I’m used to typing the shorthand -f for force pushing. Thankfully, Git allows you to add aliases to make this quicker. I like to think that I’m asking Git if it’s okay to force push, so I’ve aliased push —force-with-lease to git please.

我不喜欢键入force-with-lease —尤其是因为我习惯于键入简写-f进行强制推送。 幸运的是,Git允许您添加别名以使其更快。 我想以为我是在问Git是否可以强制执行推送,因此我push —force-with-lease别名为git please

$ git please origin master

You can add an alias in git by typing this into your terminal:

您可以通过在终端中输入以下别名在git中添加别名:

git config --global alias.please 'push --force-with-lease'

Or you can open up your ~/.gitconfig file and manually add the alias:

或者,您可以打开~/.gitconfig文件并手动添加别名:

[alias]	co = checkout	ci = commit	please = push --force-with-lease

总是有一个警告... (There’s always a caveat…)

It’s possible to trick force with lease however. When you use git pull to get updates from the origin, this is doing two commands at once. Git runs a fetch to pull down the reference to all the changes. Then it runs a merge to merge the changes you just fetched into your current branch.

但是,可以通过租赁来欺骗力量。 当您使用git pull从源获取更新时,这会同时执行两个命令。 Git运行fetch以拉低所有更改的引用。 然后,它运行merge以将您刚获取的更改merge到当前分支中。

If you only do a fetch to get the latest updates, you’ll only be updating your references — not actually merging the changes into your working copy. Then, if you force push with lease, Git will look at those references and think that the local copy is up to date with the remote, when in reality it isn’t yet. This will trick Git into overwriting the changes on the remote with your local copy, without having the changes actually merged in.

如果您仅fetch最新的更新,则只会更新参考文献,而实际上并未将更改合并到工作副本中。 然后,如果您强制使用租用推送,则Git将查看这些引用,并认为本地副本与远程设备是最新的,而实际上还不是。 这将使Git欺骗您使用本地副本覆盖远程上的更改,而无需真正合并更改。

The easiest way to avoid this problem is always use git pull to fetch and merge at the same time. I’ve not run into any instances where I’ve needed to fetch and merge manually, so I can’t speak to those circumstances. Using pull has always worked for me.

避免此问题的最简单方法是始终使用git pull来同时fetchmerge 。 我没有遇到需要手动fetchmerge任何实例,因此我无法对这些情况进行讨论。 使用pull一直对我有用。

I hope you find git please helpful and that, as a result, you never have to recover from a force-push nightmare.

我希望您对git please有所帮助,因此,您不必从强行噩梦中恢复过来。

翻译自:

转载地址:http://epgwd.baihongyu.com/

你可能感兴趣的文章
JAVA基础2——类初始化相关执行顺序
查看>>
转:Zend Framework 重定向方法(render, forward, redirect)
查看>>
Linux下查看磁盘与目录的容量——df、du
查看>>
关于日记app的思考
查看>>
使用sencha的cmd创建项目时提示找不到\Sencha\Cmd\repo\.sencha\codegen.json
查看>>
如何快速启动一个Java Web编程框架
查看>>
MSP430单片机存储器结构总结
查看>>
文本框过滤特殊符号
查看>>
教育行业安全无线网络解决方案
查看>>
7个杀手级的开源监测工具
查看>>
软件架构学习小结
查看>>
C语言实现UrlEncode编码/UrlDecode解码
查看>>
返回用户提交的图像工具类
查看>>
树链剖分 BZOJ3589 动态树
查看>>
挑战程序设计竞赛 P131 区间DP
查看>>
【例9.9】最长公共子序列
查看>>
NSFileManager打印目录下的文件的函数
查看>>
Selenium自动化-调用Mysql数据库
查看>>
项目一
查看>>
[转载]AAF灵便应用框架简介系列(6):休息一下,泛谈面向对象 Why OO+多层结构?...
查看>>