2017-07-14 84 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)」,而只有在更改內容時纔會執行正好推送提交的更改會執行哪些操作。

相關問題