# git操作

  • clone代码

    git clone -b 指定分支 ssh://网址/项目名.git 本地项目名
    
  • 创建本地分支

    #使用默认分支创建
    git branch 分支名
    #指定分支创建本地分支
    git branch 本地分支名 远程分支名
    
  • 常用的组合操作(全部提交并push到远程)

    git add .; git commit -m '这是一次提交';git push;
    
  • 切换分支

    git checkout 分支名
    
  • 创建并切换分支

    git checkout -b 本地新分支名
    #或
    git checkout -b 本地新分支名 远程分支名
    
  • 删除本地分支

    git branch -d 本地分支名
    
  • 删除远程分支

    git push --delete origin 远程分支名
    
  • 提交本地分支到远程仓库

    git push origin 本地分支名
    
  • 查看远程分支

    git branch -r
    
  • 创建本地分支并关联

    git checkout -b 本地分支 origin/远程分支
    
  • 远程分支志本地分支关联

    git branch --set-upstrem-to origin/远程分支 本地分支
    
  • 显示当前HEAD上最近一次提交(commit)

    # git commit -a ##刚提交了一次变化,要查看提交了哪些内容
    git show
    #或者
    git log -n1 -p
    
  • 修改提交信息(commit message)

    #写错提交信息时,还没push时,通过下面命令修改
    git commit -amend --only -m '新的信息'
    #或者(交互式)
    git commit -amend --only
    #已经push后再更改提交信息时,需要force push
    git push -f
    
  • 删除最后提交

    #删除只commit的提交
    git reset --soft HEAD@{1}
    #删除已经push的最后一次提交
    git reset HEAD^ --hard
    git push -f [remote] [branch]
    
  • 从一个提交里移除一个文件

    git checkout HEAD^ myfile
    git add -A
    git commit --amend
    
  • 删除任意提交(慎重)

    git rebase --onto SHA1_OF_BAD_COMMIT^ SHA1_OF_BAD_COMMIT
    git push -f [remote] [branch]
    
  • 还原不小心硬重置(git reset --hard)前的内容

    #不小心硬重置(hard reset)了
    git reset --hard
    #列出所有的commit
    git reflog
    #找回想要的提交
    git reset --hard SHAxxxx
    
  • 暂存(add)的内容添加到上一次提交(commit)

    git add newfile.txt
    git commit --amend
    
  • 更改文件名的大小写

    git mv -f file.java File.java