I have committed certain number of file in git e.g.
Commit 1 contains:
Now I want to move file2[renamed] file to be placed in different commit without disturbing other files. Is it possible in git?
initialize repository
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
make changes and commit 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%)
soft reset and unstage desired separate changes
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
make commit with just other changes files (not renamed one)
jeff tg (master *+) $ git commit -m "commit 2"
[master 2f29079] commit 2
 3 files changed, 3 insertions(+)
now add and commit renamed file
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%)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With