Git的常规操作

老毛 -
Git的常规操作
作为一名开发人员,不管走到哪里可能都会和代码仓库发生不可描述的关系,从clone仓库 => 新的功能分支 => 开发功能完成后提交 => 分支合并 => 打上标签 => 部署到线上环境,这一系列操作都离不开这个代码版本管理工具Git,所以常见命令烂熟于心有助于我们提升效率。克隆git仓库

gitlab或者github有有两种克隆方式,ssh协议和http协议。使用http只要输入用户名和密码即可,使用ssh是要部署自己的公钥到gitlab或者github上,在登录用户的设置里边配置。查询自己的公钥,一般自己电脑的以下目录:

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

公钥就是id_rsa.pub这个文件中的内容,部署好之后就可以去克隆仓库了。

分支操作git checkout -b

好的,听说名为Blog的仓库你已经克隆到本地,现在要做一个评论的功能,那么从终端进入项目一般是这个样子:

➜  Blog git:(main)

假如生产环境部署的是main分支或者叫master,那么新的功能应该从这个主分支检出。

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

* comment
  ele
  main
git status / git add / git commit

到这里就可以在这个分支上做你的功能了。当你写到兴起的时候,突然领导告诉你线上有个问题急需要修复。突然有点慌张,不过还是得先保存你做了一半的评论功能。于是你使用了以下命令:

➜  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

这样做是可以,但是产生了一次提交记录,其实还可以把你的修改先暂存起来,现在我们把这次提交重置掉,要进行重置我们需要知道上一个版本号:

➜  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

可以看到我们只要重置2bf17f8这一次就好了,但是需要注意需要保留辛苦写起来的 代码(bug)。于是机智的使用了下面命令:

➜  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

愉快的回到了未提交之前的状态。如果你使用git reset --hard 2bf17f8,那么就要恭喜了,之前的改动就全部丢失了,所以一定要慎重。

git stash

为了减少一次无用的提交,我们使用git stash来暂存未完成的评论功能:

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

接下来就可以处理领导安排的紧急任务了,从主分支切出hot_fix分支

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

问题修复后保存提交:

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

切换分支到main合并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(-)

到这里,就可以从main分支发布新的版本到线上了,并且修复了线上的紧急问题。这时你的本地出现了没有用的分支hot_fix:

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

现在可以把它删除掉了使用:

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

假如你之前还把它推送到了远程,查看所有远程分支:

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

那现在远程也没有任何用途了,使用下面命令删除远程分支:

➜  Blog git:(main) git branch -r -d origin/hot_fix
Deleted remote-tracking branch origin/hot_fix (was b85e853).

现在又重新回到清爽的状态,可以重新回到comment分支完成之前的功能:

➜  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了。本文通过一个场景,重现了在实际工作中一些git命令的使用,希望对你有帮助。

特别申明:本文内容来源网络,版权归原作者所有,如有侵权请立即与我们联系(cy198701067573@163.com),我们将及时处理。

Tags 标签

gitgithubgitlabshell

扩展阅读

加个好友,技术交流

1628738909466805.jpg