Git undo/merge issues solved with visual examples

Hi folks, hope you are doing well. In this post I am going to share you some 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 me…


This content originally appeared on DEV Community and was authored by Fidal Mathew

Hi folks, hope you are doing well. In this post I am going to share you some 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 -


This content originally appeared on DEV Community and was authored by Fidal Mathew


Print Share Comment Cite Upload Translate Updates
APA

Fidal Mathew | Sciencx (2021-12-06T18:54:48+00:00) Git undo/merge issues solved with visual examples. Retrieved from https://www.scien.cx/2021/12/06/git-undo-merge-issues-solved-with-visual-examples/

MLA
" » Git undo/merge issues solved with visual examples." Fidal Mathew | Sciencx - Monday December 6, 2021, https://www.scien.cx/2021/12/06/git-undo-merge-issues-solved-with-visual-examples/
HARVARD
Fidal Mathew | Sciencx Monday December 6, 2021 » Git undo/merge issues solved with visual examples., viewed ,<https://www.scien.cx/2021/12/06/git-undo-merge-issues-solved-with-visual-examples/>
VANCOUVER
Fidal Mathew | Sciencx - » Git undo/merge issues solved with visual examples. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/12/06/git-undo-merge-issues-solved-with-visual-examples/
CHICAGO
" » Git undo/merge issues solved with visual examples." Fidal Mathew | Sciencx - Accessed . https://www.scien.cx/2021/12/06/git-undo-merge-issues-solved-with-visual-examples/
IEEE
" » Git undo/merge issues solved with visual examples." Fidal Mathew | Sciencx [Online]. Available: https://www.scien.cx/2021/12/06/git-undo-merge-issues-solved-with-visual-examples/. [Accessed: ]
rf:citation
» Git undo/merge issues solved with visual examples | Fidal Mathew | Sciencx | https://www.scien.cx/2021/12/06/git-undo-merge-issues-solved-with-visual-examples/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.