在工作中,最多用的代码管理工具就是git,接下来分享一些git的常用命令:
1. git clone
这个命令没啥多说的,获取开源的源代码或者组内的project
git clone ...
2. git remote
git remote -v
通过这个命令,你可以得到当前repo所指向的远端repo,一般是 fetch 和 push两个。
origin https://github.com/qcl006007/chat-vue.git (fetch)
origin https://github.com/qcl006007/chat-vue.git (push)
- 增加 一个remote
git remote add remote_name remote_url
- 移除remote
git remote remove remote_name
- 设置新的url
git remote set-url remote_name new_remote_address
3. git branch
使用git分支的好处在于多人协作的时候,可以避免大量的冲突。
- 分支信息
git branch
通过这个命令,可以拿到当前repo的active分支,和有哪些分支。
master
pr/fix_bug
pr/new_compare
* pr/use_slotname
- 创建新分支
git branch new_branch(branch_name)
注意,如果使用这种方式创建新分支,那么active branch仍然是当前分支。
git branch pr/show_demo
git branch
master
pr/fix_bug
pr/new_compare
pr/show_demo
* pr/use_slotname
如果想在创建新分支的同时切到另一个分支去工作,可以使用:
git checkout -b branch_name
删除本地分支,可以使用
git branch -D branch_name
4. git merge
git checkout base_branch
git merge new_branch
5. git handle conflict
<<<<<<<< HEAD
...
...
========
...
...
>>>>>>>> new_branch
通常有conflict的地方会有如下的显示,如果使用的是 vscode, visual studio等成熟的开发工具也会有冲突的提示。
6. git pull
同步最新的远端代码
7. git push
提交分支代码到远端仓库。
当然git的操作远不止这些,git还有好多实用的命令,类似 git stash, git commit, git revert, 等等。
接下来会继续介绍git的命令。