文章目录
Git常用命令
克隆到本地
git clone https://github.com/tomoyachen/helloflask.git
查看分支列表与当前所在分支
git branch
检出 切换分支到xxx分支
git checkout xxx
添加目录内新文件到版本管理列表
.可以提交未跟踪和修改文件,但是不处理删除文件
只能够提交当前目录或者它后代目录下相应文件
git add .
添加目录内新文件到版本管理列表
all可以提交未跟踪、修改和删除文件
ll无论在哪个目录执行都会提交相应文件
git add all
提交到本地仓库
git commit -m "更新内容提交说明"
推送到远端
#第一次
git push origin master
#绑定远端与分支后
git push
拉取到本地 不自动megre
#第一次
git fetch origin master
#绑定远端与分支后
git fetch
拉取到本地 自动megre
#第一次
git pull origin master
#绑定远端与分支后
git pull
把xxx分支合并到当前分支
git megre xxx
新建一个名为xxx的分支(从当前分支克隆)
#新建,但不切换
git branch xxx
#新建,并切换到此分支
git checkout -b xxx
删除 xxx分支
git branch -d xxx
查看Git日志
git log
回滚到某次提交
git log信息如下
commit 796e93b680f31d7762b7760363f624a366d374f1 (origin/master, origin/HEAD)
#慎重 !不可逆操作
git reset --hard 796e93b680f31d7762b7760363f624a366d374f1
#慎重!不可逆操作
git checkout 796e93b680f31d7762b7760363f624a366d374f1
常见场景
同时两方修改同一文件, 自动merge
A用户
新加了个def a()函数
git add .
git commit -m "Updates by A"
git push
B用户
新加了个def b()函数(代码无冲突)
git add .
git comiit -m "Updates by B"
git pull
git 自动merge
同时两方修改同一文件, 手动merge
A用户
修改了a()函数里的内容
git add .
git commit -m "Updates by A"
git push
B用户
也修改了a()函数里的内容(代码有冲突)
git add .
git comiit -m "Updates by B"
git pull
合并失败,提示:
Auto-merging demos/watchlist/app.py
CONFLICT (content): Merge conflict in demos/watchlist/app.py
Automatic merge failed; fix conflicts and then commit the result.
最终需要手动merge,解决冲突
<<<HEAD 到 === 是你本地内容
=== 到 >>> 36c…a6c 是远端拉取的内容
手动修改代码为期望功能后保存提交即可,手动merge容易出错,一定要告知测试进行测试。
def a():
<<<<<<< HEAD
a = 10
b = 20
c = a - b
=======
a = 1
b = 2
c = 1+2
>>>>>>> 36c10d1ad12723d54f8ef6711b065d4a90330a6c
pass