How to handle common errors in git

Tram Ho

First want to fix the error in git we have to create it first. So part 1 I will say some basic commands in git follow follow create and use a project.

The basic commands

1. Initialize git

Surely everyone knows that want to create a repo we use the command

2. Add a new remote to the project

3. Clone project

You discovered that you already have the url of the project already, so why bother creating git again. Use clone instead

4. Working with branches

  • Git is a distributed version management system, so its model is like a tree. So you may recognize that each tree always has a main trunk, and branches and small branches. Similarly in git we have branch representing each small branch of a tree and the main branch equivalent to the trunk is often called the master. If you’ve ever worked on real projects, you often see that the master branch will be an empty branch, instead developers use branches like development, product, test, etc. This makes it easier and more transparent for developers to analyze and manipulate corresponding environments through the software development process.
  • Some basic commands

5. Merge branch

This is one of the most commonly used basic statements of the process. But with support tools like github, gitlab, … the use of this command is only local.

6. Update to the latest code

When working alone, you will not need git, but when working in a team and want to get the latest code from a branch on an online repo, you will use the following command:

7. see status and a few more things

Common problems in Git

1. Write wrong message commit

To change the content of the commit, adding and editing files in the commit is very simple, you just need to use.

2. Incorrect branch name

Typing or setting a component by convention when naming a branch is not uncommon. When you first approach you often choose the solution is to create a new branch. There is a much faster way like rename in the files that work with the command:

3. Commit plays and now wants it to fly

This dumb job happens often during work, so how to code the data and delete the commit, first you have to learn some of the following theories: HEAD, Working tree, Index in git is what?

  • Git holds a special pointer named HEAD. In Git, this is a pointer to the local branch you’re working on
  • On Git, the directories placed under Git’s management that people are doing in practice are called working trees.
  • Between the repository and the working tree, there is a place called index. Index is a place to prepare for committing to the repository

Method 1: Use reset (when no one has pulled back)

git reset HEAD ~ Move the HEAD to the commit reset location and keep all changes to the file, but remove the changes from the stage (Bring HEAD and index back to the original).

git reset –hard HEAD ~

  • Move the HEAD cursor to the reset commit location and remove all file changes after the commit reset time
  • HEAD ~ has the same meaning as HEAD ^ or @ ^, meaning it goes back 1 commit
  • If you want to go back before n commits, for example, 5 commits can be replaced by HEAD ~ 5.
  • –hard means to remove commits and uncommitted changes in the working space. This environment will be completely “clean” as before committing. git reset –soft HEAD ~
  • This command only moves HEAD to the commit reset position. The state of the stage and all file changes will be preserved
  • –soft means to abort the commits but keep uncommitted changes in working space.
  • –soft is useful when you want to keep uncommitted changes for the next commit

Method 2: Use revert (others have pulled back)

Method 3: Use –amend

4. When you accidentally commit a wrong branch to another branch

There are times when we accidentally commit to the master when we actually want to commit to another branch

5. Merge multiple commits into one

Use rebase + squash (or fixup) to summarize old commits

  • Squash: between the original commit message and the previous commit
  • Fixup: delete the commit message from the previous commit

6. Divide large commits into several small commits

7. Move another branch while you’re working

8. Recover important commits when you accidentally delete them

This must be the most terrible incident. It can happen by accidentally git reset –hard. However, any commits can be recovered

9. Restore deleted branch

10. Restore to status before merging

Note: Only for cases where others have not yet pulled the code

11. Restore to status before pulling from the remote

After I pulled the update code but found that there was a lot of conflict, I wanted to go back to the way it was

12. When you want to rename 1 branch both locally and on remote (github)

13. When you want to delete a branch on the remote

That’s the basics and how to fix some common mistakes when working with your git. I hope you can contribute to improve this article and the following post more. ?

Share the news now

Source : Viblo