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
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 -
I made some changes -
The file is modified, now
git add .
git commit -m “ 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.
(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 -
The fix hasn’t been merged yet -
So, let’s go to fix1 branch and fix the issue.
git checkout fix1
Now go to the “master” branch and “merge fix1” branch -
git checkout master
git merge fix1
I manually adjusted the code and finally came up with this.
I hope you got clarity on how git merge conflicts happen and how to solve them if you get one.
There are many times when I need to revert back to the previous commit or unstage the selected files.
First let’s initialize an empty git repository.
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.
git checkout -- . or
git checkout -- filename
Sometimes, after you have made changes and added the changes using
git add -A
If you want to unstage it then
git restore --staged .
The file will keep the changes but remove them from the staging area.
If you want now you can use
git checkout -- . to go back to the last commit.
If you want to unstage it and go back to commit
git reset --hard if file is modified.
I made 3 commits - 1 =“ Hello I am fine”
2 =”How are you”
3 =” I am fine”
Suppose I want to go back to 1st commit
I have to revert the 2nd commit
git revert commit id-2
A merge conflict has occurred, simply choose the incoming change.
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 -