2017-07-14 99 views
1

我有一个设置(它需要这样),我正在同步更新的文件到git存储库。这发生在文件更改时,并且在此同步过程中忽略.git文件夹。`git reset --hard`会为我改变吗?

所以

  • 客户机(与回购结帐)>文件同步(忽略的.git)>服务器(带有回购结帐)
  • 客户>上游git仓库(不时)
  • 上游git仓库>服务器(从时间到时间)

有些时候,我需要基于上游上server更新GIT中,所以我使用git fetch --all && git reset --hard origin/branchname。哪些工作为此目的。

但是,我希望能够检测到,如果这实际上更新任何文件或没有。 换句话说,如果客户端文件同步是最新的,与上游混帐回购协议,我想检测吧..

要做到这一点,因为我能找到的最接近的方式是一个答案here这是使用类似git merge-tree $(git merge-base FETCH_HEAD master) FETCH_HEAD master。但问题是,它显示了变化,即使该文件和内容已经存在......

与测试用例

$ { ~/repos }$ mkdir a && cd a 

$ { ~/repos/a }$ git init 
Initialized empty Git repository in ~/repos/a/.git/ 

$ { ~/repos/a }$ echo file1 > f1 && git add f1 && git commit -m "1" 
[master (root-commit) 9b1b025] .. 
1 file changed, 1 insertion(+) 
create mode 100644 f1 

$ { ~/repos }$ git clone a b 
Cloning into 'b'... 
done. 

$ { ~/repos }$ cd b 

$ { ~/repos/b }$ ls 
f1 

$ { ~/repos/a }$ echo file2 > f2 && git add f2 && git commit -m "2" 
[master 4e40da5] 2 
1 file changed, 1 insertion(+) 
create mode 100644 f2 

$ { ~/repos/b }$ git fetch --all 
Fetching origin 
remote: Counting objects: 3, done. 
remote: Compressing objects: 100% (2/2), done. 
remote: Total 3 (delta 0), reused 0 (delta 0) 
Unpacking objects: 100% (3/3), done. 
From ~/repos/a 
    9b1b025..4e40da5 master  -> origin/master 

# The folder `b` started with 1 file `f1`, and it can now 
# see the new file `f2` as expected. 
$ { ~/repos/b }$ git diff HEAD..origin/master 
diff --git a/f2 b/f2 
new file mode 100644 
index 0000000..6c493ff 
--- /dev/null 
+++ b/f2 
@@ -0,0 +1 @@ 
+file2 

# Without using git (another file sync job, think rsync), 
# the `f2` is created identical, but without informing git. 
# After this, the repos contains the exact same data, 
# but git doesn't know that. 
$ { ~/repos/b }$ echo file2 > f2 

# However, git diff still thinks this file is new.. 
# At this point, I want to be able to see if 
# the origin/master is identical to the content of this repository. 
$ { ~/repos/b }$ git diff HEAD..origin/master 
diff --git a/f2 b/f2 
new file mode 100644 
index 0000000..6c493ff 
--- /dev/null 
+++ b/f2 
@@ -0,0 +1 @@ 
+file2 

# Untill I do a reset --hard 
$ { ~/repos/b }$ git reset --hard origin/master 
HEAD is now at 4e40da5 2 

# Doesn't show any output 
$ { ~/repos/b }$ git diff HEAD..origin/master 
+1

所以......你想知道'origin/branchname'与'branchname'不同吗?那不就是'git diff branchname origin/branchname'吗?我错过了关于这个问题的东西......? –

+0

请注意,'git reset --hard'会重置索引和工作树中存储的文件(无论出自所选提交的文件),因此如果您的文件与任何提交中的文件不同,那么这些文件也会得到改变。 – torek

回答

1

更新您可以reset之前看到和HEADdiff之间origin/master

$ git fetch --all 
$ git diff HEAD..origin/master  # see what is in origin/master that is not in HEAD (local latest commit) 

$ git diff origin/master..HEAD  # see what is in HEAD that is not in origin/master 

# If local uncommitted changes exist (tracked by git) 
$ git diff origin/master 

# for untracked/new files, one way is to use '-N' flag with 'git add' command 
$ git add -N . 
$ git diff origin/master 

更多关于 '-N':在$ git add --help

-N, --intent-to-add 
     Record only the fact that the path will be added later. An entry for the path 
     is placed in the index with no content. This is useful for, among other 
     things, showing the unstaged content of such files with git diff and 
     committing them with git commit -a. 
+0

谢谢..这有点合理,但它没有办法。我用问题的一个例子更新了问题。 – xeor

+0

因为你没有提交更改(在'repo'b'中创建'f2'文件并且尚未提交],请'git diff origin/master'。 'HEAD'指向最新的本地提交。 –

+0

使用'git diff origin/master'可以处理已经被git添加和跟踪的文件。但是没有在新文件上工作。这比没有好多了..所以问题已经解决了一半:)你能想出一种办法来处理新文件吗? – xeor

0

只要有服务器检出的(由SHA)后接收和git push做你的第1步。 “文件同步(忽略.git)”,而只有在更改内容时才会执行正好推送提交的更改会执行哪些操作。

相关问题