Git 常用命令
一、【GIT常用】
git clone git status git log git reflog git add . git commit -a -m "commit remark" git remote -v // 查看远程仓库地址
git remote show origin // 查看当前仓库基本信息。 git diff git diff HEAD
git config user.name //获取当前登录的用户
git config user.email //获取当前登录用户的邮箱
git config --global user.name 'userName' // 修改登陆账号,userName为你的git账号
git config --global user.email 'email' // 修改登陆邮箱,email为你的git邮箱
git config --global user.password 'password' // 修改登陆密码,password为你的git密码
二、【拉取】
1、git fetch 相当于是从远程获取最新到本地,不会自动merge,如下指令:
git fetch orgin master //将远程仓库的master分支下载到本地当前branch中
git log -p master ..origin/master //比较本地的master分支和origin/master分支的差别
git merge origin/master //进行合并
也可以用以下指令:
git fetch origin master:tmp //从远程仓库master分支获取最新,在本地建立tmp分支
git diff tmp //將當前分支和tmp進行對比
git merge tmp //合并tmp分支到当前分支
2. git pull:相当于是从远程获取最新版本并merge到本地
git pull origin master
git pull 相当于从远程获取最新版本并merge到本地
在实际使用中,git fetch更安全一些
3. 自己要与origin master建立连接(下划线为远程仓库链接)
git init
git remote add origin git@github.com:xxx/xxx.git
等同于
git clone git@github.com:xxx/xxx.git
三、【分支】
git branch -a
git branch -r
git branch -vv
git checkout -b {branch_name}
----- 指定当前分支到 branch_name 追踪关系
git branch --set-upstream-to=origin/{branch_name}
==
git branch -u origin/{branch_name}
当我想从远程仓库里拉取一条本地不存在的分支时:
git checkout -b 本地分支名 origin/远程分支名
若成功,将会在本地创建新分支,并自动切到新分支上。
如果出现提示:
fatal: Cannot update paths and switch to branch 'dev' at the same time. Did you intend to checkout 'origin/dev' which can not be resolved as commit?
表示拉取不成功。我们需要先执行
git fetch origin 远程分支名
然后再执行
git checkout -b 本地分支名 origin/远程分支名
**或者直接拉取
git clone -b 远程分支名 仓库地址
[example] git clone -b dev git@gitlab.xxx.com:backend/xxx.git (dev是分支名称)
四、【Push】
----- 如果远端无 branch_name,则push新建一个
git push --set-upstream origin {branch_name}
----- 或者 -u 新建远端分支,并与当前本地分支建立追踪关系
git push -u origin {branch_name}
此命令 ==
git push --set-upstream origin {branch_name} && git branch -u origin/{branch_name}
----- 删除本地分支
git branch -d {branch_name}
----- 删除远端分支
git push origin --delete {branch_name}
==
git push origin :{branch_name}
删除问题:
在执行git push origin --delete删除远程分支时,因为删除分支时间较长没响应,强行中断了该命令,在gitlab上查看远程分支确实已经删除,但是通过git branch -a 仍然可以看到该分支,最后通过git remote show origin命令查看,执行git remote prune origin解决。
git branch -m [oldBranchName] [newBranchName]
git push origin [oldBranchName ]
git push origin [newBranchName]
六、【git stash暂存的操作】
1. 暂存操作(查看当前状态 -> 如果有修改,添加修改文件 -> 暂存)
git status
git add .
git stash save ['stash-sign']
2. 查看当前暂存的记录
git stash list
3. 恢复暂存的工作
恢复指定暂存记录,恢复后暂存区域会删除当前记录,通过list索引index取出恢复。
git stash pop stash@{index}
4. 删除暂存
-- 删除某个暂存, 暂存记录保存在list内,需要通过list索引index取出恢复
git stash drop stash@{index}
-- 删除全部暂存
git stash clear
-- 其它学习
https://www.cnblogs.com/daxiong225/p/13472840.html
https://blog.csdn.net/stone_yw/article/details/80795669
https://jingyan.baidu.com/article/49ad8bceacac6b5834d8fa9a.html
👌👌