[英]how to undo git pull or git push?
我经常使用git,但是对引擎盖下的细节和“ git语言”的了解并不深。
我有两个目录:一个在我的电脑上,一个在我的笔记本电脑上。 两者都包含一个.git存储库(不是裸露的!)。
我的笔记本电脑通过ssh连接到我的电脑,然后从笔记本电脑推到我的笔记本电脑(添加了ssh遥控器)。
因为我无法推送到已签出的分支(在笔记本电脑上的PC上),所以我在PC上有两个分支(主机和Laptop-Master)。 因此,我先推送,然后在PC上合并分支。
在某种程度上,我搞砸了正确的顺序,而且我有冲突。
我正在尝试使用git log,git diff(和gitk)来清理内容。
为了撤消我曾经使用过的合并
git reset --merge
这个命令危险吗?
如何撤消对远程的拉动或推动?
我是否需要一个“中央”存储库来避免冲突(我认为这意味着一个裸存储库)?
为什么无法推送到已签出的远程服务器上的分支? 我可以用某种非危险的方式强迫它吗?
每个提示都很好...谢谢
要撤消对远程存储库的推送:
git push -f origin HEAD^:master
此命令将在一次提交时回退远程存储库。
要撤消拉动,可以执行以下操作:
git reset --hard {hash of the last commit before pull}
拥有一个中央的裸仓库将是有帮助的,但是...它只是另一个git仓库,您可以在其中进行推送和提取操作;
进行git reset --merge
并不危险。
一个git pull
可以“撤销”做一个git reset --hard
,以拉做之前提交。
通过git push
git reset
为您在本地存储库中实际想要的提交,然后通过执行git push -f
强制推送,可以“撤消” git push -f
中央存储库将是更标准的设置。
为什么无法推送到已签出的远程分支上?
因为通常通过某些外部存储库将更改推送到存储库中来将更改添加到存储库中会造成混乱。
我可以用某种非危险的方式强迫它吗?
您可以通过在该仓库中发出以下命令来配置git以允许它(据我所知您的PC仓库):
git config receive.denyCurrentBranch warn
然后,您可以将更改推送到检出的分支。 但是,当您切换到PC仓库时,您将处于混乱状态,因为这些更改不会反映在工作树中。 实际上,您看起来已经撤消了这些更改(因为它们在存储库中,而不在工作树中)。
您可以通过git reset --hard
获得推送的更改,但这会丢弃您拥有的所有本地更改。 如果您有本地更改,则必须使用git reset HEAD <file>
和git checkout -- <file>
手动还原每个推送的git checkout -- <file>
这是一个示例,其中我设置配置,切换到笔记本电脑,进行更改,推送更改,切换到pc并重置该存储库以在工作树中获取更改:
C:\git\pc_repo [master]> git config receive.denyCurrentBranch warn
C:\git\pc_repo [master]> cd ..
C:\git> cd .\laptop_repo
C:\git\laptop_repo [master]> echo hello >> text.txt
C:\git\laptop_repo [master]> git status
# On branch master
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: text.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
C:\git\laptop_repo [master +0 ~1 -0]> git commit -am "modified text"
[master 4925c26] modified text
1 file changed, 2 insertions(+), 1 deletion(-)
C:\git\laptop_repo [master]> git push
Counting objects: 5, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 303 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: warning: updating the current branch
To C:/git/.\pc_repo\.git
eb4f07f..4925c26 master -> master
C:\git\laptop_repo [master]> cd ..
C:\git> cd .\pc_repo
C:\git\pc_repo [master +0 ~1 -0]> git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: text.txt
#
C:\git\pc_repo [master +0 ~1 -0]> git reset --hard
HEAD is now at 4925c26 modified text
C:\git\pc_repo [master]> git status
# On branch master
nothing to commit, working directory clean
C:\git\pc_repo [master]>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.