Git 常用命令

作者: 淡淡_孩子气 | 来源:发表于2019-12-30 19:04 被阅读0次

Git命令

cmd 描述
git status 查看当前分支状态
git branch 查看当前所属分支
git branch -a 查看所有本地分支和远程分支
git checkout -b 分支名 新建并切换本地分支
git checkout -b 本地分支名 origin/远程分支名 新建并切换远程分支
git branch -D 分支名 删除本地分支
git push origin --delete 分支名 删除远程分支
git pull origin master 下拉代码
git clone git@github.com 克隆代码
git stash 储藏代码
git stash list 查看现有的储藏
git stash apply 应用储藏
git remote -v 查看现有远程仓库的地址url
git config credential.helper store 保存密码到硬盘
git push origin HEAD ||
git push origin ***(分支名称)
本地推到服务器
git config --global credential.helper store 全局配置密码
git diff 查看尚未暂存的文件更新了哪些部分

创建标签tag

//创建
git tag -a <版本号> <SHA值> -m "<备注信息>"
//推送
git push origin --tags

切换分支

git checkout 分支名

若:切换失败

git reset --hard 分支名

git checkout 分支名

新建远程分支

  1. git checkout -b 分支名
  2. git push origin 分支
  3. git branch --set-upstream-to=origin/分支名 //将本地分支my-test关联到远程分支
  4. git branch -a //查看远程分支

上传代码

跟踪项目文件夹中的所有文件和文件夹
1. git add . 
2. git commit -m 'first_commit'
3. git remote add origin https://github.com/jerryhanjj/baike_spider.git

如果关联出现错误

fatal: remote origin already exists

则执行下列语句再进行关联

git remote rm origin

把本地库的所有内容推送到远程库上

git push -u origin master

如果在推送时出现错误

error:failed to push som refs to.......,

则执行下列语句
git pull origin master

error:refusing to merge unrelated histories

则执行下面语句

git pull origin master --allow-unrelated-histories

将项目提交到服务器
mkdir ddys-lite-android
cd ddys-lite-android
git init
touch README.md
git add README.md
git commit -m "first commit"
git remote add origin git@git.dingdingyisheng.mobi:ddys-lite/ddys-lite-android.git
git push -u origin master

创建远程仓库

  1. git init //初始化文件夹为代码仓库
  2. git add . //添加本地所有文件到
  3. git commit -m "你的提交信息" //向本地仓库提交add的所有文件
  4. git remote add origin https://git.oschina.net/sanchi3/ThreeKillers.git //将OSC的远程分支拉取到本地
  5. git push -u origin master //提交本地仓库的修改到远程master分支。

由于是第一次提交,远端还没有创建任何分支,所以使用-u命令,与远端建立联系。

若push 失败

git pull origin master --allow-unrelated-histories

强推

git push --force-with-lease origin master:master

推错远程分支

git reset --hard <commit_id>

git push origin HEAD --force

全局配置用户信息

git config --global user.email "zhengweiqun@flyaudio.cn" && git config --global user.name "zhengweiqun" && git config --global core.editor "vim"
<1>:git config --global user.email "linghuihua@flyaudio.cn"  指定邮箱
<2>:git config --global user.name "linghuihua"        指定用户名
<3>:git config --global core.editor "vim"          指定编辑器为VIM
Git global setup
git config --global user.name "郑炜群"
git config --global user.email "zhengwq@iaskdoc.com"

删除所有提交,清空仓库

1、只需删除.git项目根目录中的目录
2、再重新初始化项目,上传到服务器
git init
git remote add origin git@github.com:user/repo
git add .
git commit -m 'message'
git push -f origin master

放弃本地修改

#本地所有修改的。没有的提交的,都返回到原来的状态
git checkout .
#把所有没有提交的修改暂存到stash里面。可用git stash pop回复。
git stash
#返回到某个节点,不保留修改。
git reset --hard HASH 
#返回到某个节点。保留修改
git reset --soft HASH 

打patch(将patch文件中对代码的修改,应用到源代码,从而把对代码的修改应用到code中)

git format-patch
$ git format-patch HEAD^                #生成最近的1次commit的patch
$ git format-patch HEAD^^             #生成最近的2次commit的patch
$ git format-patch HEAD^^^                   #生成最近的3次commit的patch
$ git format-patch HEAD^^^^             #生成最近的4次commit的patch
$ git format-patch <r1>..<r2>                   #生成两个commit间的修改的patch(包含两个commit. <r1>和<r2>都是具体的commit号)
$ git format-patch -1 <r1>                  #生成单个commit的patch
$ git format-patch <r1>                             #生成某commit以来的修改patch(不包含该commit)
$ git format-patch --root <r1>               #生成从根到r1提交的所有patch

git am
$ git apply --stat 0001-limit-log-function.patch         # 查看patch的情况
$ git apply --check 0001-limit-log-function.patch        # 检查patch是否能够打上,如果没有任何输出,则说明无冲突,可以打上
(注:git apply是另外一种打patch的命令,其与git am的区别是,git apply并不会将commit message等打上去,打完patch后需要重新git add和git commit,而git am会直接将patch的所有信息打上去,而且不用重新git add和git commit,author也是patch的author而不是打patch的人)
$ git am 0001-limit-log-function.patch                                # 将名字为0001-limit-log-function.patch的patch打上
$ git am --signoff 0001-limit-log-function.patch                  # 添加-s或者--signoff,还可以把自己的名字添加为signed off by信息,作用是注明打patch的人是谁,因为有时打patch的人并不是patch的作者
$ git am ~/patch-set/*.patch             # 将路径~/patch-set/*.patch 按照先后顺序打上
$ git am --abort                                                                   # 当git am失败时,用以将已经在am过程中打上的patch废弃掉(比如有三个patch,打到第三个patch时有冲突,那么这条命令会把打上的前两个patch丢弃掉,返回没有打patch的状态)
$ git am --resolved                                                             #当git am失败,解决完冲突后,这条命令会接着打patch


// 生成365a到4e16之间的补丁
git format-patch 365a..4e16 --365a和4e16分别对应两次提交的名称
// 应用patch:
git apply --stat newpatch.patch  ->  先检查patch文件
git apply --check newpatch.patch  ->  检查能否应用成功
git am  newpatch.patch      ->  打补丁
或者
git am --signoff < newpatch.patch
(使用-s或--signoff选项,可以commit信息中加入Signed-off-by信息)

相关文章

网友评论

    本文标题:Git 常用命令

    本文链接:https://www.haomeiwen.com/subject/dnnmoctx.html