【问题标题】:Replace working copy of git repo with actual contents of repo in .git?用.git中repo的实际内容替换git repo的工作副本?
【发布时间】:2013-03-08 21:46:18
【问题描述】:

TLDR

强制工作副本的内容成为 .git 子文件夹中 repo 中的实际内容的 git 命令是什么? (如果更改从另一台机器推送到具有工作副本的远程仓库)

长篇大论

我希望我公司内使用 perforce 的团队迁移到 git。我想使用 Git-P4 来实现这一点。我想将一部分 perforce 克隆到 git repo,并将其作为远程 repo,以便人们克隆它,将更改推送到远程 repo,我会定期将远程 repo 中所做的更改重新提交回 perforce .所以我遵循了这个教程

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

归结为这个命令:

git p4 clone //depot/path.to/folder@all folder

这行得通,然后在我的客户端机器上我做

git clone "user1@server:/home/user1/path/to/folder"

没关系,它会显示出来,所以我对测试文件进行了编辑,然后执行

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

当我尝试将其推送回远程仓库时,我在客户端收到此错误

git push
user1@server1'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 user1@server1:/home/user1/path/to/folder
 ! [remote rejected] master -> master (branch is currently checked out)
error: failed to push some refs to 'user1@server1:/home/user1/path/to/folder'

这里有解释

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

所以我设置

git config receive.denyCurrentBranch ignore

又试了一次, git push 成功了。但是这次回到远程仓库,它可以工作,但是当我尝试执行 git status 时它会抱怨一些不同的东西

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

它告诉我这是因为远程仓库中的工作副本与刚刚推送到远程仓库的工作副本不同。 git p4 submit 和 git p4 rebase 工作他们也抱怨这些未提交的更改

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 status 仍然显示由于工作副本和索引之间的差异导致的未暂存更改。

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/stash@{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
#

那么我如何强制工作副本代表 repo 中实际内容的内容?

【问题讨论】:

  • 您是否考虑过使用 Perforce Git Fusion?它基本上完成了 git-p4 所做的所有事情,但避免必须提交第二组 p4 工作文件。 perforce 也支持它。

标签: git perforce git-p4


【解决方案1】:

您可以使用以下命令将头部设置为任何状态:

git reset --hard REFSPEC

【讨论】:

    【解决方案2】:

    git checkout -f 获取存储库中所有文件的最新版本。根目录中的git clean -f 删除所有不受版本控制的文件,git clean -f -x 还删除显式忽略的文件(在.gitignore 文件中)。

    【讨论】:

      猜你喜欢
      • 2012-04-16
      • 1970-01-01
      • 2014-11-01
      • 2016-10-01
      • 2013-01-07
      • 2012-01-06
      • 2012-12-18
      • 2017-04-10
      • 1970-01-01
      相关资源
      最近更新 更多