search
HomeDevelopment Toolsgit3 moves to get it done! Keep a clean Git commit record

This article brings you knowledge about keeping clean Git commit records, including "git commit –amend", "git rebase -i" and "rebase" Question, hope it helps everyone.

3 moves to get it done! Keep a clean Git commit record

## Recommended study: "

Git Tutorial"

Everyone has learned how to write code in a standardized and concise way

, but rarely learn how to standardize and concise submit code. Nowadays, everyone basically uses Git as a source code management tool. Git provides great flexibility. We submit/merge code according to various workflows. If this flexibility is not well controlled, it will also cause many problems

The most common problem is the messy git log history. It is really an old lady's foot wrap, smelly and long. I personally dislike this kind of log

3 moves to get it done! Keep a clean Git commit record

The root cause of this problem is submitting code at will.

The code has been submitted, is there any way to save it? Three tips can solve the problem perfectly

Make good use of git commit –amend

The help document of this command is described like this:

--amend               amend previous commit
In other words, it It can help us modify

The last submission

can modify not only the message we submitted, but also the file we submitted, and finally replace the last commit-id

We may miss a certain file during a certain submission. When we submit again, there may be a useless commit-id. If everyone does this, the git log will gradually become too messy to track the complete function

Suppose we have such a piece of log information

* 98a75af (HEAD -> feature/JIRA123-amend-test) feat: [JIRA123] add feature 1.2
* 119f86e feat: [JIRA123] add feature 1.1
* 5dd0ad3 feat: [JIRA123] add feature 1
* c69f53d (origin/main, origin/feature/JIRA123-amend-test, origin/HEAD, main) Initial commit
Suppose we want to modify the last log message, we can use the following command:

git commit --amend -m "feat: [JIRA123] add feature 1.2 and 1.3"
Let’s take a look at the log information again, we can find , we replaced the old commit-id 98a75af with the new commit-id 5e354d1, modified the message, and did not add nodes

* 5e354d1 (HEAD -> feature/JIRA123-amend-test) feat: [JIRA123] add feature 1.2 and 1.3
* 119f86e feat: [JIRA123] add feature 1.1
* 5dd0ad3 feat: [JIRA123] add feature 1
* c69f53d (origin/main, origin/feature/JIRA123-amend-test, origin/HEAD, main) Initial commit
Now the files in our repo look like this:

.
├── README.md
└── feat1.txt

0 directories, 2 files
Suppose that when we submitted feature 1.3, we forgot a configuration file config.yaml and did not want to modify the log or add a new commit-id. Then the following command is very easy to use

echo "feature 1.3 config info" > config.yaml
git add .
git commit --amend --no-edit
git commit -- amend --no-edit is the soul. Let’s take a look at the current repo file:

.
├── README.md
├── config.yaml
└── feat1.txt

0 directories, 3 files
Let’s take a look at git log

* 247572e (HEAD -> feature/JIRA123-amend-test) feat: [JIRA123] add feature 1.2 and 1.3
* 119f86e feat: [JIRA123] add feature 1.1
* 5dd0ad3 feat: [JIRA123] add feature 1
* c69f53d (origin/main, origin/feature/JIRA123-amend-test, origin/HEAD, main) Initial commit
Knowing this technique, we can ensure that every submission we make All contain valid information. A picture describing the process looks like this:

3 moves to get it done! Keep a clean Git commit record

With the buff bonus of --no-edit, it is more powerful

Make good use of it git rebase -i

You can see that the above logs are all in the development of feature1. Before merging the feature branch into the main branch, we should continue to merge the log commit nodes. This is used

git rebase -i HEAD~n
where n represents the last few submissions. We have three submissions for feature 1 above, so we can use:

git rebase -i HEAD~3
After running, a vim editor will be displayed with the following content:

 1 pick 5dd0ad3 feat: [JIRA123] add feature 1
 2 pick 119f86e feat: [JIRA123] add feature 1.1
 3 pick 247572e feat: [JIRA123] add feature 1.2 and 1.3
 4
 5 # Rebase c69f53d..247572e onto c69f53d (3 commands)
 6 #
 7 # Commands:
 8 # p, pick <commit> = use commit
 9 # r, reword <commit> = use commit, but edit the commit message
10 # e, edit <commit> = use commit, but stop for amending
11 # s, squash <commit> = use commit, but meld into previous commit
12 # f, fixup <commit> = like "squash", but discard this commit's log message
13 # x, exec <command> = run command (the rest of the line) using shell
14 # d, drop <commit> = remove commit
15 # l, label <label> = label current HEAD with a name
16 # t, reset <label> = reset HEAD to a label
17 # m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
18 # .       create a merge commit using the original merge commit's
19 # .       message (or the oneline, if no original merge commit was
20 # .       specified). Use -c <commit> to reword the commit message.
21 #
22 # These lines can be re-ordered; they are executed from top to bottom.
23 #
24 # If you remove a line here THAT COMMIT WILL BE LOST.
25 #
26 #   However, if you remove everything, the rebase will be aborted.
27 #
28 #
29 # Note that empty commits are commented out</commit></oneline></label></commit></commit></label></label></commit></command></commit></commit></commit></commit></commit>
The most commonly used methods for merging commit-ids are squash and fixup. The former contains commit message, while the latter does not. Use fixup here, and then :wq to exit

