头图
As a developer, no matter where you go, you may have an indescribable relationship with the code repository, from clone repository => new feature branch => commit after the development function is completed => branch merge (conflict resolution) => label = > Deploying to the online environment, this series of operations is inseparable from the code version management tool Git, so familiarity with common commands will help us improve efficiency.

clone the git repository

There are two ways to clone gitlab or github, ssh protocol and http protocol. To use http, you only need to enter the username and password. To use ssh, you need to deploy your own public key to gitlab or github, and configure it in the settings of the login user. Query your own public key, usually in the following directory on your computer:

➜  ~ cd .ssh
➜  .ssh ls
id_rsa      id_rsa.pub  known_hosts

The public key is the content in the file id_rsa.pub. After deployment, you can clone the repository.

branch operation

What is a branch?

Git can divide our codebase into different branches, so that each feature can be independent of each other during the development process.

With branches, we can work on different versions of a code repository, such as developing features and fixing bugs at the same time.

git checkout -b

Ok, I heard that you have cloned the repository named Blog to the local, and now the Leader arranges you to do a comment function, then entering the project from the terminal generally looks like this:

➜  Blog git:(main)

If the production environment is deployed on the main branch or master, then new features should be checked out from this master branch.

➜  Blog git:(main) git checkout -b comment
Switched to a new branch 'comment'

* comment
  ele
  main

git status / git add / git commit

At this point, you can do your function on this branch. When you write about the rise, suddenly the leader tells you that there is a problem with the line that needs to be fixed urgently. You look like an old dog, but you're actually panicking. But in any case can think of with your feet to save the comment function that you have done half of it. So you use the following command:

➜  Blog git:(comment) ✗ git status
On branch comment
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
    modified:   README.md

no changes added to commit (use "git add" and/or "git commit -a")

// 看到了熟悉的内容改动,表示放心,使用下面的命令把它们纳入到版本管理
➜  Blog git:(comment) ✗ git add .
➜  Blog git:(comment) ✗ git commit -m "comment progress 50%"
[comment 006a55d] comment progress 50%
 1 file changed, 1 insertion(+), 1 deletion(-)

git log / git reset

This is certainly possible, but a commit record is generated. In fact, you can also temporarily store your changes. Now we reset this commit. To reset, we need to know the previous version number:

➜  Blog git:(comment) git log --oneline

006a55d (HEAD -> comment) comment progress 50%
2bf17f8 (origin/main, origin/HEAD, main) loading
56131ba 优化样式
cc91d8c abstract
a81dbe7 sitemap
b915f65 baidu

It can be seen that we only need to reset 2bf17f8 this time, but pay attention to the need to keep the hard-written code (bug). So I used the following command wisely:

➜  Blog git:(comment) git reset --mixed 2bf17f8
Unstaged changes after reset:
M    README.md
➜  Blog git:(comment) ✗ git status
On branch comment
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
    modified:   README.md

Happy to be back to where it was before committing. If you use git reset --hard 2bf17f8, then congratulations, all previous changes are lost, so be careful.

git stash

To reduce one useless commit, we use git stash to stage the unfinished comment feature:

➜  Blog git:(comment) ✗ git stash
Saved working directory and index state WIP on comment: 2bf17f8 loading
// 可以看到是以上次提交的信息记录本次stash的内容

Next, you can handle the urgent tasks arranged by the Leader, and cut the hot_fix branch from the main branch

➜  Blog git:(main) git checkout -b hot_fix
Switched to a new branch 'hot_fix'

Save the commit after the issue is fixed:

➜  Blog git:(hot_fix) ✗ git add .
➜  Blog git:(hot_fix) ✗ git commit -m "bug fixed"

git merge

switch branch to main merge hot_fix

➜  Blog git:(hot_fix) git checkout main
Switched to branch 'main'
Your branch is up to date with 'origin/main'.
➜  Blog git:(main) git merge hot_fix
Updating 2bf17f8..b85e853
Fast-forward
 README.md | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

At this point, you can release a new version from the main branch to the online, and fix the urgent problems online. At this time, there is a useless branch hot_fix in your local:

➜  Blog git:(main) git branch
  comment
  ele
  hot_fix
* main

It can now be removed using the command:

➜  Blog git:(main) git branch -d hot_fix
Deleted branch hot_fix (was b85e853).

If you also pushed it to the remote before, view all remote branches:

➜  Blog git:(main) git branch -r
  origin/HEAD -> origin/main
  origin/ele
  origin/hot_fix
  origin/main
(END)

Then the remote branch is useless now. Use the following command to delete the remote branch:

➜  Blog git:(main) git push origin -d hot_fix
To github.com:iicoom/Blog.git
 - [deleted]         hot_fix

Now back to a clean state, you can go back to the functionality before the comment branch was completed:

➜  Blog git:(main) git checkout comment
Switched to branch 'comment'

// 把之前暂存的内容恢复
➜  Blog git:(comment) git stash pop
On branch comment
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
    modified:   README.md

no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (0553a4fa89f44ea5708a21d5c421c7b1115c40cb)

Happy coding can be done again. This article reproduces the use of some git commands in actual work through a scenario, hoping to help you.

If the above operation is necessary for daily pretense, then the next article is going to be a little exciting. People who don’t know will usually panic when they encounter it~

Git Daily Loading Manual (2) · Accidentally deleted branch recovery git reflog


来了老弟
508 声望31 粉丝

纸上得来终觉浅,绝知此事要躬行