Git: fixing bugs and ordering commits
- Transfer
Error in commit ... How to fix it? Confusion in the history of commits ... How to bring everything into a decent look? The author of the article, the translation of which we publish today, says that it was written specifically for those who asked such questions. According to him, having studied the techniques of working with Git, presented here, you can significantly advance along the path of mastering Git.
![](https://habrastorage.org/getpro/habr/post_images/349/17b/969/34917b969193928c586023f0b971ae38.png)
It is assumed that the reader of this article is already familiar with the basics of Git. If this is not the case, it is first recommended to master the base, for example, using this material.
Here we consider several scenarios for the appearance of errors in commits and their correction.
Suppose you commit many files and understand that the commit message was not very clear. After that you decided to change this message. In order to do this, use the command
Suppose you wanted to commit six files, but, by mistake, commit only five. It seems that you can correct this error simply by creating a new commit and adding the missing sixth file there.
Such an approach is completely entitled to life. But in order to keep the commit history in good condition, it would probably be much better to be able to add a randomly missed file to the same commit. You can do this, again, as a command
A flag
To the commits that make in Git, the name of the author and his email address are attached. Usually this data is indicated by performing the initial Git setup. As a result, the one who uses Git may, by performing each commit, not worry about the details of its author.
In this case, it is quite possible that, when working with some projects, you will need to use information about the author, for example, an email address that is different from the main ones. For such a project, you must specify the email address using the following command:
Suppose you forget to do this setup and have already made the first commit. Correct the situation can already familiar to us the team
Use the command
Suppose you are working on a piece of code for a project. You know that it will take about ten days. During these ten days, other developers commit to the source repository.
It is recommended to maintain synchronization between local and remote repositories. This avoids many of the merge conflicts that occur when synchronization of repositories is too rare. Following this practice, you decide to download changes from a remote repository every two days.
Each time you download code from a remote to a local repository, a new merge commit is created in the local repository. This means that in your local commit history there will be many such commits that can confuse the person who will view your code.
![](https://habrastorage.org/getpro/habr/post_images/ac5/abf/43b/ac5abf43b0e1f38fec80193de0db8b45.jpg)
The history of commits in the local repository.
How to fix the history of commits? To solve this problem, you can use the command
Consider the command
![](https://habrastorage.org/getpro/habr/post_images/5ff/01b/8a7/5ff01b8a77ee67f99dc8a807ff81defb.jpg)
Commits to the branch and the branch Release Feature
The branch
We will use the names
As a result, the use of the command
The command is
When using this command, the system tries to add
![](https://habrastorage.org/getpro/habr/post_images/b9d/375/b10/b9d375b102b136e99d128571d6d6a562.jpg)
Feature branch and three steps of the rebase command
At the moment of calling the command, the branch
Now Git is trying to add a commit
Once
After completion of the team's work
When working with Git, both the command
If you use the command,
It is recommended to use these commands in various situations. So, it
After examining the concepts presented in this material, you have improved your Git skills and come closer to the level of a Git expert. We hope that what you have learned here will be useful to you. But the world of Git is huge, therefore, having mastered something new, do not stop there and move on.
Dear readers! Did you encounter confusion in git commits?
![](https://habrastorage.org/files/1ba/550/d25/1ba550d25e8846ce8805de564da6aa63.png)
![](https://habrastorage.org/getpro/habr/post_images/349/17b/969/34917b969193928c586023f0b971ae38.png)
It is assumed that the reader of this article is already familiar with the basics of Git. If this is not the case, it is first recommended to master the base, for example, using this material.
Correction of errors in commits
Here we consider several scenarios for the appearance of errors in commits and their correction.
▍Scenario number 1
Suppose you commit many files and understand that the commit message was not very clear. After that you decided to change this message. In order to do this, use the command
git commit --amend
. Here is an example of its use:git commit--amend -m "New commit message"
▍Scenario number 2
Suppose you wanted to commit six files, but, by mistake, commit only five. It seems that you can correct this error simply by creating a new commit and adding the missing sixth file there.
Such an approach is completely entitled to life. But in order to keep the commit history in good condition, it would probably be much better to be able to add a randomly missed file to the same commit. You can do this, again, as a command
git commit --amend
. Its use looks like this:git add file6
git commit--amend --no-edit
A flag
--no-edit
means that the commit message does not change.▍Scenario number 3
To the commits that make in Git, the name of the author and his email address are attached. Usually this data is indicated by performing the initial Git setup. As a result, the one who uses Git may, by performing each commit, not worry about the details of its author.
In this case, it is quite possible that, when working with some projects, you will need to use information about the author, for example, an email address that is different from the main ones. For such a project, you must specify the email address using the following command:
git config user.email "your email id"
Suppose you forget to do this setup and have already made the first commit. Correct the situation can already familiar to us the team
amend
. With its help, you can change information about the author of the previous commit:git commit--amend --author "Author Name <Author Email>"
▍Note
Use the command
amend
only in your local repository. Using it in remote repositories can be a lot of confusion.Bringing order to commit history
Suppose you are working on a piece of code for a project. You know that it will take about ten days. During these ten days, other developers commit to the source repository.
It is recommended to maintain synchronization between local and remote repositories. This avoids many of the merge conflicts that occur when synchronization of repositories is too rare. Following this practice, you decide to download changes from a remote repository every two days.
Each time you download code from a remote to a local repository, a new merge commit is created in the local repository. This means that in your local commit history there will be many such commits that can confuse the person who will view your code.
![](https://habrastorage.org/getpro/habr/post_images/ac5/abf/43b/ac5abf43b0e1f38fec80193de0db8b45.jpg)
The history of commits in the local repository.
How to fix the history of commits? To solve this problem, you can use the command
git rebase
.▍ git rebase command
Consider the command
git rebase
by example. ![](https://habrastorage.org/getpro/habr/post_images/5ff/01b/8a7/5ff01b8a77ee67f99dc8a807ff81defb.jpg)
Commits to the branch and the branch Release Feature
The branch
Release
has three commits: Rcommit1
, Rcommit2
, and Rcommit3
. You created your branch Feature
from a branch Release
when there was only one commit in it - Rcommit1
. After that you added two commits to the branch Feature
. This is - Fcommit1
and Fcommit2
. Your goal is to upload commits from a branch Release
to your branch Feature
. In order to do this, you are going to use the command rebase
. We will use the names
release
and for the two considered branches feature
. As a result, the use of the command
rebase
will look like this:git checkout feature
git rebase release
▍ Rebase command features
The command is
rebase
used to ensure that the branch Feature
contains fresh code from the branch Release
. When using this command, the system tries to add
Feature
each commit to the branch , one at a time, and check for conflicts. If it sounds difficult - let's consider the following figure. It shows the internal mechanisms of the team rebase
. ![](https://habrastorage.org/getpro/habr/post_images/b9d/375/b10/b9d375b102b136e99d128571d6d6a562.jpg)
Feature branch and three steps of the rebase command
Step 1
At the moment of calling the command, the branch
Feature
points to the head of the branch Release
. After that, the branch Feature
is three commits: Rcommit1
, Rcommit2
, and Rcommit3
. Perhaps here you will have a question about what happened to the commits Fcommit1
and Fcommit2
. These commits have not gone away; they will be used in the next steps.Step 2
Now Git is trying to add a commit
Fcommit1
to a branch Feature
. In the absence of conflict is Fcommit1
added after Rcommit3
. If a conflict is detected, Git will report this and you will have to resolve this conflict manually.Step 3
Once
Feature
a commit has been added to a branch Fcommit1
, Git tries to add to the same place Fcommit2
. Here, again, if there are no conflicts, then it is Fcommit2
added after Fcommit1
and the operation is successfully completed. If a conflict is detected, then Git, as before, will report this and offer to deal with it. After completion of the team's work
rebase
will be seen that the branch Feature
has checkins Rcommit1
, Rcommit2
, Rcommit3
, Fcommit1
, and Fcommit2
.▍Note
When working with Git, both the command
merge
and the command are used rebase
. It cannot be said that one of them is preferable to the other. If you use the command,
merge
you will receive a commit merge. If you use rebase
any additional commits you will not have. It is recommended to use these commands in various situations. So, it
rebase
is suitable for updating the local repository code on the basis of fresh code from a remote repository. Use the command merge
by executing pull requests to merge a branch Feature
with a branch Release
or Master
.Results
After examining the concepts presented in this material, you have improved your Git skills and come closer to the level of a Git expert. We hope that what you have learned here will be useful to you. But the world of Git is huge, therefore, having mastered something new, do not stop there and move on.
Dear readers! Did you encounter confusion in git commits?
![](https://habrastorage.org/files/1ba/550/d25/1ba550d25e8846ce8805de564da6aa63.png)