Git: fixing bugs and ordering commits

Original author: Aditya Sridhar
  • 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.


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-editmeans 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 amendonly 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.


The history of commits in the local repository.

How to fix the history of commits? To solve this problem, you can use the commandgit rebase.

▍ git rebase command


Consider the command git rebaseby example.


Commits to the branch and the branch Release Feature

The branch Releasehas three commits: Rcommit1, Rcommit2, and Rcommit3. You created your branch Featurefrom a branch Releasewhen there was only one commit in it - Rcommit1. After that you added two commits to the branch Feature. This is - Fcommit1and Fcommit2. Your goal is to upload commits from a branch Releaseto your branch Feature. In order to do this, you are going to use the command rebase.

We will use the names releaseand for the two considered branches feature.

As a result, the use of the command rebasewill look like this:

git checkout feature
git rebase release

▍ Rebase command features


The command is rebaseused to ensure that the branch Featurecontains fresh code from the branch Release.

When using this command, the system tries to add Featureeach 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.


Feature branch and three steps of the rebase command

Step 1


At the moment of calling the command, the branch Featurepoints to the head of the branch Release. After that, the branch Featureis three commits: Rcommit1, Rcommit2, and Rcommit3. Perhaps here you will have a question about what happened to the commits Fcommit1and 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 Fcommit1to a branch Feature. In the absence of conflict is Fcommit1added after Rcommit3. If a conflict is detected, Git will report this and you will have to resolve this conflict manually.

Step 3


Once Featurea 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 Fcommit2added after Fcommit1and 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 rebasewill be seen that the branch Featurehas checkins Rcommit1, Rcommit2, Rcommit3, Fcommit1, and Fcommit2.

▍Note


When working with Git, both the command mergeand the command are used rebase. It cannot be said that one of them is preferable to the other.

If you use the command, mergeyou will receive a commit merge. If you use rebaseany additional commits you will not have.

It is recommended to use these commands in various situations. So, it rebaseis suitable for updating the local repository code on the basis of fresh code from a remote repository. Use the command mergeby executing pull requests to merge a branch Featurewith a branch Releaseor 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?


Also popular now: