git 常用命令以及步骤总结

**********全局属性配置**********
账号密码:
git config --global user.name "Your Name"
git config --global user.email you@example.com

换行符:
#提交时转换为LF,检出时转换为CRLF
git config --global core.autocrlf true   
#提交时转换为LF,检出时不转换
git config --global core.autocrlf input   
#提交检出均不转换(默认)
git config --global core.autocrlf false
 
使用git config --list查看已设配置

**********代码clone**********
glt clone -b develop xxx.git

git status命令可以让我们时刻掌握仓库当前的状态;
git diff fileName 可查看变更内容;
git add 将文件从工作区添加到暂存区;

git stash 清空工作区和暂存区
git fetch 
git pull 同步服务器代码(pull == fetch + merge FETCH_HEAD)

**********代码合并**********
git merge
    git merge branchname;合并本地分支
    git merge origin/remote_branch 合并远程分支
    如果出现冲突,需要手动修改,可以用git mergetool.
    解决冲突的时候可以用到git diff,解决完之后用git add添加,即表示冲突已经被resolved.
    Git merge –no-ff 可以保存你之前的分支历史。能够更好的查看 merge历史,以及branch 状态。
    忽略回车符合并: git merge branchname -s recursive -Xignore-space-at-eol -X renormalize

git rebase
     --rebase不会产生合并的提交,它会将本地的所有提交临时保存为补丁(patch),放在”.git/rebase”目录中,然后将当前分支更新到最新的分支尖端,最后把保存的补丁应用到分支上.
     rebase的过程中,也许会出现冲突,Git会停止rebase并让你解决冲突,在解决完冲突之后,用git add去更新这些内容,然后无需执行commit,只需要:
     git rebase --continue就会继续打余下的补丁.
     git rebase --abort将会终止rebase,当前分支将会回到rebase之前的状态.
     
**********分支管理**********
创建分支  git branch branchname
从远程分支创建分支:git checkout -b local_branch origin/remote_branch
分支切换   git checkout branchname
创建并切换 git checkout -b branchname
删除分支 git branch -d branchname (强行删除分支:git branch -D branchname)
提交分支到远程 git push origin local_branch:remote_branch (提交当前分支git push origin branchname)
设置分支对应的远程分支:git branch --set-upstream local_branch origin/remote_branch
删除远程分支 git push origin :branchname(git push origin --delete branchname)

**********代码提交**********
git commit(将暂存区的所有内容提交到当前分支)
git commit -a (添加当前工作区所有文件到暂存区并提交到当前分支)
添加提交注释:git commit -a -m "commit info"
修改最近一次的提交:git commit -a -amend
git commit –allow-empty -m “this is a test” 进行一次空提交
git reset HEAD^ –-hard 删除刚才的空提交
提交代码到远程
git push
本地覆盖远程提交(慎用)
git push origin branch-name --force

**********日志管理**********
git log 查看提交记录
git reflog 用来记录你的每一次命令
--pretty=oneline 让提交记录只显示commit ID 和message
git log --oneline --graph --decorate

**********git代码还原**********
#将本地的状态回退到和远程的一样
git reset –-hard origin/master
#还原为上一次提交,^代表往前推的head数
git reset –-hard HEAD^
git reset –-hard {headId}
git checkout -- file(可以丢弃工作区的修改,就是让这个文件回到最近一次git commit或git add时的状态)
git reset HEAD file可以把暂存区的修改撤销掉(unstage),重新放回工作区
git rm file 删除一个文件,可用(git checkout -- file 还原)
场景1:当你改乱了工作区某个文件的内容,想直接丢弃工作区的修改时,用命令git checkout -- file。
场景2:当你不但改乱了工作区某个文件的内容,还添加到了暂存区时,想丢弃修改,分两步,第一步用命令git reset HEAD file,就回到了场景1,第二步按场景1操作。
git rm用于删除一个文件。如果一个文件已经被提交到版本库,那么你永远不用担心误删,但是要小心,你只能恢复文件到最新版本,你会丢失最近一次提交后你修改的内容

**********patch**********
使用git format-patch(生成git专用patch,一次commit一个patch):
    生成patch:
        当前分支所有超前master的提交:git format-patch -M master
        某次提交以后的所有patch:git format-patch 4e16 4e16指的是commit名
        从根到指定提交的所有patch:git format-patch --root 4e16
        某两次提交之间的所有patch:git format-patch 365a..4e16  365a和4e16分别对应两次提交的名称
        某次提交(含)之前的几次提交:git format-patch –n 07fe --n指patch数,07fe对应提交的名称
        单次提交即为:git format-patch -1 07fe
        备注:git format-patch生成的补丁文件默认从1开始顺序编号,并使用对应提交信息中的第一行作为文件名。如果使用了-- numbered-files选项,则文件名只有编号,不包含提交信息;如果指定了--stdout选项,可指定输出位置,如当所有patch输出到一个文件;可指定-o <dir>指定patch的存放目录;
    应用patch:
        先检查patch文件:git apply --stat newpatch.patch
        检查能否应用成功:git apply --check newpatch.patch
        打补丁:git am --signoff < newpatch.patch
      patch应用失败解决步骤:
        1. 使用 git apply --reject PATCH 强行应用不冲突的部分; 
        2. 手动修改未合并部分,并通过 git add files 添加修改(根据.rej文件进行修改,解决完后删除.rej文件); 
        3. 通过 git am --resolved 解决冲突并回复commit信息。
使用git diff
  生成patch:
    git diff HEAD  > patch 工作版本(Working tree)和HEAD的差别
    git diff  --cached > patch 暂存起来的文件(staged)和上次提交时的快照之间(HEAD)的差异
    git diff  branchname --cached > patch 两个分支的比较
 应用patch:
        先检查patch文件:git apply --stat newpatch.patch
        检查能否应用成功:git apply --check newpatch.patch
        应用patch:git apply patch
    patch应用失败解决步骤:
        1. 使用 git apply --reject PATCH 强行应用不冲突的部分; 
        2. 手动修改未合并部分(根据.rej文件进行修改,解决完后删除.rej文件); 
        3. 提交patch应用结果:git common -a -m '应用patch备注';
特此总结记录,方便自己以后回顾。

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值