+. reset vs revert ?
reset : make clear commit history but, should clear whole commits before reset point
revert : delete only one commit between already committed items && good for remotely pushed commit (reset can be done by --force option but it is not desirable)
+. git commit --amend : add to last commit
+. git reset : change previous commit & restore old version
--soft : changes only commit history, commit history is deleted but, every changes are remained status -> can commit again
--mixed : default option, changes only staged files & commit history
--hard : changes all working directory, staged files, repository => most clear & popular way
cf. git reset HEAD <file> -> unstage specific file
cf. git checkout -- <file> -> changes file to original status of working directory (like just cloned status)
(or) git checkout -- .
+. git reset : merge commit history using 'git reset --soft HEAD~n'
>>> delete commit history & keep changes
tomcat@dev ~/workspace (master) $ ls
v1.txt v2.txt v3.txt v4.txt
tomcat@dev ~/workspace (master) $ git lg1
* 3adf76e - (4 seconds ago) v4.txt - jjung (HEAD -> master)
* ec6e89e - (12 seconds ago) v3.txt - jjung
* 7cbd268 - (20 seconds ago) v2.txt - jjung
* 467315b - (27 seconds ago) v1.txt - jjung
tomcat@dev ~/workspace (master) $ git reset --soft HEAD~3
tomcat@dev ~/workspace (master) $ git lg1
* 467315b - (2 minutes ago) v1.txt - jjung (HEAD -> master)
tomcat@dev ~/workspace (master) $ ls
v1.txt v2.txt v3.txt v4.txt
+. git reset : merge commit history using 'git reset --hard HEAD~n'
>>> delete commit history & remove changes
tomcat@mint ~/_gt[master] $ git lg1 -n 10
* 22a20d3 - (4 seconds ago) v3 - jjung (HEAD -> master)
* 713afad - (26 seconds ago) v2 - jjung
* 3b0bfea - (47 seconds ago) v1 - jjung
tomcat@mint ~/_gt[master] $ ls
1.txt 2.txt 3.txt
tomcat@mint ~/_gt[master] $ git reset --hard HEAD~1
HEAD is now at 713afad v2
tomcat@mint ~/_gt[master] $ ls
1.txt 2.txt
tomcat@mint ~/_gt[master] $ git lg1 -n 10
* 713afad - (58 seconds ago) v2 - jjung (HEAD -> master)
* 3b0bfea - (79 seconds ago) v1 - jjung
+. git revert : make new commit (after current version) & restore old version
+. git revert usage #01 : delete only one commit between already committed items
tomcat@dev ~/forked/gitrepo01 (dev) $ git lg1
* ae027e0 - (2 seconds ago) F-v3.txt - jjung (HEAD -> dev)
* 9d116e1 - (2 minutes ago) F-v2.txt - jjung
* 9f31110 - (74 minutes ago) Fork & Branch v1 - jjung (origin/dev)
* c600480 - (76 minutes ago) Main Stream update v1 - jjung (upstream/master)
* 2054426 - (83 minutes ago) first commit - jjung (origin/master, origin/HEAD, master)
tomcat@dev ~/forked/gitrepo01 (dev) $ ls
F-v1.txt F-v2.txt F-v3.txt M-v1.txt README.md
tomcat@dev ~/forked/gitrepo01 (dev) $ git revert 9d116e1
[dev 6cf7c7a] Revert "F-v2.txt"
1 file changed, 0 insertions(+), 0 deletions(-)
delete mode 100644 F-v2.txt
tomcat@dev ~/forked/gitrepo01 (dev) $ git lg1
* 6cf7c7a - (6 seconds ago) Revert "F-v2.txt" - jjung (HEAD -> dev)
* ae027e0 - (30 seconds ago) F-v3.txt - jjung
* 9d116e1 - (2 minutes ago) F-v2.txt - jjung
* 9f31110 - (74 minutes ago) Fork & Branch v1 - jjung (origin/dev)
* c600480 - (76 minutes ago) Main Stream update v1 - jjung (upstream/master)
* 2054426 - (84 minutes ago) first commit - jjung (origin/master, origin/HEAD, master)
tomcat@dev ~/forked/gitrepo01 (dev) $ ls
F-v1.txt F-v3.txt M-v1.txt README.md
+. git rebase
<<< Type #01 >>>
tomcat@serena ~/_git01 (master) $ git lg1
* c9f223d - (5 minutes ago) Revert "d1-v5" - jjung (dev )
* 33dff97 - (7 minutes ago) d1-v6 - jjung
* 8e28bcb - (8 minutes ago) d1-v5 - jjung
| * 237ffb3 - (7 minutes ago) m1-v6 - jjung (HEAD -> master )
| * 71522bd - (7 minutes ago) m1-v4 - jjung
| * 2849938 - (36 minutes ago) v5 - jjung
|/
* 9a7b911 - (36 minutes ago) v4 - jjung
* ee470e1 - (39 minutes ago) v2 - jjung
* 4c84853 - (39 minutes ago) v1 - jjung
tomcat@serena ~/_git01 (master ) $ git rebase c9f223d
First, rewinding head to replay your work on top of it...
Applying: v5
Applying: m1-v4
Applying: m1-v6
tomcat@serena ~/_git01 (master) $ git lg1
* 8dbc9e8 - (7 minutes ago) m1-v6 - jjung (HEAD -> master )
* 4e4a048 - (7 minutes ago) m1-v4 - jjung
* 449d995 - (37 minutes ago) v5 - jjung
* c9f223d - (6 minutes ago) Revert "d1-v5" - jjung (dev)
* 33dff97 - (8 minutes ago) d1-v6 - jjung
* 8e28bcb - (8 minutes ago) d1-v5 - jjung
* 9a7b911 - (37 minutes ago) v4 - jjung
* ee470e1 - (40 minutes ago) v2 - jjung
* 4c84853 - (40 minutes ago) v1 - jjung
<<< Type #02 >>>
tomcat@serena ~/_git01 (master) $ git lg1
* 9272161 - (2 seconds ago) m-r-2 - jjung (HEAD -> master )
* 88f8d6d - (11 seconds ago) m-r-1 - jjung
* 4e4a048 - (10 minutes ago) m1-v4 - jjung
* 449d995 - (39 minutes ago) v5 - jjung
| * 99b6d07 - (46 seconds ago) d-r-2 - jjung (dev )
| * b3d357f - (54 seconds ago) d-r-1 - jjung
|/
* c9f223d - (8 minutes ago) Revert "d1-v5" - jjung
* 33dff97 - (11 minutes ago) d1-v6 - jjung
* 8e28bcb - (11 minutes ago) d1-v5 - jjung
* 9a7b911 - (40 minutes ago) v4 - jjung
* ee470e1 - (42 minutes ago) v2 - jjung
* 4c84853 - (43 minutes ago) v1 - jjung
tomcat@serena ~/_git01 (master) $ git rebase dev
First, rewinding head to replay your work on top of it...
Applying: v5
Applying: m1-v4
Applying: m-r-1
Applying: m-r-2
tomcat@serena ~/_git01 (master) $ git lg1
* 299b6e1 - (31 seconds ago) m-r-2 - jjung (HEAD -> master)
* 08322c9 - (40 seconds ago) m-r-1 - jjung
* e987916 - (11 minutes ago) m1-v4 - jjung
* 5e77ffc - (40 minutes ago) v5 - jjung
* 99b6d07 - (75 seconds ago) d-r-2 - jjung (dev)
* b3d357f - (83 seconds ago) d-r-1 - jjung
* c9f223d - (9 minutes ago) Revert "d1-v5" - jjung
* 33dff97 - (11 minutes ago) d1-v6 - jjung
* 8e28bcb - (11 minutes ago) d1-v5 - jjung
* 9a7b911 - (40 minutes ago) v4 - jjung
* ee470e1 - (43 minutes ago) v2 - jjung
* 4c84853 - (43 minutes ago) v1 - jjung
<<< Type #03 >>>
tomcat@mint ~/_gt[master] $ git lg1 -n 10
* db6bdf1 - (4 seconds ago) v4 - jjung (HEAD -> master)
* 9ec6ca0 - (14 seconds ago) v3 - jjung
* 713afad - (80 minutes ago) v2 - jjung
* 3b0bfea - (80 minutes ago) v1 - jjung
tomcat@mint ~/_gt[master] $ ls
1.txt 2.txt 3.txt 4.txt
tomcat@mint ~/_gt[master] $ git rebase -i HEAD~3
---------------------------------------
pick 713afad v2
pick 342adf3 v3 --> delete this line (commit v3)
pick 2e86b18 v4
# Rebase 3b0bfea..2e86b18 onto 3b0bfea (2 commands)
---------------------------------------
Successfully rebased and updated refs/heads/master.
tomcat@mint ~/_gt[master] $ ls
1.txt 2.txt 4.txt
tomcat@mint ~/_gt[master] $ git lg1 -n 10
* 2e86b18 - (63 seconds ago) v4 - jjung (HEAD -> master)
* 713afad - (81 minutes ago) v2 - jjung
* 3b0bfea - (81 minutes ago) v1 - jjung
+. git rebase & upstream
+. using rebase for cherry-picking multiple commits
(Scenario: need to merge 'some of required commits' from 'develop branch' to 'master branch')
tomcat@serena ~/_git01 (master) $ git lg1
* 8c889a6 - (4 minutes ago) dev01-3 - jjung (HEAD -> dev01 )
* e1c88dc - (6 minutes ago) dev01-2 - jjung
* ed4a439 - (7 minutes ago) dev01-1 - jjung
| * ff9ee46 - (5 minutes ago) m-3 - jjung (master )
| * 2489da9 - (6 minutes ago) m-2 - jjung
|/
* 3ac7d31 - (9 minutes ago) v1 - jjung
tomcat@serena ~/_git01 (master) $ git checkout -b dev01_rebase
Switched to a new branch 'dev01_rebase'
tomcat@serena ~/_git01 (dev01_rebase) $ git lg1
* 8c889a6 - (4 minutes ago) dev01-3 - jjung (HEAD -> dev01_rebase, dev01 )
* e1c88dc - (6 minutes ago) dev01-2 - jjung
* ed4a439 - (7 minutes ago) dev01-1 - jjung
| * ff9ee46 - (5 minutes ago) m-3 - jjung (master )
| * 2489da9 - (6 minutes ago) m-2 - jjung
|/
* 3ac7d31 - (9 minutes ago) v1 - jjung
tomcat@serena ~/_git01 (dev01_rebase) $ git rebase --interactive master
(select only required commit)
Successfully rebased and updated refs/heads/dev01_rebase.
tomcat@serena ~/_git01 (dev01_rebase) $ git lg1
* d3d2882 - (4 minutes ago) dev01-2 - jjung (HEAD -> dev01_rebase )
* 358474d - (4 minutes ago) dev01-1 - jjung
* ff9ee46 - (3 minutes ago) m-3 - jjung (master )
* 2489da9 - (3 minutes ago) m-2 - jjung
| * 8c889a6 - (2 minutes ago) dev01-3 - jjung (dev01 )
| * e1c88dc - (4 minutes ago) dev01-2 - jjung
| * ed4a439 - (4 minutes ago) dev01-1 - jjung
|/
* 3ac7d31 - (7 minutes ago) v1 - jjung
tomcat@serena ~/_git01 (dev01_rebase) $ git checkout master
Switched to branch 'master'
tomcat@serena ~/_git01 (master) $ git merge dev01_rebase
Updating ff9ee46..d3d2882
Fast-forward
dev01-1.txt | 1 +
dev01-2.txt | 1 +
2 files changed, 2 insertions(+)
create mode 100644 dev01-1.txt
create mode 100644 dev01-2.txt
tomcat@serena ~/_git01 (master) $ git lg1
* d3d2882 - (5 minutes ago) dev01-2 - jjung (HEAD -> master, dev01_rebase )
* 358474d - (5 minutes ago) dev01-1 - jjung
* ff9ee46 - (4 minutes ago) m-3 - jjung
* 2489da9 - (4 minutes ago) m-2 - jjung
| * 8c889a6 - (3 minutes ago) dev01-3 - jjung (dev01 )
| * e1c88dc - (5 minutes ago) dev01-2 - jjung
| * ed4a439 - (5 minutes ago) dev01-1 - jjung
|/
* 3ac7d31 - (8 minutes ago) v1 - jjung
tomcat@serena ~/_git01 (master) $ git branch -D dev01_rebase
+. git cherry-pick (merge for only required commit)
tomcat@serena ~/_gt2 (master) $ git lg1
* 4b5b440 - (45 seconds ago) m-3 - jjung (HEAD -> master)
* 4f6f33a - (53 seconds ago) m-2 - jjung
| * 3722f30 - (84 seconds ago) dev01-3 - jjung (dev01)
| * fef466f - (2 minutes ago) dev01-2 - jjung
| * 27e9013 - (2 minutes ago) dev01-1 - jjung
|/
* 98141d5 - (2 minutes ago) v1 - jjung
tomcat@serena ~/_gt2 (master) $ git cherry-pick fef466f
[master 1d2c11a] dev01-2
Date: Thu Mar 8 14:36:37 2018 +1300
1 file changed, 1 insertion(+)
create mode 100644 dev01-2.txt
tomcat@serena ~/_gt2 (master) $ git lg1
* 1d2c11a - (2 minutes ago) dev01-2 - jjung (HEAD -> master)
* 4b5b440 - (60 seconds ago) m-3 - jjung
* 4f6f33a - (68 seconds ago) m-2 - jjung
| * 3722f30 - (2 minutes ago) dev01-3 - jjung (dev01)
| * fef466f - (2 minutes ago) dev01-2 - jjung
| * 27e9013 - (2 minutes ago) dev01-1 - jjung
|/
* 98141d5 - (3 minutes ago) v1 - jjung