Git常用指令总结

git分区
这是我理解的git的分区,包括工作区,暂存区,本地仓库和远程仓库

  • git config

    git相关设置

    • git config user.name  查看git当前用户名
    • git config user.email  查看git当前用户邮箱
    • git config [–global] user.name “[name]”  设置git当前用户名
    • git config [–global] user.email “[email]”  设置git 当前用户邮箱

  • git clone

    克隆版本库,将远程仓库代码克隆到本地

    • git clone [url]   url是远程仓库的git地址

  • git add

    将工作区代码改动添加至缓存区

    • git add [file]    指定改动文件添加到缓存区
    • git add .      全部改动文件添加到缓存区
    • git add [dir]    指定目录及其子目录添加至缓存区

  • git commit

    将缓存区代码推至本地仓库

    • git commit -m”message” 提交暂存区至仓库区,message为此次提交的信息,需开发者根据本次改动进行手动输入
    • git commit [file] -m”message”  指定文件提交
    • git commit -a -m”message”  提交工作区自上次commit后有变动的所有代码文件,等价于“ git add . ”+“ git commit -m”message” ”

  • git push

    将本地仓库代码推送至远程仓库

    • git push    推送当前分支至对应的远程仓库
    • git push origin [branch_name]   指定远程仓库名和分支名
    • git push origin –delete [branch_name]   删除远程分支

  • git pull

    从远程仓库拉取代码到本地仓库

    • git pull 拉取当前分支对应的远程分支的代码
    • git pull –rebase  以rebase合并代码的方式拉取远程代码岛本地

    git pull 和 git pull –rebase 的区别

    • git pull 拉取代码时,相当于执行了 git fetch + git merge FETCH_HEAD
    • git pull –rebase 拉取代码时,相当于执行了 git fetch + git rebase FETCH_HEAD
    • git rebase 和 git merge 的区别请点击查看

  • git branch

    分支管理

    • git branch  查看所有本地分支
    • git branch -r  查看所有远程分支 (-a 列出本地和远程所有分支)
    • git branch –set-upstream-to=origin/[branch_name] [branch_name]  本地分支关联远程分支
    • git branch [branch_name]  新建一个分支
    • git branch -d [branch_name]  删除指定本地分支(需先切换至非删除分支)

  • git checkout

    分支切换

    • git checkout [branch_name]   切换到指定分支
    • git checkout -b [branch_name]  新建一个分支并切换到新建的分支
    • git checkout -b [branch_name] origin/[branch_name]  新建本地分支后自动切换到该分支,并与远程分支建立联系

  • git merge

    合并分支

    • git merge [branch_name]  合并指定分支到当前分支
  • git rebase

    分支变基

    • git rebase [branch_name]  合并指定分支代码到当前分支
  • git remote

    操作远程仓库

    • git remote -v 显示所有远程仓库
    • git remote show [remote] 显示某个远程仓库信息
    • git remote add [short_name] [url] 添加远程版本库
    • git remote rm [name] 删除远程仓库
    • git remote rename [old_name] [new_name] 修改仓库名