[Git Basics] Commonly used git commands (2)

1. Merge commit into one

When using git, we may have multiple submissions for the same task. For example, we may have multiple colleagues modify different parts of the same feature, but multiple submissions make our version management look messy. At this time, we can Merge multiple commits into one.

1.1 git commit --amend

It's a Git command to modify the most recent commit. Use this command when you want to change the latest commit message, add missing file changes, or tweak already committed files. Note that this operation changes the commit hash, so be careful when using it on shared branches.

  1. Adding Missing Changes: If you realize that you missed some changes in a recent commit, you can first add the missing files to the staging area and then add them git commit --amendto the most recent commit with . For example:
    git add forgotten-file.txt
    git commit --amend --no-edit
    
    This will forgotten-file.txtadd to the most recent commit, and preserve the original commit message (because of the option used --no-edit).
  2. Adjusting committed files: If you want to modify committed files, you can first make changes to the files, then add the changed files to the staging area and merge them into the most recent commit with git commit --amend:
    # 对文件进行更改
    git add modified-file.txt
    git commit --amend --no-edit
    
    This will modified-file.txtadd the changes to the most recent commit and keep the original commit message, which is equivalent to merging this commit with the previous commit and retaining the message of the last commit.

Note that git commit --amendthe command creates a new commit object and replaces the original commit. This means the commit hash changes. Be cautious when doing this on a branch shared with others, as it can lead to inconsistencies in the branch history.

1.2 git rebase -i

This method uses Git's interactive rebase feature (interactive rebase), allowing you to modify the commit history. Take the following commit history as an example:

A---B---C---D---E  (main)

Suppose you want to merge commits C, D, and E into one commit. You can do it like this:

  1. Switch to the branch you want to modify (eg main):
    git checkout main
    
  2. Use git rebase -ito select the range of commits to merge. To merge C, D and E, we need to rebase from B:
    git rebase -i B^
    
  3. This will open a text editor showing a list of commits similar to the following:
    pick C
    pick D
    pick E
    
  4. Change the actions of the second and third commits (D and E) from pickto squash(or s for short):
    pick C
    squash D
    squash E
    
  5. Save and close the editor. This will open an editor again, allowing you to edit the new commit message. Create a new commit message merging C, D, and E, then save and close the editor.
  6. At this point, the commit history will become:
    A---B---F  (main)
    
    F is the new merge commit.

1.3 git reset

This approach will undo the commit, but keep the workspace changes. You can then create a new commit that includes the changes from the previous commits. Using the same example as a reference:

  1. Switch to the branch you want to modify (eg main):
    git checkout main
    
  2. Use git resetthe command to roll back to commit B, with --softthe option to preserve workspace changes:
    git reset --soft B
    
  3. Now, use git committo create a new commit containing the changes from C, D, and E. When committing, add a descriptive commit message:
    git commit -m "合并 C, D 和 E 的更改"
    
  4. At this point, your commit history will become:
    A---B---F  (main)
    
    F is the new merge commit.

1.4 Examples

  1. Added 3 commits
    insert image description here
  2. git rebase -i <hash>
    insert image description here
    Change the last two picks to squash using
  3. Edit the commit information.
    insert image description here
    Here I will annotate the following two information, and change the first one to: feat:add new declaration
    After saving, the commit will be automatically completed, and then push, and the three commits will be merged into one.
  4. visualization
    insert image description here

2. Modify the content of the commit

2.1 git commit --amend

git commit --amendcommand allows you to modify the last commit. This will use the current staging area as the new commit. If there is no change in the temporary storage area , this command can be used to modify the commit information.

For example:
Suppose a commit has just been submitted, but it is found that a file has been forgotten to be added. You can follow the steps below:

git add forgotten_file.txt
git commit --amend

Next, a text editor will open, where you can modify the commit information. After saving and closing the editor, the new commit will replace the previous commit.
insert image description here

2.2 git rebase -i

