DEV Community

Cover image for Problems faced by GIT beginners
Fidal Mathew
Fidal Mathew

Posted on • Updated on

Problems faced by GIT beginners

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
Let's initialize the project with -
git init
git add .
git commit -m “first commit”

If we check git log then ->
first commit

I made some changes -

The file is modified, now
git add .
git commit -m “ add ironman”
add ironman
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

(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
Commit list -
git log
git log

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

git checkout fix1

git checkout fix1
Let’s fix it and make a git commit.

commit in fix1

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

error
Now, this is exactly what happens when you send a pull request on a file that has been modified.

merge conflict
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
After fixing the conflict just -
git add .
git commit

final

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
and add some changes.
hello
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 -- .
After command -

hello

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
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

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

revert

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

merge error

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 -

Discussion (0)