Situation 1: handling conflict
While working in a team, code conflict is unavoidable, often occurring when 2 members do tasks on the same module. Let’s see when conflicts appear and how to resolve them
Initially, on the dev branch, Teo and Ti checkout 2 different branches, Teo checkout the branch is create-user
, Ti checkout the branch is view-user
, and then Teo and Ti together edit the first line on the file asuser.js
Next, Teo merges the branch create-user
into the dev branch. Now everything is fine until Ti merges the branch view-user
into the dev branch. BOOM. Unable to merge and an error appearsCONFLICT (content): Merge conflict in user.js
The way to solve this problem is that whoever has the conflicted branch will handle it. Here, Ti’s branch, Ti needs to discuss with the members related to the code conflict and perform the following steps:
1. Identify conflicting code:
Use git status
to find conflicted files
In the user.js file, you will see the following content:
<<<<<<< HEAD
code of Tèo
=======
code of Tí
>>>>>>> view-user
2. Conflict resolution:
Discuss with Teo, determine which branch you want to keep the code from or combine both and then remove the <<<<<<<, =======, and >>>>>>>
Use the git add user.js
and command git commit
to complete the merge process
3. Check again and push to remote:
Check whether the conflict is resolved ok by usinggit status
Then, use it git push
to push the merged changes to the remote
Note: To limit code conflicts, you need to regularly pull code and commit code
Situation 2: view commit history and activity history on git
Use git log
to view commit history such as commit hash, committer, commit time, commit message
commit 7a2cbffba6023df7f37dd1808f46d5a6b59d810b
Author: teo <teo@gmail.com>
Date: Fri Feb 11 10:05:00 2024 +0700
Create log
git reflog
as an activity history, helping to save activities performed on git, for example, Teo just checked out through the master branch, or Ti just reset a commit,… git reflog
very useful for mistakes during use. git (I have an example of a mistake in situation 4)
2de6d93 (view-user) HEAD@{11}: checkout: moving from view_user to master
6303151 (create-user) HEAD@{12}: reset: moving to HEAD@{3}
Situation 3: delete commit
A long day of hard work on tasks, Teo finally finished the task, Teo committed the code and went to sleep
The next day, Teo opened the code again and saw that his code was not okay at all. Teo needed to delete all the code or need to edit the code again. Teo can use git reset
or git revert
to solve this problem (here I introduce git reset
)
Git reset has several options such as mixed
(default when no options are passed), --soft
and--hard
git reset <commit>
: retain committed changes in staginggit reset --soft <commit>
: retain changes in working directorygit reset --hard <commit>
: discard all changes, both in working directory and staging
Situation 4: restore commit
As in situation 3, Teo wanted to reset the commit to edit the code, but Teo accidentally used it, --hard
causing the code to disappear. Teo confusedly asked the Leader for help. The Leader instructed Teo to get the code back with the following operations: after:
- Use
git reflog
to get commit hash:
627f5d1 (HEAD -> master, conflict-2) HEAD@{0}: reset: moving to 627f5d161bff441840d5b34a77abe16561f505f8
ec02ce2 HEAD@{1}: reset: moving to HEAD@{1}
- Use
git reset --hard
to restore commit:
git reset --hard ec02ce2
Teo happily cheered and thanked the leader
Situation 5: merge multiple commits into 1 commit
Ti was assigned the task of managing users including CRUD. I listened to the Leader’s advice: “You must commit code continuously.”
Ti creates the file userRouter, Ti commits
I continue to create the userController file, I continue to commit
Ti creates the userService file, Ti continues to commit
I finished the task and happily took it to the Leader to review the code. The Leader looked at the Pull Request and said: “Let me combine the commits into 1 for you. Committing continuously is necessary but it is necessary to commit reasonable, avoiding redundancy or too much duplication”
Ti struggled and thought in his head: “So what should I do now?”
Well, everything is up to the Leader. The Leader continues to instruct Ti to merge the commit, as follows:
- Use git rebase
git rebase -i HEAD^n
(n là số commit muốn gộp)
- Git will open an interactive window, select the commits to merge
pick
intosquash
and save - Git continues to open an interactive window, select the message or enter an arbitrary commit message and save. Done
Scenario 6: edit commit message
One time Ti committed the wrong message and Ti edited that message. Ti was so happy, Ti immediately shared it on forum Ti’s handling method is as follows:
For the latest message: use git commit --amend -m "new_message"
(can only change the message of the latest commit)
For multiple messages at the same time, old or new are fine:
- Use
git rebase -i HEAD~n
(n is the number of commits you want to change the message) - Git will open an interactive window, you can change
pick
toreword
the commits you want to change and save - Git continues to open an interactive window, you change the new message and save it. As many commits as you want to change, there are as many interactive windows open to change the message.
Situation 7: rename branch
Another time, Ti misnamed the branch and Ti corrected the branch name. Ti was so happy, Ti immediately shared it on forum X. The author (me) happened to see Ti’s shared post so he copied and pasted it here. Ti’s handling method is as follows:
If the branch name is incorrect, please enter the following command:git branch -m new_branch
Situation 8: cannot checkout via another branch
Ti and Teo are two interns at A Bo Co company, Ti joined 2 days before Teo.
One day, Teo was coding in a branch auth
but also eagerly checked out user
Ti’s branch to see where Ti’s code was because Teo’s branch was dependent on Ti’s branch.
Teo entered the command git checkout user
, BOOM, checkout failed and this error appearederror: Your local changes to the following files would be overwritten by checkout: auth.js Please commit your changes or stash them before you switch branches. Aborting
Teo was confused, Teo turned to ask Ti because Ti worked 2 days before Teo so he thought Ti would know how to handle it.
Ti looked at the error and said: “There must be something wrong, try deleting all the code.” The Leader next to him was startled, so he had to turn around and deal with the two children who had not yet experienced this life
Mr. Leader said: “Teo cannot checkout to another branch because Teo is doing bad coding in the current branch. In order for Teo to be able to checkout to another branch, he needs to commit the code first”
Teo replied: “I don’t want to commit the code yet because I haven’t finished coding yet.”
Mr. Leader continued: “OK, then use it for me. git stash
“
git stash
Helps store code temporarily and then use it when you come back to dev againgit stash pop
Situation 9: restore state before merge
Everyone will make mistakes and so will Leader
The Leader assigned Ti a task and after Ti completed it, the Leader merged it into the dev branch
After merging, Mr. Leader discovered that the code did not work as expected. So Leader used the command: git reset --hard ORIG_HEAD
to delete the merged commits and told Ti to edit the code
Situation 10: merge any commit into another branch
Teo was assigned the task of product management
Teo completes the create product feature, Teo commits
Teo completed the product view feature, Teo committed
Teo completed the product update feature, Teo committed
Teo is working on the delete product feature, Teo has not committed yet…
At this time, the customer called the Leader: “Please release the create product and view product feature for me urgently.”
Mr. Leader is so familiar with customers making unexpected requests, Mr. Leader immediately used the command git cherry-pick <commit>
to merge those 2 commits into the main branch to release to the customer.
git cherry-pick
is a useful git command for cases where you want to merge any commit into another branch instead of the entire branch (git cherry-pick often conflicts, use the command git add .
and git commit -m “message”
it can be handled)
End
So, we have explored 10 common situations in Git . Hopefully the article has brought value to you and helped you gain a deeper understanding of git. In addition, I have an article Summary of 15 useful git commands for your project , you can read to learn more about git. Thank you for reading
Source : https://viblo.asia/p/10-tinh-huong-thuong-gap-trong-git-yZjJYgPOVOE
wonderful points altogether, you just received a new reader.
What would you recommend in regards to your post that you made a few days in the past?
Any sure?
Wow, that’s what I was searching for, what a data! existing here at this blog, thanks admin of this site.
When I originally commented I clicked the “Notify me when new comments are added” checkbox and now each time a comment is added I get several emails with the same comment. Is there any way you can remove people from that service? Cheers!
オナドールAbortion is the least common experience on the list: 17 percent of the women in my data report having had an abortion,which is consistent with the national averag Among those with this experience,
Two early insights that led the Buddha to enlightenment are the Middle Way and Dependent Origination.ラブドール おすすめAccording to the doctrine of the Middle Way,
Admiring the commitment you put into your site and in depth information you present.
It’s nice to come across a blog every once in a while that isn’t the
same unwanted rehashed material. Wonderful read!
I’ve bookmarked your site and I’m including your RSS feeds to my Google account.
When I initially commented I clicked the “Notify me when new comments are added” checkbox and now each time a comment is added I get four e-mails with the same comment.
Is there any way you can remove people from that service?
Thank you!
70918248
Hello! I’ve been reading your blog for a while now and finally got the courage to
go ahead and give you a shout out from Dallas Tx! Just
wanted to say keep up the fantastic job!
Hi there I am so happy I found your blog page, I really found
you by error, while I was searching on Aol for something else, Nonetheless I am here now and would just like to say thanks a lot for
a tremendous post and a all round thrilling blog (I also love the theme/design), I don’t have time to browse it all at the moment but
I have book-marked it and also added your RSS feeds, so when I have time I will be back to read much more, Please do keep up the awesome work.
Fastidious respond in return of this issue with firm arguments and explaining
the whole thing concerning that.
70918248
Even during tough days, 인천여성전용마사지 helps me bounce back.
70918248
70918248
70918248
70918248
70918248
Hi! Someone in my Facebook group shared this site with us so I came to give it a look.
I’m definitely loving the information. I’m bookmarking and will be tweeting this to my followers!
Excellent blog and wonderful style and design.
This is a topic which is close to my heart… Best wishes!
Exactly where are your contact details though?
Hey there would you mind letting me know which web host you’re using?
I’ve loaded your blog in 3 completely different internet
browsers and I must say this blog loads a lot faster then most.
Can you recommend a good web hosting provider at a honest price?
Thanks a lot, I appreciate it!
Hi there! I just wanted to ask if you ever have any trouble with hackers? My last blog (wordpress) was hacked and I ended up losing months of hard work due to no back up. Do you have any methods to protect against hackers?
Thanks for any other informative website. Where else could I get that kind of info written in such an ideal approach? I have a undertaking that I’m simply now working on, and I have been on the look out for such info.
great post, very informative. I wonder why the opposite experts of this sector don’t understand this.
You must continue your writing. I am confident, you’ve a great
readers’ base already!
Thanks for this detailed review! I’ve tried so many joint formulas
over the years, but most don’t deliver lasting relief. Joint Genesis sounds
promising, especially since it’s made in a GMP-certified facility.
Subscribed to hear more reviews like this!
Hey! I could have sworn I’ve been to this website before but after
checking through some of the post I realized it’s new to me.
Anyways, I’m definitely happy I found it and I’ll be book-marking and checking back frequently!