Using Git in the Real Life for Beginners 4
Conflict, mistake, lose files, those things happen, which I used to google the answer to solving, then forgot how to deal with it the next time. So know fundamental knowledge would help you better calm down, don’t get frustrated.
5.Fixing messed up
When we need to merge local branches.
git merge newName
AUto-merging master.txt
CONFLICT(connect): Merge conflict in master.txt
Automatic merge failed; fix conflicts and then commit the result
git remind you it is the master.txt cause the problem.
use git status
to see the details
<<<<<<<<<<< HEAD
xxxxxxxxx
============
xxxxYxxxx
>>>>>>>>>>> Branch
above «««< HEAD to ======= contend is showing your conflict place in master branch; ======= to »»»> Branch is showing you conflicts in Branch devlop
after you fix the file cause conflict, “git add && commit”. done!
Coding, Coding ☕☕☕☕
Got feedback from market team, our team decide to build a slight different features.
🙆♀️🙆♀️🙆♀️🙆♀️
Time to leave, git commit
, git push
.
The next day, Coding, Coding ☕
we are informed it's better to stick to our original plan.... 😅😅😅😅
“git reset” can save you.
git reset --
? It has three options.
hard, mixed, soft would return to different file version, see the drawing below to understand.
we can time travel through commit history
reset
- -hard
- -mixed
- -soft
since we don’t want the new feature, so we choose –hard model
git log
git reset --hard 3245lcio99
Here is a sincerely suggestion with tear, do a backup.
git log
git branch feature/abort1 // create a new branch
//base on feature, name feature/abort1
git reset --hard 3245lcio99
Coding, Coding. your cowork ask you to pull the lastest code from remote
git pull
Error:
git pull failed, git pull failed you have unstaged changes..Your local changes to the following files would be overwritten
you cannot just commit, and you sure wannt to keep your file, what do you do?
git reset –mixed
git pull xxx
you just create a commit;
then find a mistake, and you don’t wannt create another commit, then?
git reset –soft HEAD^ // or commit id
git add
git commit -m ‘message’
or easier way
git commit –amend
- fix your mistake
- git add
- git commit –amend
Be carefully with reset –hard, you won’t find it back when using it.
checkout and reset
when we write a lot of code, it turns out not working, don’t use ctr+Z
, checkout can enable you return lastest
Stage version.
git status // find out the modified file name
git checkout –
you just git add
a file, then you dicide commit it next time
git status // find out to be committed files
git reset HEAD// to unstage
Coding, Coding, checkout, reset –hard, coding, pulling, merge.
You just lose some files!!
git reflog // on master branch
git reflog
23452754 HEAD@{0} : commit: create 1.txt file
22344275 HEAD@{1} : revert: Revert "xxxx"
75639567 HEAD@{2} : reset: moving to 75639567
23452754 HEAD@{3} : cherry-pick: create 1.txt file
23452754 HEAD@{4} : commit: create 1.txt file
23452754
it is the prev-commit SHA1 valueHEAD@{0}
commit location, 0 mean lastest commit history
check ref-log on develop branch
git reflog [branchName]
to find the version you need.
When you need to find your coworker’s file, use git log
to search
git log
git log --author="name"
git log --author="name1\|name2"
git log --since="2030.01.01"
git log --after="2030.01.01"
git log --before="2030.01.01"
git log --since="2030.01.01" --untile="2040.01.01"
git log --prep="love" --oneline