如何推送到非裸 Git 存储库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1764380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to push to a non-bare Git repository?
提问by woky
I usually work on a remote server via ssh (screen and vim), where I have a Git repository. Sometimes I'm not online, so I have a separate repository (cloned from my remote) on my laptop.
我通常通过 ssh(屏幕和 vim)在远程服务器上工作,在那里我有一个 Git 存储库。有时我不在线,所以我的笔记本电脑上有一个单独的存储库(从我的遥控器克隆)。
However, I can't pull from this repository on remote side because I'm usually behind a firewall or I don't have a public IP.
但是,我无法从远程端的这个存储库中提取,因为我通常在防火墙后面或者我没有公共 IP。
I've read that I should push just to a bare repository. How should I then push my changes to my remote repository?
我读过我应该只推送到一个裸存储库。然后我应该如何将我的更改推送到我的远程存储库?
回答by Dan Moulding
Best Option
最佳选择
Probably the cleanest, least confusing, and safest way to push into your non-bare remote repository, is to push to dedicated branches in the remote that represent your laptop branches.
推送到非裸远程存储库的最干净、最简单、最安全的方法可能是推送到远程中代表笔记本电脑分支的专用分支。
Let's look at the simplest case, and assume you have just one branch in each repo: master. When you push to the remote repo from your laptop, instead of pushing master -> master, push master -> laptop-master (or a similar name). This way the push doesn't affect the currently checked-out master branch in the remote repo. To do this from the laptop, the command is pretty simple:
让我们看一个最简单的情况,假设您在每个 repo 中只有一个分支:master。当您从笔记本电脑推送到远程存储库时,不是推送 master -> master,而是推送 master ->laptop-master(或类似名称)。这样推送不会影响远程仓库中当前检出的主分支。要从笔记本电脑执行此操作,命令非常简单:
git push origin master:laptop-master
This means that the local master branch will be pushed to the branch named "laptop-master" in the remote repository. In your remote repo, you'll have a new branch named "laptop-master" that you can then merge into your remote master when you are ready.
这意味着本地 master 分支将被推送到远程存储库中名为“laptop-master”的分支。在您的远程仓库中,您将拥有一个名为“laptop-master”的新分支,当您准备好时,您可以将其合并到您的远程主机中。
Alternate Option
替代选项
It's also possible to just push master -> master, but pushing to the currently checked-out branch of a non-bare repo is generally not recommended, because it can be confusing if you don't understand what is going on. This is because pushing to a checked-out branch doesn't update the work tree, so checking git status
in the checked-out branch that was pushed into will show exactly the opposite differences as what was most recently pushed. It would get especially confusing if the work tree was dirty before the push was done, which is a big reason why this is not recommended.
也可以只推送 master -> master,但通常不建议推送到非裸仓库的当前检出分支,因为如果您不明白发生了什么,这可能会令人困惑。这是因为推送到检出分支不会更新工作树,因此检git status
入推入的检出分支将显示与最近推送的完全相反的差异。如果在推送完成之前工作树是脏的,它会变得特别混乱,这是不推荐这样做的一个重要原因。
If you want to try just pushing master -> master, then the command is just:
如果您只想尝试推送 master -> master,那么命令就是:
git push origin
But when you go back to the remote repo, you'll most likely want to do a git reset --hard HEAD
to get the work tree in sync with the content that was pushed. This can be dangerous, because if there are any uncommittedchanges in the remote work tree that you wanted to keep it will wipe them out. Be sure you know what the consequences of this are before you try it, or at least make a backup first!
但是当您返回远程存储库时,您很可能希望执行 agit reset --hard HEAD
以使工作树与推送的内容同步。这可能很危险,因为如果您想要保留的远程工作树中有任何未提交的更改,它将清除它们。在尝试之前,请确保您知道这样做的后果,或者至少先进行备份!
EDITSince Git 2.3, you can use "push-to-deploy" git push: https://github.com/blog/1957-git-2-3-has-been-released. But pushing to a separate branch and then merging is usually better since it does an actual merge (hence works with uncommitted changes just like merge does).
编辑从 Git 2.3 开始,您可以使用“推送到部署”git push:https: //github.com/blog/1957-git-2-3-has-been-released。但是推送到单独的分支然后合并通常会更好,因为它进行了实际合并(因此可以像合并一样处理未提交的更改)。
回答by Manish
I would suggest to have a bare-repository and a local working (non-bare) repos in your server. You could push changes from laptop to server bare repo and then pull from that bare repo to server working repo. The reason I say this is because you might have many complete/incomplete branches in server which you will want to replicate on the laptop.
我建议在您的服务器中有一个裸存储库和一个本地工作(非裸)存储库。您可以将更改从笔记本电脑推送到服务器裸仓库,然后从该裸仓库拉到服务器工作仓库。我这么说的原因是因为您可能在服务器中有许多完整/不完整的分支,您希望在笔记本电脑上复制这些分支。
This way you don't have to worry about the state of the branch checked out on server working repo while pushing changes to server.
这样您就不必担心在将更改推送到服务器时在服务器工作存储库上检出的分支状态。
回答by Todd Freed
Another option is to setup a reverse ssh tunnel so that you can pull instead of push.
另一种选择是设置反向 ssh 隧道,以便您可以拉而不是推。
# start the tunnel from the natted box you wish to pull from (local)
$ ssh -R 1234:localhost:22 user@remote
# on the other box (remote)
$ git remote add other-side ssh://user@localhost:1234/the/repo
$ git pull other-side
And if you want the tunnel to run in the background
如果你想让隧道在后台运行
$ ssh -fNnR 1234:localhost:22 user@remote
回答by vidwan reddy
You can do:
你可以做:
$git config --bool core.bare true
$git config --bool core.bare true
this can be done in bare or central repository so that it will accept any files that are pushed from non bare repositories. If you do this in non bare repository then we cannot push any files from non bare to bare repository.
这可以在裸存储库或中央存储库中完成,以便它接受从非裸存储库推送的任何文件。如果您在非裸存储库中执行此操作,则我们无法将任何文件从非裸存储库推送到裸存储库。
If you are practicing GIT by creating central and non bare repo in PC it might not show the pushed files in some PC's but it has been pushed. you can check it by running.
如果您通过在 PC 中创建中央和非裸仓库来练习 GIT,它可能不会在某些 PC 中显示推送的文件,但它已被推送。你可以通过运行来检查它。
$git log
in central repo.
$git log
在中央回购。
Other than if you push to GitHub it will show the files there.
除了推送到 GitHub 之外,它还会在那里显示文件。