git - git可以推送到远程仓库的当前分支吗?

标签 git

来自 https://stackoverflow.com/a/34535193/156458

When you push to a checked out branch of a remote repository, you usually get a warning and Git will not allow you to do so.

但是我得到一个暗示,git 可以推送到远程存储库的当前分支,来自 Loeliger,2ed 的 Version Control with Git,尤其是粗体文本:

The push operation can update the repository state, including the HEAD commit. That is, even though the developer at the remote end has done nothing, the branch refs and HEAD might change, becoming out of sync with the checked out files and index.

A developer who is actively working in a repository into which an asynchronous push happens will not see the push. But a subsequent commit by that developer will occur on an unexpected HEAD, creating an odd history. A forced push will lose pushed commits from the other developer. The developer at that repository also may find herself unable to reconcile her history with either an upstream repository or a downstream clone because they are no longer simple fast-forwards as they should be. And she won’t know why: the repository has silently changed out from underneath her. Cats and dogs will live together. It’ll be bad.

As a result, you are encouraged to push only into a bare repository. This is not a hard- and-fast rule, but it’s a good guide to the average developer and is considered a best practice. There are a few instances and use cases where you might want to push into a development repository, but you should fully understand its implications. When you do want to push into a development repository, you may want to follow one of two basic approaches.

In the first scenario, you really do want to have a working directory with a branch checked out in the receiving repository. You may know, for example, that no other developer will ever be doing active development there and therefore there is no one who might be blind sided by silent changes being pushed into his repository.

In this case, you may want to enable a hook in the receiving repository to perform a checkout of some branch, perhaps the one just pushed, into the working directory as well. To verify that the receiving repository is in a sane state prior to having an automatic checkout, the hook should ensure that the nonbare repository’s working directory contains no edits or modified files and that its index has no files in the staged but uncommitted state when the push happens. When these conditions are not met, you run the risk of losing those edits or changes as the checkout overwrites them.

There is another scenario where pushing into a nonbare repository can work reasonably well. By agreement, each developer who pushes changes must push to a non–checked out branch that is considered simply a receiving branch. A developer never pushes to a branch that is expected to be checked out. It is up to some developer in particular to manage what branch is checked out and when. Perhaps that person is responsible for handling the receiving branches and merging them into a master branch before it is checked out.

是不是暗示git可以push到远程仓库的当前分支? (我的猜测是肯定的,但我不确定)

最后一段(推荐使用钩子(Hook)检查推送更新的分支的段落)是否假设要推送到的分支不是远程存储库中的当前分支? (我的想法是检查要推送的分支意味着要推送的分支不是当前分支,但最后一段指出推送到非检查分支的“不同场景”,这对我来说意味着上一段是关于推送到 checkout 的分支,即当前分支)

最佳答案

从 Git 2.3+ 开始,您可以配置接收端以“在接收存储库中 checkout 一个分支的工作目录”。

特别是:"push-to-checkout" (git 2.4, May 2015) ,改进了 "push-to-deploy" (Git 2.3, February 2015) .

有关具体示例,请参见“Deploy a project using Git push”。

  • 首先:推送部署(git 2.3):

you can push changes directly to the repository on your server. Provided no local modifications have been made on the server, any changes to the server's current branch will be checked out automatically. Instant deploy!

To use this feature, you have to first enable it in the Git repository on your server by running

$ git config receive.denyCurrentBranch updateInstead
  • 然后 push-to-checkout hook (git 2.4)

There is now a push-to-checkout hook, which can be installed on the server to customize exactly what happens when a user pushes to the checked-out branch.
For example, by default such a push fails if there have been any changes to the working tree on the server. The push-to-checkout hook could instead try to merge any server-side edits with the new branch contents, or it could unconditionally overwrite any local changes with a pristine copy of the pushed branch contents.

简而言之,您可以直接推送到 checkout 的分支。
参见 commit 4d7a5ce注意事项:

  • A change only to the working tree but not to the index is still a change to be protected;

  • An untracked file in the working tree that would be overwritten by a push-to-deploy needs to be protected;

  • A change that happens to make a file identical to what is being pushed is still a change to be protected (i.e. the feature's cleanliness requirement is more strict than that of checkout).

关于git - git可以推送到远程仓库的当前分支吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34574166/

相关文章:

git - 有没有git管理GUI?

git - Cygwin 的 Git 命令正在从命令中删除大括号。我该如何防止这种情况?

git - TFS、Windows 身份验证和命令行 git (msysgit)

git - 将已解决的 git merge 复制到另一个 merge

javascript - 使 rails 中的 javascript 库保持最新的最佳方法是什么?

git - git:如何以编程方式确定是否需要 pull 或 push ?

GIT:在开发服务器上还是在客户端上开发?

git - 删除/切断 Git 的修订/提交历史

python - 将 Django 项目从 virtualenv 推送到 github

Git - 远程 : fatal: You are on a branch yet to be born