Git常用命令

 git branch -a   列出所有分支(本地+远程)
 git checkout -b dev origin/dev  作用是checkout远程的dev分支,在本地起名为dev分支,并切换到本地的dev分支
 git checkout -b test 创建并切换到test分支
 git branch -D test  删除本地名称为test的分支
 git push origin :heads/test, git push origin :test  删除远程名称为test的分支
 git add .  // '.'表示所有文件  将工作目录中的修改放到暂存区
 git commit -m  'msg' // 将暂存区中的修改合并到本地分支
 git push  // 将本地分支修改发布到远程分支
 git status // 查看工作目录文件修改状态
 git diff file  // 查看文件差异
 查看远程分支: git branch -r
 拉取远程test分支并创建本地同名分支: git checkout -b test origin/test
 修改commit提交消息:git commit --amend (只适用于还没有push的时候)

 为本地当前分支设置upstream (本地分支与远程分支关联, 名称相同最好)
 git push --set-upstream origin remote_branch_name
 git push -u origin remote_branch_name


 配置用户名以及邮箱
 git config --global user.name 'your name'
 git config --global user.email 'your email'
 配置ssh key:
 参考:
 https://zm10.sm-tc.cn/?src=l4uLj8XQ0IiIiNGem5KWkc7Pz8%2FP0ZyQktCbkJyKkpqRi9DKzMjL0ZeLkpM%3D&uid=2363c847da0ff323653226af4e927cf2&hid=4816934e0259268c11659b11ee227463&pos=2&cid=9&time=1515043826611&from=click&restype=1&pagetype=0000004000000402&bu=structure_web_info&query=Git教程&mode=&v=1&uc_param_str=dnntnwvepffrgibijbprsvdsdichei

上线流程

 1) 代码合并到master  
     git checkout master
     git merge develop(或者其它分支)
     git push

 2) 给master打个标签
     git tag -a 标签名称(如v1.0) -m "标签信息"
     git push origin --tags  把本地的打的标签全部提交到远程仓库
     git tag -d v1.0 删除本地名称为v1.0的标签
     git push origin :refs/tags/v1.0 删除远程名称为v1.0的标签
     git tag -l -n  查看所有标签以及其注解信息

git代码库回滚: 指的是将代码库某分支退回到以前的某个commit id

1) 本地代码库回滚
   git reset --hard commit-id :回滚到commit-id,讲commit-id之后提交的commit都去除
   git reset --hard HEAD~3:将最近3次的提交回滚

2) 远程代码库回滚
   1、git checkout the\_branch
   2、git pull
   3、git branch the\_branch\_backup //备份一下这个分支当前的情况 (新创建的分支代码与创建分支时所在分支代码一致)
   4、git reset --hard the\_commit\_id //把the\_branch本地回滚到the\_commit\_id
   5、git push origin :the\_branch //删除远程 the\_branch
   6、git push origin the\_branch //用回滚后的本地分支重新建立远程分支
   7、git push origin :the\_branch\_backup //如果前面都成功了,删除这个备份分支

feature分支开发流程

1. git checkout base_branch_name(基于哪个分支创建feature)
2. git pull 拉取一下当前分支上的代码
3. git branch feature/test
4. git checkout feature/test
5. git push --set-upstream origin feature/test (本地feature到远程关联)
    开始开发 git add,commit, push...
6. 合并到dev分支
    git checkout develop
    git merge feature/test
    git push

git pull vs (git fetch + git rebase)

git pull = git fetch + git merge
同一个分支不要使用git pull(存在git merge), 会产生菱形提交 引起错误。 只有在不同分支之间进行合并的时候,才使用 git merge!

同一个分支代码合并,使用 (git fetch + git rebase)代替 git pull

results matching ""

    No results matching ""