将git repo的工作副本替换为.git中的实际回购内容?

问题描述:

TLDR

git命令强制工作副本的内容成为.git子文件夹中回购库中的实际内容? (在这种变化是从另一台机器推到,有一个工作副本远程回购事件)将git repo的工作副本替换为.git中的实际回购内容?

说来话长

我想我的公司,它使用Perforce公司,搬到混帐中我的团队。我想用Git-P4来实现。我想要将一段perforce克隆到一个git仓库,并将其作为一个远程仓库,以便人们克隆它,将更改推送到远程仓库,并且我会定期重新提交远程仓库中所做的更改回到仓库。所以我跟着这个教程

http://answers.perforce.com/articles/KB_Article/Git-P4

它归结为这样的命令:

git p4 clone //depot/path.to/[email protected] folder 

这样的作品,那么我的客户端机器上我做

git clone "[email protected]:/home/user1/path/to/folder" 

这很好,它出现所以我编辑一个测试文件,然后做

git add test7 
git commit -m 'test' 
git push 

当我试图将其推回给远程的回购协议,我得到这个错误在客户端

git push 
[email protected]'s password: 
Counting objects: 5, done. 
Delta compression using up to 4 threads. 
Compressing objects: 100% (2/2), done. 
Writing objects: 100% (3/3), 273 bytes, done. 
Total 3 (delta 1), reused 1 (delta 0) 
remote: error: refusing to update checked out branch: refs/heads/master 
remote: error: By default, updating the current branch in a non-bare repository 
remote: error: is denied, because it will make the index and work tree inconsistent 
remote: error: with what you pushed, and will require 'git reset --hard' to match 
remote: error: the work tree to HEAD. 
remote: error: 
remote: error: You can set 'receive.denyCurrentBranch' configuration variable to 
remote: error: 'ignore' or 'warn' in the remote repository to allow pushing into 
remote: error: its current branch; however, this is not recommended unless you 
remote: error: arranged to update its work tree to match what you pushed in some 
remote: error: other way. 
remote: error: 
remote: error: To squelch this message and still keep the default behaviour, set 
remote: error: 'receive.denyCurrentBranch' configuration variable to 'refuse'. 
To [email protected]:/home/user1/path/to/folder 
! [remote rejected] master -> master (branch is currently checked out) 
error: failed to push some refs to '[email protected]:/home/user1/path/to/folder' 

其在这里解释

What are the consequences of using receive.denyCurrentBranch in Git?

所以后来我设置

git config receive.denyCurrentBranch ignore 

并尝试再次和git推工作。但回到位于远程回购这一次,它的工作原理,但它抱怨不同的东西,当我尝试做一个git状态

git status 
# On branch master 
# Changes to be committed: 
# (use "git reset HEAD <file>..." to unstage) 
# 
# deleted: test7 
# 

它告诉我这是因为远程回购工作副本是不一样的是什么刚被推到远程回购。 git的P4提交和git P4底垫的工作,他们也抱怨这些未提交的修改

git p4 submit 
Perforce checkout for depot path //depot/path.to/folder/ located at /home/user1/path/to/perforce.folder/ 
Synchronizing p4 checkout... 
... - file(s) up-to-date. 
Applying 38f67b9 cym: test 7 from linux 
//depot/path.to/folder/test7#1 - opened for add 
//depot/path.to/folder/test7#1 - nothing changed 
Submit template unchanged. Submit anyway? [y]es, [n]o (skip this patch) y 
Change 254654 created with 1 open file(s). 
Submitting change 254654. 
Locking 1 files ... 
add //depot/path.to/folder/test7#1 
Change 254654 submitted. 
All commits applied! 
Performing incremental import into refs/remotes/p4/master git branch 
Depot paths: //depot/path.to/folder/automation/ 
Import destination: refs/remotes/p4/master 
Importing revision 254654 (100%) 
You have uncommited changes. Please commit them before rebasing or stash them away with git stash. 

git p4 rebase 
Performing incremental import into refs/remotes/p4/master git branch 
Depot paths: //depot/path.to/folder/ 
No changes to import! 
You have uncommited changes. Please commit them before rebasing or stash them away with git stash. 

这似乎喜欢的事,会随着时间的推移一个大问题。我不想在该状态下永久保留远程回购工作副本。

所以我必须弄清楚如何用.git文件夹中的实际存储库索引强制覆盖工作存储库的内容。

现在我发现这个

How do I discard unstaged changes in Git?

里面说要做到这一点

git stash save --keep-index 
git stash drop 

或做

git checkout -- . 

既不这些工作之一。他们看起来像他们的工作,但增加的文件仍然不存在,并且git状态仍然显示工作副本和索引之间的差异导致的未分级更改。

git stash save --keep-index 
Saved working directory and index state WIP on master: 38f67b9 cym: test 7 from linux 
HEAD is now at 38f67b9 cym: test 7 from linux 
git stash drop 
Dropped refs/[email protected]{0} (3ce5805230e4faa3ec4dd2daa9cb65c86335e1a8) 
git status 
# On branch master 
# Changes to be committed: 
# (use "git reset HEAD <file>..." to unstage) 
# 
# deleted: test7 
# 

git checkout -- . 

git status 
# On branch master 
# Changes to be committed: 
# (use "git reset HEAD <file>..." to unstage) 
# 
# deleted: test7 
# 

那么我如何强制工作副本来表示实际上在回购中的内容?

+0

你有没有想过使用Perforce的Git的融合?它基本上做了一切git-p4做的,但避免必须有第二套p4工作文件提交。它也被perforce支持。 – cmcginty 2013-05-10 09:23:24

git checkout -f获取存储库中所有文件的最新版本。 git clean -f删除了所有不受版本控制的文件,git clean -f -x也删除了明确忽略的文件(在.gitignore文件中)。

+0

这是有效的。谢谢! – TinyGrasshopper 2013-03-20 05:35:23

您可以用命令你的头设置为任何状态:

git reset --hard REFSPEC