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 theforce
flag. 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 documentation, 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
來同時fetch
和merge
。 我沒有遇到需要手動fetch
和merge
任何實例,因此我無法對這些情況進行討論。 使用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
有所幫助,因此,您不必從強行噩夢中恢復過來。
翻譯自: https://www.freecodecamp.org/news/git-please-a182f28efeb5/