git merge --squash / git rebase -i / git cherry-pick

Keywords: Programming git REST shell vim

git merge --squash

git merge --squash {srcBranch} Often used infeatBranch merge todevThe complex commit logs are compressed to make the merge clear.

take srcBranchAhead of the current branch commits Merge to the current branch, and the current branch does notcommit,Allow us to do it manually once the conflict is successfully merged or resolved commit log,suchsrcBranchMultiple commits Only one is generated when merging to the current branchcommit Now.

git checkout dev
echo 1111 >> foo.txt
git add foo.txt
git commit -m "foo.txt 1111"

git checkout -b feat_1
echo 2222 >> foo.txt && git commit -am "foo.txt 2222"
echo 3333 >> foo.txt && git commit -am "foo.txt 3333"
echo 4444 >> foo.txt && git commit -am "foo.txt 4444"
git log

git checkout dev
# If we go directly to git merge feat 1, all the commits of feat 1 will be recorded in dev
# But we want to make it simple
git merge --squash feat_1

# Manually write log submission
git commit -m '
foo.txt 2222
foo.txt 3333
foo.txt 4444
'
# Or make diff logs submit as a template
git commit -v
# Enter vim mode to edit the merged log
:wq

# You can see that the 3 log s of feat_1 are only 1 log after they are merged into dev
git log

--squash only merges the content of the source branch into the stage area of the current branch, and leaves the commit to us, so that we can integrate the commit logs of other branches.

git rebase -i

Remember that branches that are not available for collaborative development can only be used for branches that are used alone.

git rebase -i [startPoint] [endPoint]

rebaseIt is convenient for us to edit thecommitRecords: merge, modify, delete. For example, I am infeatDevelop test on branchok,Need to merge todevBranches, butfeatThere are many complicatedcommit logs,We may not need todevSome people may think that you candevUpper use merge --squash,But iffeatbackwarddev Many, we may need to use git cherry-pick commitLogHashThe method willfeatThe specific submissions in are merged intodevBranch. We need todevCreate atempBranchtemp merge --squash featGet one-time submittedtemp_commit_log,Then switch todevimplement git cherry-pick {temp_commit_log}Merge todevBranch, or we can use rebase -i HEAD~N HEAD To merge multiple commits of this branch.

Merge multiple submission records to use one

git checkout dev
echo 1111 >> bar.txt && git add bar.txt
git commit -m "bar.txt 1111"

git checkout -b feat_2 dev
echo 2222 >> bar.txt && git commit -am "bar.txt 2222"
echo 3333 >> bar.txt && git commit -am "bar.txt 3333"
echo 4444 >> bar.txt && git commit -am "bar.txt 4444"

# After the development, we need to merge feat_2 back to dev, but we don't want too many feat-2 log s
git rebase -i dev

-----------------------------------------------------------------------
pick 0483730 bar.txt 2222
pick adf4d92 bar.txt 3333
pick cd1b421 bar.txt 4444

# Rebase 7ffea48..cd1b421 onto 7ffea48 (3 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# .       create a merge commit using the original merge commit's
# .       message (or the oneline, if no original merge commit was
# .       specified). Use -c <commit> to reword the commit message.
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
-----------------------------------------------------------------------

# Amend to read
pick 0483730 bar.txt 2222
s adf4d92 bar.txt 3333
s cd1b421 bar.txt 4444
:wq

-----------------------------------------------------------------------
# This is a combination of 3 commits.
# This is the 1st commit message:
# Merge multiple logs
bar.txt 2222
bar.txt 3333
bar.txt 4444
# After editing: wq save exit
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Fri Apr 26 14:26:09 2019 +0800
#
# interactive rebase in progress; onto 7ffea48
# Last commands done (3 commands done):
#    squash adf4d92 bar.txt 3333
#    squash cd1b421 bar.txt 4444
# No commands remaining.
# You are currently rebasing branch 'feat_2' on '7ffea48'.
-----------------------------------------------------------------------

# You can see that three submissions are merged into one
git log
# However, it will cause head detached from feed 40
git checkout -b temp
# View specific
git log
git checkout feat_2
git cherry-pick 
# It will be very simple to switch to dev and merge the feat_2 logs 
git checkout dev
git merge feat_2
git log

Modify commit log

Git rebase - I [startpoint] [endpoint] has an open range, so if we want to modify the submitted logs, we need to

#Rebuild last commit
git rebase -i HEAD~1
#Rebuild head ~ 3 - two commits of head ~ 2
git rebase -i HEAD~4 HEAD~2

For example, we have submitted foo4 of feat_4 four times. The log is as follows

git log --pretty=oneline --abbrev-commit
fed40ed (HEAD -> feat_4) foo4 4444
8455d9a foo4 3333
1548e84 foo4 2222
53e837b foo4 1111

Rewrite last committed log

git rebase -i HEAD~1
#Enter interactive mode
pick fed40ed foo4 4444

# Rebase 8455d9a..fed40ed onto 8455d9a (1 command)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# .       create a merge commit using the original merge commit's
# .       message (or the oneline, if no original merge commit was
# .       specified). Use -c <commit> to reword the commit message.
------------------------------------------------------------------------------
# Modify pick to word to keep this submission, but edit the submitted information
pick fed40ed foo4 4444
# Save and exit
:wq
# Log editing mode will be entered
foo4 4444

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Fri Apr 26 18:40:09 2019 +0800
#
# interactive rebase in progress; onto 8455d9a
# Last command done (1 command done):
#    reword fed40ed foo4 4444
# No commands remaining.
# You are currently editing a commit while rebasing branch 'feat_4' on '8455d9a'.
#
# Changes to be committed:
#       modified:   foo4
--------------------------------------------------------------------------------
# Modify to the log you want
foo4 4444 modify by rebase::reword
:wq

# You can see that the log has been modified
git log --pretty=oneline --abbrev-commit
7ccdb4c (HEAD -> feat_4) foo4 4444 modify by rebase:reword
8455d9a foo4 3333
1548e84 foo4 2222
53e837b foo4 1111

git rebase -i merges / modifies submitted records as shown above.

git cherry-pick

Git cherry pick is mainly used to select and merge the submitted branches on a branch to the current branch. For example, we have merged feat1 into dev to generate log-dev-feat-1, feat1 into dev to generate log-dev-feat-2. Now I just want to publish feat1 to the test branch. git merge dev can no longer meet this requirement, because we will merge log-dev-feat-2 together.

git checkout dev
git merge feat1
git merge feat2
git log --pertty=oneline
git checkout test
# Select and merge the submission of feat1
git cherry-pick feat1-log-hash

In this way, we can flexibly select the functional items to be published to the test environment.

Posted by viko20 on Sun, 03 Nov 2019 08:45:47 -0800