Problems faced by GIT beginners
Published: 2021-12-06 18:54:48



Hi folks, hope you are doing well. In this post I am going to share you some explain some concepts and methods that will help you while using git.

**I am gonna talk about -**
**1. Git conflicts**
**2. Git undo methods**

## 1. Git merge conflicts -

Many developers fear when git merge conflict happens, I was one of them too until I knew why it happened.

So, what causes Git merge conflict?

Let me tell you via an example -

Let us take an empty folder and make a file named Fidal.txt
![initialize](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sg9jbf2u2y87486jrkw7.png)
Let's initialize the project with -
`git init`
`git add .`
`git commit -m “first commit”`

If we check `git log` then ->
![first commit](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p9jnq888zrzv6aat60r2.png)


I made some changes -

The file is modified, now
`git add .`
`git commit -m “ add ironman”`
![add ironman](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s22ih7enustywinazuok.png)
Now read carefully,
consider the **master** branch as the main branch of the repo you want to contribute. All the changes will eventually be committed there.

Now, I want to work on the issue of making 
“Iron man” text into “Iron Man” (both capital).
I made a new branch called **“fix1”**

`git checkout -b fix1`  

But other members (assume, but in this example I will be portraying them) were simultaneously making commits in the master branch. 

![fix1 branch](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8iugq28iyih268rpjpnk.png)

(If you want to follow up)

I added "2. Captain America"
         "3. Thor" ...so on
Added all the files and made a commit.
This process went on.

Currently, the master branch looks like - 


![master branch](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qznrqrwehkucqc1llb15.png) 
Commit list -
`git log`
![git log](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ln1pvj0tggg0c6p1pty8.png)
 

The fix hasn’t been merged yet -
So, let’s go to fix1 branch and fix the issue.
 
`git checkout fix1`

![git checkout fix1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ijoj7c3u3dw2mfk6jofo.png)
Let’s fix it and make a git commit. 


![commit in fix1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0vjdurq9ul47m1v4i8q0.png)


Now go to the “master” branch and “merge fix1” branch -
`git checkout master`
`git merge fix1`

![error](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wb9l764pz34bah5tj29t.png)
Now, this is exactly what happens when you send a pull request on a file that has been modified.


![merge conflict](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y7r549t5dpdlxkwtz0nz.png)
Merge conflict happens because Git gets confused on which commit to choose.
So now, you have to make the changes in the document manually.

I manually adjusted the code and finally came up with this. 

![merge success](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i2w63ttnihlk6ko0urxn.png)
After fixing the conflict just -
`git add .`
`git commit `


![final](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nqm1f7h5by29v1msgcrm.png)

I hope you got clarity on how git merge conflicts happen and how to solve them if you get one.



## 2. Undo with Git Commands

There are many times when I need to revert back to the previous commit or unstage the selected files.

### So, here are the ways you can do it - 

First let’s initialize an empty git repository.

![init](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2y0uld53wvl3fgrvgjdi.png)
and add some changes.
![hello](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pxyc9j5mqrr7i0jg2zjq.png)
and make a commit.
### 1.Reverting unstaged file -

Suppose you added some lines here and there and want those lines to be removed. 
The file is **modified(not staged)** and has unnecessary lines.
USE -` git checkout -- .  ` or `git checkout -- filename`

![git checkout -- .](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0ztet179eyr485b247ws.png)
After command -

![hello](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pxyc9j5mqrr7i0jg2zjq.png)

### 2.Unstage the staged file -

Sometimes, after you have made changes and added the changes using `git add -A`
If you want to unstage it  then 
use -> ` git restore --staged . `
The file will keep the changes but remove them from the staging area.
![restore](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/svn3er1lsq516jlq1glq.png) 
If you want now you can use `git checkout -- .` to go back to the last commit.

OR

If you want to **unstage** it and go back to commit 

Use -> ` git reset --hard ` if file is modified.

 
### 3.Undo commits -

I made 3 commits - 1 =“ Hello I am fine”
			2 =”How are you”
			3 =” I am fine”


![log](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pk7fr2h8g32lnqsh19rf.png)

Suppose I want to go back to 1st commit 
I have to revert the 2nd commit 
Use - `git revert commit id-2`

![revert](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4iu4rcqiamg52nwyzz89.png)


A merge conflict has occurred, simply choose the incoming change. 


![merge error](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0sf43m4kygs7j28yfucd.png)

`git add .` command will add the changes to the staging area and get rid of the conflict sign.
Now, you can do any changes you need.

If you want to save the previous state changes-

`git reset commit_id-x file_name`
Use this to undo changes on a single file or directory from commit x, but retain them in the staged state.

`git checkout commit_id-x file_name`
To undo changes on a single file or directory from commit x, but retain them in the unstaged state.

I would recommend you to try these commands, you will learn way faster. 
If you would like to add any other commands that I missed out on, do let me know in the comments. 
Thank you for reading :)

**Connect with me -**
* [Twitter](https://twitter.com/fidalmathew10)
* [Github](https://github.com/FidalMathew)
* [Linkedin](https://www.linkedin.com/in/fidal-mathew-82aba7200/)