Git 命令行
文章目录
一、Git远程仓库创建和管理
1.创建远程仓库(基于GitLab)
-
web界面创建
-
clone
git clone git@gitLab.com:Mr.Chao/projectName.git
-
已有目录,设置远程仓库
git init
git remote add origin git@gitLab.com:Mr.Chao/projectName.git -
首次提交主分支
git push -u origin master
-
首次提交本地分支
git push -u origin br_name
2.远程仓库管理
-
查看远程仓库地址
git remote -v
-
查看远程仓库状态
git remote show origin
-
删除远程仓库
git remote rm repository
二、Git分支管理
1.本地分支
-
查看本地分支
git branch
-
查看远程分支
git branch -r
-
创建新分支
git branch new_br_name
-
切换到某个分支
git checkout br_name
-
创建新分支并立马切换到新分支
git checkout -b new_br_name
-
删除某个分支
git branch -d br_name
-
强制删除某个分支(未合并的分支)
git branch -D br_name
-
解决本地分支和远程已删除分支不同步问题(git branch -a时依然能看到远程已删除的分支)
git remote show origin 查看remote地址、远程分支和本地分支的对应关系
如果有在远程已删除的分支,会提示: use ‘git remote prune’ to remove
这样只要使用: git remote prune origin命令就可删除本地和远程不同步的分支信息
2.远程分支
-
拉取远程仓库中所有分支更新到本地
git pull
-
push更新所有分支
git push
-
删除远程分支
git push origin - -delete br_name
3.分支合并
-
将某个分支合并到当前分支
git merge br_name
git push
三、标签
1.创建标签
-
切换到需要打标签的分支创建标签
git tag tag_name //默认打在最新的commit上
git tag -a tag_name -m ‘注释说明tag信息’
2.标签操作
-
查看tag
git tag
-
推送tag到远程
git push origin tag_name
-
推送本地所有未推送过的tag到远程
git push origin - -tags
-
删除本地tag
git tag -d tag_name
-
删除远程tag
git push origin:refs/tags/tag_name
四、常用命令
1.帮助
-
查看帮助命令
git help
-
查看某个命令的帮助
git help (cmd)
2.增、删、改、查、回退
-
查看commit记录
git log
-
查看某次提交的内容
git show (commit)
-
查看当前工作区状态
git status
-
把工作区某个文件修改添加到本地暂存区
git add file_path
-
把所有工作区修改的文件添加到本地暂存区
git add .
-
放弃工作区某个文件的修改
git checkout file_path
-
放弃工作区的所有修改
git checkout .
-
从版本库中删除文件文件
git rm file_path
//如果确定删除,就再commit提交;如果发现删错了,则可用命令:git checkout file_path恢复
-
删除文件缓存
git rm - -cached file_path
git rm -r - -cached .
-
从暂存区恢复文件到工作区
git reset file_path
git reset - - .
-
回滚到某一次提交状态
方法一:
git revert (commit) //git revert用一次新的commit来回滚之前的commit(推荐)方法二:
git reset - -hard (commit) //git reset是直接删除commit,回退的是仓库源码
git push -f -u origin branchname // 强制提交到branchname分支