git rebase -iCommands allow you to modify past multiple commits in one interactive interface. You can change the order of commits, merge, split, and edit commit messages.

An example:
Suppose you want to modify the third last commit, you can follow the steps below:

git rebase -i HEAD~3

This will display a list similar to:

pick f392171 Commit message 1
pick 58a9d84 Commit message 2
pick b6a0f7c Commit message 3

pickChange the line before the commit line you want to modify edit, then save and close the file. For example, we want to modify the second commit:

pick f392171 Commit message 1
edit 58a9d84 Commit message 2
pick b6a0f7c Commit message 3

Now, Git will rollback to the second commit. You can make any modifications such as adding, deleting or modifying files. Once done, add the changes to the staging area:

git add modified_file.txt

Then, run git commit --amendAmend commit message. Once done, continue with the rebase process:

git rebase --continue

If you encounter a conflict during the rebase process, you need to resolve the conflict, add the resolved file to the temporary storage area, and then continue the rebase process.

If you encounter a conflict during the rebase process, you need to resolve the conflict, add the resolved file to the temporary storage area, and then continue the rebase process.

2.3 git cherry-pick

git cherry-pickA command can apply a specific commit on one branch to another branch. This is useful for modifying the contents of a specific commit, especially if multiple branches are involved.

Example:
Suppose there are two branches: masterand feature, you want to modify featurea commit on the branch. You can follow the steps below:

First, check featurethe commit history on the branch and find the commit you want to modify (eg, a123456). Then mastercreate a new branch on the branch:

git checkout master
git checkout -b temp

Next, git cherry-pickapply the commit to be modified to the new branch using the command:

git cherry-pick a123456

Now, the contents of the commit can be modified. When you're done making modifications, add the changes to the staging area and commit git commit --amendthe message with the modification. Finally, merge the modified commit back into featurethe branch:

git checkout feature
git merge temp

Finally, delete the temporary branch:

git branch -d temp

2.4 git reflog and git reset

In some cases, you may need to restore commits that were lost due to misuse. git reflogcan help you find these missing commits. git resetcommand can be used to reset the branch pointer to a specific commit.

Example:
Suppose you accidentally git push --forceoverwrite the remote branch, resulting in the loss of some commits. First, git reflogfind missing commits using:

git reflog

Find the hash of the missing commit (for example, b123456), then git resetreset the branch pointer to that commit using:

git reset b123456

This way, you can recover lost commits and make changes as needed.

Note that these operations may cause branch history to change, so use caution when performing these operations on shared branches. Also, to prevent unnecessary problems, please backup your repository before performing these operations.

3. View commit content

3.1 git log

version without any arguments
insert image description here

3.2 git log --oneline

Each log is only shown in one line
insert image description here

3.3 git log -[length]

Only display the previous length logs
insert image description here

3.4 git log --skip=[skip]

Skip the previous skip log, which can be used in conjunction with the above -[length]
insert image description here

3.5 git log -p

Display some statistics and file changes and line information
insert image description here

3.6 git log --stat

Show commit author, date, message and file content statistics
insert image description here

3.7 git shortlog

Display each author's commit and how many commits
insert image description here

3.8 Filtration

  1. by date
    git log --after="2023-4-1" # 2023年4月1号之后的所有日志
    git log –-before="2013-4-1" # 2023年4月1号之前的所有日志
    
  2. by author
    git log --author="Ricky"
    
  3. press submit info
    git log --grep=“issue” # 查看提交本中包含issue的日志
    
  4. by file
    git log -- ./src/http/modules/ngx_http_xslt_filter_module.c
    
  5. by content
    git log -S “ngx_free” # 查看所有文件中包含了ngx_free字符串的修改
    
  6. by range
    git log <since>..<until>
    

For example, git log master...feature This can show all the commits on the feature branch since the fork of the master branch

3.9 git show [commit_id]

Display commit_id's submission content, including modification information of all files
insert image description here

Guess you like

Origin blog.csdn.net/weixin_52665939/article/details/130177699