When you're working with a lot of people on a project, it's important to keep a clean and clear Git history. In this article, I will introduce some simple Git commands that will help you.
#Why is a clean Git history important?
When it comes to reading your Git history, it's important to be able to easily find what you're looking for and also be able to fix it quickly.
For example, if you need to roll back a function, you probably only want to roll back one commit (same for special selections)... If you look at the commits of a merge request, it's much easier to understand how (the steps) the function was designed , if it hasn't been polluted by unnecessary commits... If you want to find the source of a bug, it's much easier if the commit contains all the changes made related to that bug. to examine multiple small commits.
#Fusion vs. Rebase
When it comes to merging one branch into another, there are two common solutions:merge git
ÖGit-Rebase
. I'll try to explain what the main differences are and how to use them.
First, suppose we are in the following situation: We have aMaestro
Branch that is public and we created onespecial feature
branch from it (common commits are blank in the image). In your feature branch you have added 3 new commits (in green). And the master branch now has 2 new commits (in blue). So we are in a classic situation where your branch is out of date with master but you need these 2 new commits of master in your feature branch...
#merge git
With the merge option, just use one of the following:
gitgit Merge Master Checkout Function
Ö
Git-Merge-Feature-Master
It will merge the main branch into its feature branchCreating a new commitmenton its characteristic branch (green circle with star).
So far so good 😀 ... if your git history doesn't bother you. Let's imagine the master branch receives new commits regularly and needs them: you have to do multiple merges creating a new commit for each merge... Also, your commits in the feature branch can be split and classified as "disappearing" Teacher Confirmations. It can quickly become confusing.
#Git-Rebase
Use: is highly recommendedrebase only on private branches. If you share your authority with someone else (e.g. a public authority),You must avoid passingas it can be messy and a bit risky, or do it very carefully and warn your teammates.
In fact, rebase will rewrite git's history by changing the base of your branch: it starts at the end of the main branch (or the branch, that ispass). As you can see in the image below, our branch is now starting after the 2 new main commits and our 3 commits have been rewritten butno merge commit made!
The rebase command is as simple as the merge command:
gitgit rebase master Checkout Function
After you run it, all your commits will be moved one by one. This means that each of them can create a conflict that you must resolve. If there are any conflicts, the rebase will stop for you to fix. When they are resolved, all you have to do is add your changes and run themgit rebase --continue
.
Since you've modified the git history (which is not a trivial operation), to commit your changes you need to force commit by executinggit push --force
Ögit push -f
.
#spot cleaning
Note that these changes should only be made if your branch is not public: you are the only developer working on it.. If this is not the case, then great care must be taken as you (or your teammates) may lose some work...
#Change your last commit (change)
Git makes it easy to change your last commit. It could be the message or even confirmation of its content. This is useful when you realize that you made a small mistake somewhere, edited some code review comments, forgot to add some files, etc. You can do it thanks to the following command:
git commit --edit
Running it will open the editor and let you edit your last commit message. If you want to change the contents of your old commit, simply add or remove the files you want to track before running the modify command.
This command accepts a few additional options that you may find useful. For example, you can change your last confirmation message directly by doing the following:
git commit --edit -m"My Changed Confirmation Message"
And on the other hand, if you know you're not going to change your commit message, you can run:
git commit --amend --no-edit
By doing so, you rewrote the history of git. If you try to push it, Git will prevent you from doing so because it conflicts with the remote branch. You must add the force option by doinggit push --force
Ögit push -f
.
#Rebase (Squash) Seus-Commits
To change the history of one of your private branches, you can change the base of your own branch; to crush, edit, reorder, delete or refactor your commits. And good news, you can do it in oneinteractiveAway! Just do one of the following:git rebase -i <Hash>
(hash = the hash of the commit you want to rebase) orgit rebase -i HEAD~<X>
(X = the number of commits you want to reorder.)
When you do this, your text editor will show something similar to:
select 58cd2a3 create function 1 select 3362add create function 2 select ddab253 correct typo of function 1 but edit commit message #e, edit = use commit but stop modifying #s, squash = use commit but include the previous commit together #f, correction = like"Pumpkin", but delete log message for this commit # x, exec = execute command (rest of line) with shell # d, drop = discard commit # # These lines can be reordered; They run from top to bottom. # # If you delete a line here, THE COMMIT WILL BE LOST. # # However, if you remove everything, the rebase will be aborted. # # Note that empty commits are commented out
If you want to change a commit, you need to replace the prefix word "choose" with one of the suggested commands. In our example, we see that we made 3 commits. 2 of which refer to a resource 1 (#58cd2a3&#ddab253) and the other relates to function 2 (Add #3362). The problem here is that we couldn't change our first commit to fix a typo because there was a commit that referenced feature 2 in the middle.
So the idea here might be to change the order of the commits and merge (shred) our input commit into function commit 1. In your editor you can edit it like this:
select 58cd2a3 create function 1 squash ddab253 correct function 1 typo select 3362add create function 2
After saving, rescheduling begins and conflicts may occur, just like normal rescheduling. When the rebase is complete, you will be prompted to enter the new commit message for the squashed commits.
Since Git history has changed, you need to force the changes here as wellgit push --force
Ögit push -f
.
Our example is quite simple, but if necessary, you can make several pumpkins at once.
#🎁 Bonus: Git up and running
As you may have noticed, reorganizing can be a bit of a chore because you may face a lot of conflicts (if you don't reorganize regularly) and these conflicts can recur... To help you resolve these recurring conflicts, you can use theflightgit functionality. means rerereErasuseErascutErasSolution.
By default it is disabled in Git and you can enable it by running:
git config --global rerere.enabled true
And this is! When a conflict occurs, git takes a snapshot of that conflict and saves its resolution as well. So the next time you see it, Git will be able to resolve it automatically. It will give you something like this in your console:
CONFLICT(content): Resolved merge conflict in index.html'index.html'using the previous resolution. Auto merge failed; Fix the conflicts and then transfer the result.
As you can see, Git automatically resolved the conflict inindex.html
but this file is not prepared automatically: you have to make onegit agregar index.html
...If you prefer, you can tell git to automatically organize the files you resolve by updating the configuration:
git config --global rerere.autoupdate verdadero
Now, if you're faced with an already-resolved conflict, Git will automatically resolve and prepare for it. That means you still have to commit and push them 😉.
#More information and inspiration
- Fusion vs. Rebase
- rewrite history
- Always minify and rebase your git commits
- A beginner's guide to rebasing and squashing Git
- Why you should care about Squash and Merge in Git
- Git-Tools - Rerere
- Resolve conflicts only once with git rerere
🙌 I want to say thank youValentin LetourneurjBoris Gueryfor your help on this article!