Tuesday, 5 September 2023

Git merge, reset, commit --amend, rebase, pick cherry, handle conflict

There are two branch
commit on branch A: 123|4.......5
commit on branch B: 123|...678 (checkout from branh A from commit 3, after add commit 678)
---------------------------------------------------
* Git merge: 
add commits from branch B to branch A follow time create commit
- git checkout A
- git merge B
- handle conflict commits: 3 5 8
- git add .
- git commit -m"M"
=> A: 123|4|678|5|M
--------------------------------------------------
* Git rebase: 
add all commits from branch B to branch A at left side of branch A from postion of commit of both branch
- git checkout A
- git rebase B
- handle conflict commit: 8 to 4 -> git add . -> git rebase --continue => A: 123|678|4
- handle conflict commit: 4 to 5 -> git add . -> git rebase --continue => A: 123|678|45
- alert result rebase, handle commits with p,s,.. can change name commit in nano, ex 4 => 4'
- ctrl+x -> y to save
=> A: 123|678|4'5
* git rebase itself: select commit want to keep
- git rebase -i idCommit
- git checkout A (A: 12345)
- git rebsae -i 2
- handle conflict commit: 2 to 3 -> git add . -> git rebase --continue => A: 12|3
- handle conflict commit: 3 to 4 -> git add . -> git rebase --continue => A: 12|34
- handle conflict commit: 4 to 5 -> git add . -> git rebase --continue => A: 12|345
- alert result rebase, handle commits with p,s,.. can change name commit in nano, ex 3 => 3', 4->4'
- ctrl+x -> y to save
=> A: 12|3'4'5
---------------------------------------------------
* Git reset: remove commit from a branch 
+ git reset --soft idCommit  # Removes from HEAD to idCommit, keeps changed staged
+ git reset --hard idCommit # Removes from HEAD to idCommit, DONT keeps changed staged
---------------------------------------------------
* Git commit --amend: to change a last commit message.
+ git commit --amend -m "........"
+ git push origin BranchName -f
* Note: 
 
- Have branh A, PR A to DEV, PM agree merge
- if you commit --amend branch A, after PR A to DEV -> Conflict
---------------------------------------------------

* Git pick cherry: pick commit from a Branch to add for orther branch
- git checkout A
- git cherry-pick -e 7 (git cherry-pick -e idCommit)
-> A: 12345|7
or
- git checkout B
- git cherry-pick -e 5 (git cherry-pick -e idCommit)
-> B: 123678|5
---------------------------------------------------
* Git handle conflict when PR
- git checkout A
- git add .
- git commit -m"..."
- git push origin A
- PR: from A to DEV -> conflict
=> handle
- git checkout A
- git checkout -b A-fix-conflict // create this branch to fix-conflict // because dont want get code from DEV to merge to A
- git checkout DEV
- git pull origin DEV
- git checkout A-fix-conflict
- git merge DEV
- handle fix conflict in local
- git push origin A-fix-conflict
- PR from A-fix-conflict to DEV 
- PM aggree merge
=> now A can PR to DEV


Thank you


 

No comments:

Post a Comment

Golang Advanced Interview Q&A