Method to amend commits.
Change files.
Commit changes
git commit -a
Enter commit message, save and exit.
Your commit will appear in the log.
If you want to change your latest or earlier commit (not yet pushed upstream), execute:
git-checkout commit_id_to_update
Edit any files you wish in working branch.
git-add files/which/are/changed
This will stage the updated files for commit.
git-commit --amend -v
Here you can edit the commit message, save and exit.
Now you have to rebase the existing commits from working branch.
git rebase --onto HEAD commit_id_to_update current_working_branch_name
Bingo! You are done!
Example:
I am working on Linux. So I will take kernel repo for example.
Step 1
Current status of repo.
$ git log --pretty=short
commit 7686e3c16c4a3669472298ecfc4636485658642b
Merge: 2f2e9f2dd1f4 75c1657e1d50
Author: Linus Torvalds <torvalds@linux-foundation.org>
Merge tag 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dledford/rdma
commit 2f2e9f2dd1f454f3c4f751b6f83359bc48f21096
Merge: 4617c2203fbc 5327c7dbd1a7
Author: Linus Torvalds <torvalds@linux-foundation.org>
Merge git://git.kernel.org/pub/scm/linux/kernel/git/nab/target-pending
Step 2
I made some changes in the repo.
$ vim Documentation/driver-model/device.txt
$ git diff
diff --git a/Documentation/driver-model/device.txt b/Documentation/driver-model/device.txt
index 1e70220d20f4..a429eaa60b8d 100644
--- a/Documentation/driver-model/device.txt
+++ b/Documentation/driver-model/device.txt
@@ -29,7 +29,7 @@ get_device() will return a pointer to the struct device passed to it
if the reference is not already 0 (if it's in the process of being
removed already).
-A driver can access the lock in the device structure using:
+A driver can access the lock in the device structure using:
void lock_device(struct device * dev);
void unlock_device(struct device * dev);
$ git branch -v
* master 7686e3c16c4a Merge tag 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dledford/rdma
$
Step 3
Commit changes
$ git commit -a
[master 6a65e6e029f4] Documentation: device.txt: removed white space
1 file changed, 1 insertion(+), 1 deletion(-)
$
$ git log --pretty=short
commit 6a65e6e029f44fa2dce0729d2502ba4b6067cdc4
Author: ABC XYZ <ab@xyz.com>
Documentation: device.txt: removed white space
commit 7686e3c16c4a3669472298ecfc4636485658642b
Merge: 2f2e9f2dd1f4 75c1657e1d50
Author: Linus Torvalds <torvalds@linux-foundation.org>
Merge tag 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dledford/rdma
commit 2f2e9f2dd1f454f3c4f751b6f83359bc48f21096
Merge: 4617c2203fbc 5327c7dbd1a7
Author: Linus Torvalds <torvalds@linux-foundation.org>
Merge git://git.kernel.org/pub/scm/linux/kernel/git/nab/target-pending
Step 4
Checkout a particular commit (to be updated)
$ git checkout 6a65e6e029f44fa2dce0729d2502ba4b6067cdc4
Note: checking out '6a65e6e029f44fa2dce0729d2502ba4b6067cdc4'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:
git checkout -b <new-branch-name>
HEAD is now at 6a65e6e029f4... Documentation: device.txt: removed white space
$
Step 5
Make changes
$ vim Documentation/driver-model/device.txt
$ git branch -v
* (HEAD detached at 6a65e6e029f4) 6a65e6e029f4 Documentation: device.txt: removed white space
master 6a65e6e029f4 [ahead 1] Documentation: device.txt: removed white space
$
$ git status
HEAD detached at 6a65e6e029f4
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: Documentation/driver-model/device.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
a.out
no changes added to commit (use "git add" and/or "git commit -a")
$
Step 6
Add changed files to staging for commit.
$ git add Documentation/driver-model/device.txt
$ git status
HEAD detached at 6a65e6e029f4
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: Documentation/driver-model/device.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
a.out
$
Step 7
Amend the commit. You can update commit message here.
$ git commit --amend -v
[detached HEAD 20eb25f6ee25] Documentation: device.txt: Removed white space. Corrected typo (extra 'be').
Date: Thu Mar 3 15:19:16 2016 +0530
1 file changed, 2 insertions(+), 2 deletions(-)
$
$ git log --pretty=short
commit 20eb25f6ee250a13af18d93e34269eaa9bae6ff7
Author: ABC XYZ <ab@xyz.com>
Documentation: device.txt: Removed white space. Corrected typo (extra 'be').
commit 7686e3c16c4a3669472298ecfc4636485658642b
Merge: 2f2e9f2dd1f4 75c1657e1d50
Author: Linus Torvalds <torvalds@linux-foundation.org>
Merge tag 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dledford/rdma
commit 2f2e9f2dd1f454f3c4f751b6f83359bc48f21096
Merge: 4617c2203fbc 5327c7dbd1a7
Author: Linus Torvalds <torvalds@linux-foundation.org>
Merge git://git.kernel.org/pub/scm/linux/kernel/git/nab/target-pending
$ git branch -v
* (HEAD detached from 6a65e6e029f4) 20eb25f6ee25 Documentation: device.txt: Removed white space. Corrected typo (extra 'be').
master 6a65e6e029f4 [ahead 1] Documentation: device.txt: removed white space
Step 8
Rebase your updated commit on working branch.
$ git rebase --onto HEAD 6a65e6e029f44fa2dce0729d2502ba4b6067cdc4 master
First, rewinding head to replay your work on top of it...
$
$ git log --pretty=short
commit 20eb25f6ee250a13af18d93e34269eaa9bae6ff7
Author: ABC XYZ <ab@xyz.com>
Documentation: device.txt: Removed white space. Corrected typo (extra 'be').
commit 7686e3c16c4a3669472298ecfc4636485658642b
Merge: 2f2e9f2dd1f4 75c1657e1d50
Author: Linus Torvalds <torvalds@linux-foundation.org>
Merge tag 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dledford/rdma
commit 2f2e9f2dd1f454f3c4f751b6f83359bc48f21096
Merge: 4617c2203fbc 5327c7dbd1a7
Author: Linus Torvalds <torvalds@linux-foundation.org>
Merge git://git.kernel.org/pub/scm/linux/kernel/git/nab/target-pending
$
$ git branch -v
* master 20eb25f6ee25 [ahead 1] Documentation: device.txt: Removed white space. Corrected typo (extra 'be').
$
Change files.
Commit changes
git commit -a
Enter commit message, save and exit.
Your commit will appear in the log.
If you want to change your latest or earlier commit (not yet pushed upstream), execute:
git-checkout commit_id_to_update
Edit any files you wish in working branch.
git-add files/which/are/changed
This will stage the updated files for commit.
git-commit --amend -v
Here you can edit the commit message, save and exit.
Now you have to rebase the existing commits from working branch.
git rebase --onto HEAD commit_id_to_update current_working_branch_name
Bingo! You are done!
Example:
I am working on Linux. So I will take kernel repo for example.
Step 1
Current status of repo.
$ git log --pretty=short
commit 7686e3c16c4a3669472298ecfc4636485658642b
Merge: 2f2e9f2dd1f4 75c1657e1d50
Author: Linus Torvalds <torvalds@linux-foundation.org>
Merge tag 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dledford/rdma
commit 2f2e9f2dd1f454f3c4f751b6f83359bc48f21096
Merge: 4617c2203fbc 5327c7dbd1a7
Author: Linus Torvalds <torvalds@linux-foundation.org>
Merge git://git.kernel.org/pub/scm/linux/kernel/git/nab/target-pending
Step 2
I made some changes in the repo.
$ vim Documentation/driver-model/device.txt
$ git diff
diff --git a/Documentation/driver-model/device.txt b/Documentation/driver-model/device.txt
index 1e70220d20f4..a429eaa60b8d 100644
--- a/Documentation/driver-model/device.txt
+++ b/Documentation/driver-model/device.txt
@@ -29,7 +29,7 @@ get_device() will return a pointer to the struct device passed to it
if the reference is not already 0 (if it's in the process of being
removed already).
-A driver can access the lock in the device structure using:
+A driver can access the lock in the device structure using:
void lock_device(struct device * dev);
void unlock_device(struct device * dev);
$ git branch -v
* master 7686e3c16c4a Merge tag 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dledford/rdma
$
Step 3
Commit changes
$ git commit -a
[master 6a65e6e029f4] Documentation: device.txt: removed white space
1 file changed, 1 insertion(+), 1 deletion(-)
$
$ git log --pretty=short
commit 6a65e6e029f44fa2dce0729d2502ba4b6067cdc4
Author: ABC XYZ <ab@xyz.com>
Documentation: device.txt: removed white space
commit 7686e3c16c4a3669472298ecfc4636485658642b
Merge: 2f2e9f2dd1f4 75c1657e1d50
Author: Linus Torvalds <torvalds@linux-foundation.org>
Merge tag 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dledford/rdma
commit 2f2e9f2dd1f454f3c4f751b6f83359bc48f21096
Merge: 4617c2203fbc 5327c7dbd1a7
Author: Linus Torvalds <torvalds@linux-foundation.org>
Merge git://git.kernel.org/pub/scm/linux/kernel/git/nab/target-pending
Step 4
Checkout a particular commit (to be updated)
$ git checkout 6a65e6e029f44fa2dce0729d2502ba4b6067cdc4
Note: checking out '6a65e6e029f44fa2dce0729d2502ba4b6067cdc4'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:
git checkout -b <new-branch-name>
HEAD is now at 6a65e6e029f4... Documentation: device.txt: removed white space
$
Step 5
Make changes
$ vim Documentation/driver-model/device.txt
$ git branch -v
* (HEAD detached at 6a65e6e029f4) 6a65e6e029f4 Documentation: device.txt: removed white space
master 6a65e6e029f4 [ahead 1] Documentation: device.txt: removed white space
$
$ git status
HEAD detached at 6a65e6e029f4
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: Documentation/driver-model/device.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
a.out
no changes added to commit (use "git add" and/or "git commit -a")
$
Step 6
Add changed files to staging for commit.
$ git add Documentation/driver-model/device.txt
$ git status
HEAD detached at 6a65e6e029f4
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: Documentation/driver-model/device.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
a.out
$
Step 7
Amend the commit. You can update commit message here.
$ git commit --amend -v
[detached HEAD 20eb25f6ee25] Documentation: device.txt: Removed white space. Corrected typo (extra 'be').
Date: Thu Mar 3 15:19:16 2016 +0530
1 file changed, 2 insertions(+), 2 deletions(-)
$
$ git log --pretty=short
commit 20eb25f6ee250a13af18d93e34269eaa9bae6ff7
Author: ABC XYZ <ab@xyz.com>
Documentation: device.txt: Removed white space. Corrected typo (extra 'be').
commit 7686e3c16c4a3669472298ecfc4636485658642b
Merge: 2f2e9f2dd1f4 75c1657e1d50
Author: Linus Torvalds <torvalds@linux-foundation.org>
Merge tag 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dledford/rdma
commit 2f2e9f2dd1f454f3c4f751b6f83359bc48f21096
Merge: 4617c2203fbc 5327c7dbd1a7
Author: Linus Torvalds <torvalds@linux-foundation.org>
Merge git://git.kernel.org/pub/scm/linux/kernel/git/nab/target-pending
$ git branch -v
* (HEAD detached from 6a65e6e029f4) 20eb25f6ee25 Documentation: device.txt: Removed white space. Corrected typo (extra 'be').
master 6a65e6e029f4 [ahead 1] Documentation: device.txt: removed white space
Step 8
Rebase your updated commit on working branch.
$ git rebase --onto HEAD 6a65e6e029f44fa2dce0729d2502ba4b6067cdc4 master
First, rewinding head to replay your work on top of it...
$
$ git log --pretty=short
commit 20eb25f6ee250a13af18d93e34269eaa9bae6ff7
Author: ABC XYZ <ab@xyz.com>
Documentation: device.txt: Removed white space. Corrected typo (extra 'be').
commit 7686e3c16c4a3669472298ecfc4636485658642b
Merge: 2f2e9f2dd1f4 75c1657e1d50
Author: Linus Torvalds <torvalds@linux-foundation.org>
Merge tag 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dledford/rdma
commit 2f2e9f2dd1f454f3c4f751b6f83359bc48f21096
Merge: 4617c2203fbc 5327c7dbd1a7
Author: Linus Torvalds <torvalds@linux-foundation.org>
Merge git://git.kernel.org/pub/scm/linux/kernel/git/nab/target-pending
$
$ git branch -v
* master 20eb25f6ee25 [ahead 1] Documentation: device.txt: Removed white space. Corrected typo (extra 'be').
$
No comments:
Post a Comment