:sparkles:增加新的命令:octocat:Git常用命令集合
GitHub 是为开发者提供 Git 仓库的托管服务。这是一个让开发者与朋友、同事、同学及陌生人共享代码的完美场所。总结一下,GitHub 最大的特征是“面向人”
Git是一个“分布式版本管理工具”,简单的理解版本管理工具:大家在写东西的时候都用过“回撤”这个功能,但是回撤只能回撤几步,假如想要找回我三天之前的修改,光用“回撤”是找不回来的。而“版本管理工具”能记录每次的修改,只要提交到版本仓库,你就可以找到之前任何时刻的状态(文本状态)
统一概念
git add 改动的文件名
,此次改动就放到了‘暂存区’git commit 此次修改的描述
,此次改动就放到了’本地仓库’,每个commit,我叫它为一个‘版本’git push 远程仓库
,此次改动就放到了‘远程仓库’(GitHub等)branch
完成某个任务,合并后再删掉分支,过程更安全。git log
,最上面那行commit xxxxxx
,后面的字符串就是commit-id下面的内容就是列举了一些常用的Git命令和小技巧,参考tips项目,和廖雪峰老师的git网站
# 全局配置
$ git config --global user.name "username" #你的昵称
$ git config --global user.email "email@address.com" #你的邮箱
# 生成密钥
$ ssh-keygen -t rsa -C "email@address.com" #上面的邮箱
$ git config --global --unset <entry-name> # {user.name|user.email}
$ git help -g
$ git clone https://github.com/user/repo.git # Use HTTPS
$ git clone git@github.com:user/repo.git # Use SSH
$ git clone -b <branch-name> --single-branch https://github.com/user/repo.git
#把工作时的所有变化提交到暂存区
$ git add .
#更新已经提交到暂存区的文件(git add --update的缩写)
$ git add -u
#是上面两个功能的合集(git add --all的缩写)
$ git add -A
$ git commit -m "描述"
$ git push
$ git pull
$ git log
#也就是把所有的改动都重新放回工作区,并清空所有的commit,这样就可以重新提交第一个commit了
$ git update-ref -d HEAD
$ git revert <commit-id>
和revert的区别是:reset命令会抹去某个commit id之后的所有commit
$ git reset <commit-id> #默认就是-mixed参数。
$ git reset –mixed HEAD^ #回退至上个版本,它将重置HEAD到另外一个commit,并且重置暂存区以便和HEAD相匹配,但是也到此为止。工作区不会被更改。
$ git reset –soft HEAD~3 #回退至三个版本之前,只回退了commit的信息,暂存区和工作区与回退之前保持一致。如果还要提交,直接commit即可
$ git reset –hard <commit-id> #彻底回退到指定commit-id的状态,暂存区和工作区也会变为指定commit-id版本的内容
$ git commit --amend
$ git log Branch1 ^Branch2
$ git log --show-signature
$ git log --pretty=oneline --graph --decorate --all
$ git log
#通过grep查找,given-text:所需要查找的字段
$ git log --all --grep='<given-text>'
$ git checkout - #分支名
$ git branch --merged master | grep -v '^\*\| master' | xargs -n 1 git branch -d
$ git branch -vv
关联远程分支之后,使用git branch -vv
就可以展示关联的远程分支名
同时推送到远程仓库直接使用:git push
,就不需要指定远程仓库了
$ git branch -u origin/mybranch #分支名
或者在push的时候加上-u
参数
$ git push origin/mybranch -u
$ git branch -r #-r参数相当于:--remote
$ git branch -a #-a参数相当于:--all
$ git checkout -b <branch-name>
$ git checkout -b <branch-name> origin/<branch-name>
$ git branch -d <local-branchname>
$ git push origin --delete <remote-branchname>
或者
$ git push origin :<remote-branchname>
$ git branch -m <new-branch-name>
$ git show <branch-name>:<file-name>
$ git tag #查看所有标签
#展示当前分支的最近的tag
$ git describe --tags --abbrev=0 #没有标签会提示fatal: No names found, cannot describe anything.
$ git tag <version-number> #如v1.0
#默认tag是打在最近的一次commit上,如果需要指定commit打tag
$ git tag -a <version-number> -m "v1.0 发布(描述)" <commit-id> #commit-id可忽略
#要保证本地创建好了标签才可以推送标签到远程仓库:
$ git push origin <local-version-number>
#一次性推送所有标签,同步到远程仓库:
$ git push origin --tags
$ git tag -d <tag-name>
#删除远程标签需要先删除本地标签,然后再执行下面的命令:
$ git push origin :refs/tags/<tag-name>
一般上线之前都会打tag,就是为了防止上线后出现问题,方便快速回退到上一版本
#下面的命令是回到某一标签下的状态:
$ git checkout -b <branch-name> <tag-name> #分支名 #标签名
$ git checkout -- <file-name>
#放弃所有的修改
$ git checkout .
$ git reset HEAD <file-name>
$ rm <file-name> #只删除工作区的文件
$ git rm <file-name> #删除暂存区中的文件
$ git rm -f <file-name> #同时删除工作区和暂存区中的文件
$ git rm --cached <file-name> #删除暂存区的文件,不删除工作区的文件
$ git blame <file-name>
$ git reflog
$ git commit --amend --author='Author Name <email@address.com>'
$ git remote set-url origin <URL>
$ git remote add origin <remote-url>
$ git remote
$ git fetch origin pull/<id>/head:<branch-name>
$ git whatchanged --since='2 weeks ago'
#相当于保存修改,但是重写commit历史
$ git checkout --orphan <branch-name>
$ git config --global alias.<handle> <command>
#比如:git status 改成 git st
$ git config --global alias.st status
$ git stash
$ git stash -u
$ git stash list
$ git stash apply <stash@{n}>
$ git stash pop
$ git stash clear
$ git checkout <stash@{n}> -- <file-path>
$ git ls-files -t
$ git ls-files --others
$ git ls-files --others -i --exclude-standard
#可以用来删除新建的文件。如果不指定文件文件名,则清空所有工作的untracked文件
$ git clean <file-name> -f
#可以用来删除新建的目录,这个命令也可以用来删除untracked的文件
$ git clean <directory-name> -df
$ git bundle create <file> <branch-name>
#新建一个分支,分支内容就是上面bundle create命令导出的内容
$ git clone repo.bundle <repo-dir> -b <branch-name>
$ git rebase --autostash
$ git diff --word-diff
$ git clean -X -f
#默认为当前目录的config
$ git config --local --list (当前目录)
$ git config --global --list (全局)
$ git status --ignored
#关闭 track 指定文件的改动,也就是 Git 将不会在记录这个文件的改动
$ git update-index --assume-unchanged path/to/file
#恢复 track 指定文件的改动
$ git update-index --no-assume-unchanged path/to/file
#不再将文件的权限变化视作改动
$ git config core.fileMode false
# 最新的放在最上面
$ git for-each-ref --sort=-committerdate --format='%(refname:short)' refs/heads/
#不添加参数,默认是-mixed
$ git reset <file-name>
$ git push -f <remote-name> <branch-name>
$ git cherry-pick commitId