Git: common mistakes and ways to fix them

Original author: Ankur Biswas
  • Transfer
If you have ever worked on a large project in which, besides you, many other programmers are involved, then you obviously used Git as a version control system. In the course of using something that is similar in complexity to Git, everyone makes mistakes.


The author of the material, the translation of which we publish today, is going to discuss common errors that programmers make when working with Git, and talk about how to deal with these errors.

Error in last commit message


After a few hours of good work, it's easy to make a mistake in the commit message. Fortunately, this is easily fixed with the following command:

git commit--amend

This command will open the editor and allow you to make changes to the latest commit message. No one except you should know that you wrote an “Initial commment” with three “m”.

Error in the name of the branch


Suppose that the clock is almost 15:00, and you have not had dinner yet. As a result, tormented by hunger, you called a new branch feature-brunch. Yummy, do not say anything.

This problem can also be solved. You can rename a branch in the same way that you rename files using the command mv. It is about moving it to a new place with the correct name:

git branch -m feature-brunch feature-branch

If you have already sent this thread to the repository, you need to do a couple more things to rename it. The old branch should be deleted, and the new one should be sent to the repository:

git push origin --delete feature-brunch
git push origin feature-branch

Random commit changes to the master branch


Suppose you are working on a new opportunity, and, in a hurry, you forgot to create a new branch for it. You are already a bunch of files and everything is in the thread master. You can move all these changes to the new branch using the following three commands. Please note that if you do not use, as applied to changes, commands commitor stash, they will be lost.

git branch feature-branch
git resetHEAD~ --hard
git checkout feature-branch

As a result, a new branch will be created, changes in the branch will be rolled back masterto the state in which it was before the changes were made, and a transition will be made to the new branch, which will contain all changes made earlier in master.

Work with files that you forgot to add to the last commit


Another common mistake when working with Git is that commits are done too early and they don’t get the necessary files. Let's say you missed a file, forgot to save it, or you need to make a small change to the file so that the last commit makes sense. In a situation like this, the team will come in handy again --amend. Add the missing file to the repository index and run this command:

git add missed-file.txt
git commit--amend

After that you can change the commit message, or leave it the same as it was.

Adding the wrong file to the repository


What if your mistake represents the exact opposite of the previous one? What if you added a file to the index that you are not going to commit? This could be some ENV file, project build directory, a photo of your dog that you accidentally saved in the wrong folder. All this can be fixed.

If your actions are limited to indexing a file, but you have not committed it yet, it will be very easy to deal with the problem using the command reset:

git reset /assets/img/misty-and-pepper.jpg

If you have advanced far enough and managed to commit change, then know that this is fixable. We'll just have to use a few more commands:

git reset--soft HEAD~1
git reset /assets/img/misty-and-pepper.jpg
rm /assets/img/misty-and-pepper.jpg
git commit

As a result, the last commit will be canceled, the image will be deleted, after which the new commit will be placed where it should be.

What if everything went wrong?


The technique that we will discuss now helps in situations where everything goes wrong. For example, this happens when you are too keen on copying ready-made solutions from StackOverflow, and, after work, your repository is in a worse state than it was at the very beginning. In this situation, perhaps, we all fell.

The command git reflogshows a list of all that you have done. It then allows you to use the Git tools to revert changes, to return to one of the past repository states. It is worth noting that this method should be considered as a last resort, and, before using it, it is worthwhile to think about it. So, the list of what has been done can be displayed with the following command:

git reflog

Git remembers all of our actions and as a result of this command, you can see something like the following:

3ff8691 (HEAD -> feature-branch) HEAD@{0}: Branch: renamed refs/heads/feature-brunch to refs/heads/feature-branch
3ff8691 (HEAD -> feature-branch) HEAD@{2}: checkout: moving from master to feature-brunch
2b7e508 (master) HEAD@{3}: reset: moving to HEAD~
3ff8691 (HEAD -> feature-branch) HEAD@{4}: commit: Adds the client logo
2b7e508 (master) HEAD@{5}: reset: moving to HEAD~137a632d HEAD@{6}: commit: Adds the client logo to the project
2b7e508 (master) HEAD@{7}: reset: moving to HEAD
2b7e508 (master) HEAD@{8}: commit (amend): Added contributing infoto the site
dfa27a2 HEAD@{9}: reset: moving to HEAD
dfa27a2 HEAD@{10}: commit (amend): Added contributing infoto the site
700d0b5 HEAD@{11}: commit: Addded contributing infoto the site
efba795 HEAD@{12}: commit (initial): Initial commit

Pay attention to the leftmost column of this list. It contains indexes. If you need to go back to a certain moment of the past, run the following command, replacing the {index}corresponding link, for example - dfa27a2. This command looks like this:

git resetHEAD@{index}

Results


We looked at some of the ways to deal with errors that arise when working with Git. We hope you will not allow such errors and these techniques of working with Git will not be useful to you. And if something goes wrong, you will know what to do.

Dear readers! Know about any interesting techniques for working with Git? If so, ask them to share.


Also popular now: