作为一名开发人员,不论走到哪里可能都会和代码仓库产生不可形容的关系,从clone仓库 => 新的性能分支 => 开发性能实现后提交 => 分支合并 => 打上标签 => 部署到线上环境,这一系列操作都离不开这个代码版本管理工具Git,所以常见命令烂熟于心有助于咱们晋升效率。
克隆git仓库
gitlab或者github有有两种克隆形式,ssh协定和http协定。应用http只有输出用户名和明码即可,应用ssh是要部署本人的公钥到gitlab或者github上,在登录用户的设置里边配置。查问本人的公钥,个别本人电脑的以下目录:
➜ ~ cd .ssh➜ .ssh lsid_rsa id_rsa.pub known_hosts
公钥就是id_rsa.pub这个文件中的内容,部署好之后就能够去克隆仓库了。
分支操作
git checkout -b
好的,据说名为Blog的仓库你曾经克隆到本地,当初要做一个评论的性能,那么从终端进入我的项目个别是这个样子:
➜ Blog git:(main)
如果生产环境部署的是main分支或者叫master,那么新的性能应该从这个主分支检出。
➜ Blog git:(main) git checkout -b commentSwitched to a new branch 'comment'* comment ele main
git status / git add / git commit
到这里就能够在这个分支上做你的性能了。当你写到衰亡的时候,忽然领导通知你线上有个问题急须要修复。忽然有点慌乱,不过还是得先保留你做了一半的评论性能。于是你应用了以下命令:
➜ Blog git:(comment) ✗ git statusOn branch commentChanges 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.mdno 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 --oneline006a55d (HEAD -> comment) comment progress 50%2bf17f8 (origin/main, origin/HEAD, main) loading56131ba 优化款式cc91d8c abstracta81dbe7 sitemapb915f65 baidu
能够看到咱们只有重置2bf17f8这一次就好了,然而须要留神须要保留辛苦写起来的 代码(bug)。于是机智的应用了上面命令:
➜ Blog git:(comment) git reset --mixed 2bf17f8Unstaged changes after reset:M README.md➜ Blog git:(comment) ✗ git statusOn branch commentChanges 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 stashSaved working directory and index state WIP on comment: 2bf17f8 loading// 能够看到是以上次提交的信息记录本次stash的内容
接下来就能够解决领导安顿的紧急任务了,从主分支切出hot_fix分支
➜ Blog git:(main) git checkout -b hot_fixSwitched 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 mainSwitched to branch 'main'Your branch is up to date with 'origin/main'.➜ Blog git:(main) git merge hot_fixUpdating 2bf17f8..b85e853Fast-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_fixDeleted 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_fixDeleted remote-tracking branch origin/hot_fix (was b85e853).
当初又从新回到清新的状态,能够从新回到comment分支实现之前的性能:
➜ Blog git:(main) git checkout commentSwitched to branch 'comment'// 把之前暂存的内容复原➜ Blog git:(comment) git stash popOn branch commentChanges 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.mdno changes added to commit (use "git add" and/or "git commit -a")Dropped refs/stash@{0} (0553a4fa89f44ea5708a21d5c421c7b1115c40cb)
又能够happy的coding了。本文通过一个场景,重现了在理论工作中一些git命令的应用,心愿对你有帮忙。