2016-05-27 73 views
1

我在git中提交了一定數量的文件,例如,將單個文件移出git commit並將其放在單獨的git commit中

凱明1包含:

  • 文件1
  • 文件2 [改名]
  • 文件3
  • 文件4

現在我想移動文件2 [更名]的文件被放置在不影響其他文件的不同提交。在git中可能嗎?

+1

「提交1」HEAD提交? – kennytm

+0

是的,即使沒有合併。在合併之前我想把它分成兩個 – Shriram

+1

[刪除修改後的文件](http://stackoverflow.com/questions/10124593/remove-file-from-amended-commit)。特別是,[這個答案](http://stackoverflow.com/a/10127051/4233593)演示你想要什麼,但你需要重置'file2'和'file2 [重命名]' –

回答

0

如果提交1是最後一次提交,請嘗試git reset --hard HEAD然後git add單獨更改file1,file3和file4。

0
  1. 復位與軟標誌,以保持階段性變化
  2. 重啓重命名的文件單獨
  3. 作出新的提交

初始化庫

jeff ~ $ mkdir tg 
jeff ~ $ cd tg 
jeff tg $ git init 
Initialized empty Git repository in /home/administrator/tg/.git/ 
jeff tg (master #) $ touch file1 
jeff tg (master #) $ touch file2a 
jeff tg (master #) $ touch file3 
jeff tg (master #) $ touch file4 
jeff tg (master #) $ git add . 
jeff tg (master #) $ git commit -m "init" 
[master (root-commit) c4668aa] init 
4 files changed, 0 insertions(+), 0 deletions(-) 
create mode 100644 file1 
create mode 100644 file2a 
create mode 100644 file3 
create mode 100644 file4 

更改並承諾1

jeff tg (master) $ mv file2a file2 
jeff tg (master *) $ echo amendment >> file1 
jeff tg (master *) $ echo amendment >> file3 
jeff tg (master *) $ echo amendment >> file4 
jeff tg (master *) $ git add . 
jeff tg (master +) $ git status 
On branch master 
Changes to be committed: 
    (use "git reset HEAD <file>..." to unstage) 

     modified: file1 
     renamed: file2a -> file2 
     modified: file3 
     modified: file4 

jeff tg (master +) $ git commit -m "commit 1" 
[master b84034a] commit 1 
4 files changed, 3 insertions(+) 
rename file2a => file2 (100%) 

軟復位和unstage所需的單獨改變

jeff tg (master) $ git reset --soft HEAD~ 
jeff tg (master +) $ git reset -- file2a 
Unstaged changes after reset: 
D  file2a 
jeff tg (master *+) $ git status 
On branch master 
Changes to be committed: 
    (use "git reset HEAD <file>..." to unstage) 

     modified: file1 
     new file: file2 
     modified: file3 
     modified: file4 

Changes not staged for commit: 
    (use "git add/rm <file>..." to update what will be committed) 
    (use "git checkout -- <file>..." to discard changes in working directory) 

     deleted: file2a 

jeff tg (master *+) $ git reset -- file2 
Unstaged changes after reset: 
D  file2a 
jeff tg (master *+) $ git status 
On branch master 
Changes to be committed: 
    (use "git reset HEAD <file>..." to unstage) 

     modified: file1 
     modified: file3 
     modified: file4 

Changes not staged for commit: 
    (use "git add/rm <file>..." to update what will be committed) 
    (use "git checkout -- <file>..." to discard changes in working directory) 

     deleted: file2a 

Untracked files: 
    (use "git add <file>..." to include in what will be committed) 

     file2 

化妝只用其它變化文件(而不是重命名一個)

jeff tg (master *+) $ git commit -m "commit 2" 
[master 2f29079] commit 2 
3 files changed, 3 insertions(+) 

現在添加並提交重命名的文件提交

jeff tg (master *) $ git add . 
jeff tg (master +) $ git status 
On branch master 
Changes to be committed: 
    (use "git reset HEAD <file>..." to unstage) 

     renamed: file2a -> file2 

jeff tg (master +) $ git commit -m "commit 3" 
[master e2fd651] commit 3 
1 file changed, 0 insertions(+), 0 deletions(-) 
rename file2a => file2 (100%)