1 pick 5dd0ad3 feat: [JIRA123] add feature 1
2 fixup 119f86e feat: [JIRA123] add feature 1.1
3 fixup 247572e feat: [JIRA123] add feature 1.2 and 1.3
Let’s take a look at the log again, it’s very clear

* 41cd711 (HEAD -> feature/JIRA123-amend-test) feat: [JIRA123] add feature 1
* c69f53d (origin/main, origin/feature/JIRA123-amend-test, origin/HEAD, main) Initial commit
Make good use of rebase

The above feature1 has been completely developed, and the main branch has also been updated by others. Then merge the feature back to the main branch to prevent code conflicts. You need to merge the contents of the main branch into the feature first. If you use the merge command, there will be an extra merge node, and there will also be an inflection point in the log history, which is not linear, so here we can use the rebase command on the feature branch

git pull origin main --rebase

3 moves to get it done! Keep a clean Git commit record

The pull command automatically helps us merge, but here in the form of rebase, let’s take a look at the log

* d40daa6 (HEAD -> feature/JIRA123-amend-test) feat: [JIRA123] add feature 1
* 446f463 (origin/main, origin/HEAD) Create main.properties
* c69f53d (origin/feature/JIRA123-amend-test, main) Initial commit
our feature1 function on The submission node of the top of main still remains linear. Next, you can push the code, then submit a PR, and merge your feature to the main branch.

A brief description of the difference between merge and rebase is this:

3 moves to get it done! Keep a clean Git commit record

I use git pull origin main --rebase here to omit the process of switching main and pulling the latest content and then switching back. It is done in one step. The principles behind it are all shown in the picture above.

Using rebase is There is a golden rule that must be followed. This has been said before, so I won’t go into details.

Summary

With these three tips, I believe everyone’s git log will be extremely clear. If you don’t know it yet, you can definitely use it. If your group members don’t know it, you can definitely promote it. This kind of repo will look healthier.

Recommended study: "Git Tutorial

The above is the detailed content of 3 moves to get it done! Keep a clean Git commit record. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:CSDN. If there is any infringement, please contact admin@php.cn delete
Git and GitHub: Exploring Their Roles and FunctionsGit and GitHub: Exploring Their Roles and FunctionsMay 09, 2025 am 12:25 AM

The role and function of Git and GitHub in software development is to manage code and collaborative development. Git efficiently manages code versions through commit, branch and merge functions, while GitHub provides code hosting and collaboration tools such as PullRequest and Issues to improve team collaboration efficiency.

GitHub: Discovering, Sharing, and Contributing to CodeGitHub: Discovering, Sharing, and Contributing to CodeMay 08, 2025 am 12:26 AM

GitHub is the preferred platform for developers to discover, share and contribute code. 1) Find specific code bases through search functions, such as Python projects. 2) Create a repository and push code to share with developers around the world. 3) Participate in open source projects and contribute code through Fork and PullRequest.

Using Git with GitHub: A Practical GuideUsing Git with GitHub: A Practical GuideMay 07, 2025 am 12:11 AM

Git is a version control system, and GitHub is an online platform based on Git. The steps to using Git and GitHub for code management and team collaboration include: 1. Initialize the Git repository: gitinit. 2. Add files to the temporary storage area: gitadd. 3. Submit changes: gitcommit-m"Initialcommit". 4. Related to the GitHub repository: gitremoteaddoriginhttps://github.com/username/repository.git. 5. Push code to GitHub: gitpush-uoriginmaste

GitHub's Impact: Software Development and CollaborationGitHub's Impact: Software Development and CollaborationMay 06, 2025 am 12:09 AM

GitHub has a far-reaching impact on software development and collaboration: 1. It is based on Git's distributed version control system, which improves code security and development flexibility; 2. Through functions such as PullRequest, it improves team collaboration efficiency and knowledge sharing; 3. Tools such as GitHubActions help optimize the development process and improve code quality.

Using GitHub: Sharing, Managing, and Contributing to CodeUsing GitHub: Sharing, Managing, and Contributing to CodeMay 05, 2025 am 12:12 AM

The methods of sharing, managing and contributing code on GitHub include: 1. Create a repository and push code, and write README and LICENSE files; 2. Use branches, tags and merge requests to manage code; 3. Fork the repository, modify and submit PullRequest contribution code. Through these steps, developers can effectively use GitHub to improve development efficiency and collaboration capabilities.

Git vs. GitHub: A Comparative AnalysisGit vs. GitHub: A Comparative AnalysisMay 04, 2025 am 12:07 AM

Git is a distributed version control system, and GitHub is a Git-based collaboration platform. Git is used for version control and code management, while GitHub provides additional collaboration features such as code review and project management.

Git vs. GitHub: Understanding the DifferenceGit vs. GitHub: Understanding the DifferenceMay 03, 2025 am 12:08 AM

Git is a distributed version control system, and GitHub is an online platform based on Git. Git is used for version control, branch management and merger, and GitHub provides code hosting, collaboration tools and social networking capabilities.

GitHub: The Frontend, Git: The BackendGitHub: The Frontend, Git: The BackendMay 02, 2025 am 12:16 AM

Git is a back-end version control system, and GitHub is a front-end collaboration platform based on Git. Git manages code version, GitHub provides user interface and collaboration tools, and the two work together to improve development efficiency.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.