git详细命令

git详细命令

1. 配置git

ssh-keygen -t rsa -C "email"    创建SSH Key
cd ~/.ssh
cat id_rsa.pub   获取密钥

git config --list   
git config --global user.name "用户名"     全局配置
git config --global user.email "email"
git config --local user.name "用户名"      本地配置
git config --local user.email "email"

2. 远程操作

git init         初始化仓库
git clone [url]  克隆数据库
git remote add origin [url]    关联远程仓库
git remote update origin --prune  同步远程分支列表

3. 对文件操作

git add <file>  把文件修改添加到暂存区
git rm <file>  删除文件,包括暂存区和工作区

如果误删除了,可执行以下命令
$ git reset head <file>
$ git checkout -- <file>
git commit -m "注释"

提交更改,把暂存区的所有内容提交到当前分支
git commit -am的区别是git commit -m用于提交暂存区的文件(只提交添加的);git commit -am用于提交跟踪过的文件(修改过的也能提交)

4. 撤销操作

git reset --hard HEAD^   回退上一个版本
git log --pretty=oneline
git reset --hard 版本号

先从log获取从最近到最远的提交日志,再回退到指定版本号,当回退后想返回,可通过git reflog查看命令历史,获取需要返回的版本号

git commit --amend

如果commit了还没push,此命令可以重新提交,将之前的提交合并为一个

git checkout -- [file]

如果file修改后还没有被放到暂存区则变为和版本库一模一样的状态;
如果file已经添加到暂存区后又作了修改,则变为暂存区的状态。

git revert 

git revert用一次新的commit来回滚之前的commit,git reset是直接删除指定的commit

git fetch --all && git reset --hard origin/master && git pull

当回滚到某个版本,想删除本地回滚后的提交,使用上面语句

5. 比较差异

git diff

比较的是工作目录中当前文件和暂存区域快照之间的差异,也就是修改之后还没有暂存起来的变化内容

git diff --cached 
git diff --staged

效果一致,都是查看已暂存的将要添加到下次提交里的内容,也就是commit时候的内容

git diff HEAD -- <file>

可以查看工作区和版本库里面最新版本的区别

6. 标签

git tag      查看所有标签
git tag <tagname> commitid commitid不填默认HEAD
git tag -a <tagname> -m "注释" commitid
git tag -d <tagname>   删除本地标签
git push origin :refs/tags/<tagname>  删除远程标签
git reset --hard <tagname>  回滚到此标签

7. 分支

git branch -vva        查看详细分支,包括和远程分支的关联
git branch <name>      创建分支

git checkout <name>     切换分支
git checkout -b <name>   创建+切换分支

git branch -d <name>      删除本地分支,如果是-D表示强制删除
git push origin --delete 分支名称     删除远程分支

git checkout -b branch-name origin/branch-name 在本地创建分支并和远程关联
git branch --set-upstream 本地分支 origin/远程分支  将本地分支关联远程分支

git merge <branchname>      合并某分支到当前分支
git merge --no-ff -m "description" <branchname>  本次合并要创建新的commit
git rebase <branchname>      衍和某分支到当前分支

git cherry-pick commitid  可以只合并别的分支的某个提交记录而不是和整个分支合并

8. 日志

git log
git reflog   查看命令历史

常用参数:

选项描述
-p用来显示每次提交的内容差异
-数字几仅显示最近几次提交
--stat显示每次提交中修改的文件的统计信息
--shortstat仅显示--stat命令中已更改/插入/删除行
--name-only显示提交信息后修改的文件列表
--name-status显示受添加/修改/删除信息影响的文件列表
--abbrev-commit仅显示SHA-1校验和的前几个字符,而不是全部40个字符
--relative-date以相对格式显示日期(例如,“2周前”),而不是使用完整日期格式
--graph在日志输出旁边显示分支和合并历史记录的ASCII图
--pretty以备用格式显示提交。选项包括oneline,short,full,fuller和format(您指定自己的格式)
--oneline--pretty=oneline --abbrev-commit一起使用的简写

9. 提交/拉取代码

git pull --rebase 使用rebase是为了去掉Merge branch的提交信息
git push origin 远程分支名x:本地分支名
git push -u origin master  一般是第一次远端仓库为空

10. 存储

git stash      存储代码
git stash apply      恢复存储的代码
git stash pop      恢复的同时把stash内容也删了
git stash list    查询存储列表
git stash apply stash@{序号}     恢复指定的存储代码
git stash drop stash@{0}   删除指定的存储代码

11. Git 别名

git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值