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
Let's initialize the project with -
git add .
git commit -m “first commit”
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
Let’s fix it and make a git commit.
Now go to the “master” branch and “merge fix1” branch -
git checkout master
git merge fix1
Now, this is exactly what happens when you send a pull request on a file that has been modified.
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.
After fixing the conflict just -
git add .
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.
and add some changes.
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.
git checkout -- . or
git checkout -- filename
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
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.
3.Undo commits -
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 -
Top comments (0)