9

image.png

Preface

Hi, everyone, I’m CrazyCodes, today we will explain GIT based on the work scenes we encounter every day, and there is a beautiful flowchart at the end of the article, please pick it up by yourself.

history

From ancient times to the present, no matter what you learn, you must first talk about its birthplace.

Git is an open source distributed version control system. It is an open source version control software developed by Linus, the father of Linux, to help manage the development of the Linux kernel.
image.png
GitHub is a software project hosting platform developed based on Git.
image.png
GitLab is also a software project hosting platform developed based on Git, but it is open source.

basic knowledge

git clone && git init

Initialize a Git repository locally
image.png
It is essentially the same as pulling from a remote to a local Git warehouse. To pull a warehouse remotely, you also need to establish a warehouse remotely. The command is still git init.

git directory

Switch branch

.git/HEAD is used to record the current branch, using git checkout branchName is to directly modify the file

ref: refs/heads/master

Current commit of a branch

.git/refs/heads

e6caa5bbcd4d362d3a5bac6b5a3417c15991484c

Similar to view the current commit of a tag. git/refs/tags

e6caa5bbcd4d362d3a5bac6b5a3417c15991484c

storage cache

git add filename The content of the action is saved in the .git/index file

Log

.git/logs saves all logs, use git log to query the file

Common commands

  • git add
  • git commit
  • git merge
  • git checkout
  • git diff
  • git status
  • git log

Learning platform

https://learngitbranching.js.org/?NODEMO=&locale=zh_CN

Application scenario

git blame to find out the "true culprit"

git blame filename

git checkout to roll back files

git checkout filename

How to cancel git add

git reset HEAD filename

How to cancel git commit

git reset --soft HEAD~1
// 重新修改commit信息
git commit --amend

How to cancel git merge

git merge --abort

Don't use git add blindly.

git status
// 在将文件添加到缓存区之前,请一定、一定、一定先git diff 下
git diff
// 检查所有修改都是想add的,如果所有修改都是的话,可以使用 git add . 除此之外还请慎重呀!
git add . || git add filename1 filename2

git rebase rebase == "change father"?

git rebase branch

git cherry-pick quietly "steal code"

git cherry-pick hashcommit|branchname

How to deal with the stoppage task

git stash
git stash apply|pop
git list

Actual combat begins

Step.1

First of all, when you receive a new code, clone it first (described above). Later, you need to create a branch of your own to develop what you need to do, as shown in the figure below
image.png

Step.2

When you have completed a function, you need to add the developed files to the staging area and prepare to submit the code to the code base, then you need to execute git add
image.png

Step.3

Suddenly, you find that your hand is shaking and you have added a file by mistake. Don't panic. At this time, you can use git reset to kick this file out of the temporary storage area.
image.png

Step.4

When you confirm that all the files added are the files you want to add, you can select commit to submit the files
image.png

Step.5

Of course, there are also opportunities for regret. What should you do if you want to withdraw after you commit? You can still use the versatile git reset command to withdraw your commit.
image.png

Step.6

After confirming that there is no problem with commit, you need to merge the code of your branch into your main branch or development branch and submit it to the server for testing.
image.png
Of course, you still have the opportunity to go back (in the merge phase) to directly undo the merger
image.png

Finally, based on the scenario described above, a complete flow chart is provided!
image.png

Thanks

Thank you for seeing here, I hope this article can help you, thank you!
图怪兽_aca660f7aa40e1eb9fb2ce7d8b804a59_89437.jpg


CrazyCodes
16.9k 声望14.8k 粉丝

I am CrazyCodes,生命不息,编码不止。