Git常用命令

新建代码库
  • 在当前目录新建一个Git代码库
    • git init 初始化为master分支
  • 新建一个目录,将其初始化为Git代码库
    • git init [project-name]
  • 克隆远程代码库到本地
    • git clone [url]
Git配置
  • 显示当前的Git配置
    • git config --list
  • 设置系统用户
    • git config --system user.name "zhangsan"
    • git config --system user.email "zhangsan@163.com"
  • 设置全局用户
    • git config --global user.name "zhangsan"
    • git config --global user.email "zhangsan@163.com"
  • 设置当前项目用户
    • git config --local user.name "zhangsan"
    • git config --local user.email "zhangsan@163.com"
  • 设置不自动转换换行符
    • git config --global core.autocrlf false
添加、删除、撤销文件
  • 添加
    • git add [file1] [file2]...指定文件到暂存区(将已修改的文件添加到暂存区)
    • git add [dir] 添加指定目录的所有文件到暂存区
    • git add . 添加当前目录的所有文件到暂存区
  • 删除
    • git rm [file1] [file2]删除文件,并将这次删除操作记录暂存区(其实就是删除文件)
    • git rm --cached [file] 把文件从Git暂存区中移除,回到已修改状态(被commit的也能回到未add状态)
  • 撤销
    • git checkout [commitId] [file]恢复某一个commit的指定文件到暂存区和工作区
    • git reset [file/commitId]只是重置暂存区的指定文件与上一次commit保持一致,但是工作区不变
    • git reset --hard /[commitId]重置暂存区与工作区,与上一次commit保持一致或指定commit
    • git reset --keep [commit]重置当前Head为指定commit,但保持暂存区和工作区不变
    • git revert [commit] 后者的所有变化都将被前者抵消,并且应用到当前分支(回滚到指定commit)
    • git stash暂时将没有add的变化移除
    • git stash pop将之前移除的变化再移入(有冲突的时候可以先移除变化,吧代码拉下来再把,变化移入,删除多余代码)
代码提交
  • 提交暂存区中的更新到本地仓库
    • git commit -m [message]
  • 提交工作区自上一次commit之后的变化,直接到本地仓库
    • git commit -a
  • 使用一次新的commit,替代上一次的提交
    • git commit --amend -m [message] (如果代码没有任何新变化,则用来改写上一次commit的提交信息)
分支
  • 列出分支
    • 本地所有分支:git branch
    • 远程所有分支:git branch -r
    • 本地和远程所有分支:git branch -a
  • 新建分支
    • 新建一个分支,但依然停留在当前分支:git branch [branch-name]
    • 新建一个分支,并切换到该分支:git checkout -b [branch]
    • 新建一个分支,指向指定commit:git branch [branch] [commit]
    • 新建一个分支,与指定远程分支建立追踪关系:git branch --trach [branch] [remote-branch
    • 新建一个分支,指向某个tag:git checkout -b [branch] [tag]
  • 切换分支
    • 切换到指定分支,并更新工作区:git checkout [branch-name]
  • 合并
    • 合并指定分支到当前分支:git merge [branch]
    • 选择一个commit,合并进当前分支:git cherry-pick [commit]
  • 删除远程分支
    • git push origin --delete [branch-name]
    • git branch -dr [remote/branch]
标签
  • 列出所有tag
    • git tag
  • 新建tag
    • 在当前commit新建一个tag:git tag [tag-name]
    • 在指定commit新建一个tag:git tag [tag-name] [commit]
  • 删除
    • 删除本地tag:git tag -d [tag-name]
    • 删除远程tag:git push origin :refs/tags/[tag-name]
  • 查看tag信息
    • git show [tag]
  • 提交
    • 提交指定tag:git push [remote] [tag]
    • 提交所有tag:git push [remote] --tags
信息查看
  • 查看变更文件
    • git status
  • 显示当前分支的版本历史
    • git log
  • 显示commit历史,以及每次commit发生变更的文件
    • git log --stat
  • 搜索提交历史
    • git log -S [keyword]
  • 显示所有提交过的用户,按提交次数排序
    • git shortlog -sn
  • 显示指定文件是什么人在什么时间修改过
    • git blame [file]
  • 显示暂存区和工作区的差异
    • git diff
  • 显示暂存区和上一个commit的差异
    • git diff -cached [file]
远程同步
  • 下载远程仓库的所有变动
    • git fetch [remote]
  • 显示所有远程仓库
    • git remote -v
  • 显示某个远程仓库的信息
    • git remote show [remote]
  • 增加一个新的远程仓库,并命名
    • git remote add [shortname] [url]
  • 取回远程仓库的变化,并与本地分支合并
    • git pull [remote] [branch]
  • 上传本地指定分支到远程仓库
    • git push [remote] [branch]
  • 强行推送当前分支到远程仓库,即使有冲突
    • git push [remote] --force

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值