Git基础
速查表

配置
1 2 3 4 5 6 7 8 9 10 11
| git config --list
git config -e [--global]
git config [--global] user.name "[name]" git config [--global] user.email "[email address]"
git config user.name [name];
git config user.email [email address]
|
创建
创建新仓库
1 2 3 4 5 6
| git init
git commit -m "first commit" git branch -M master git remote add origin [url] git push -u origin master
|
创建版本库
创建根目录 mkdir git
将目录变成git可管理的仓库 git init
把文件添加到版本库
把文件添加到仓库 git add lalala.txt 可反复使用 添加多个文件
把文件提交到仓库 git commit -m “可以输入任意内容,最好是本次提交的说明”
版本回退
查看lalala.txt文件一共有几个版本 git log git log –pretty=oneline (此时已有两个版本)
回退到版本1 git reset –hard HEAD^ (上一个版本是HEAD^,上上一个版本是HEAD^^,往上一百个白本写成HEAD~100)
重返版本2 git reset –hard (commit id) (可用git reflog查看)
文件管理器中删除文件 rm lalala.txt
版本库删除 git rm git commit
版本库恢复(删错了) git checkout – lalala.txt
上传已有仓库
1 2 3
| git remote add origin [url] git branch -M master git push -u origin master
|
添加远程库
创建SSH Key ssh-keygen -t rsa -C “youremail@example.com“
在用户主目录里找到.ssh目录,里面有id_rsa和id_rsa.pub两个文件,这两个就是SSH Key的秘钥对,id_rsa是私钥,不能泄露出去,id_rsa.pub是公钥,可以放心地告诉任何人。
登陆GitHub,打开“Account settings”,“SSH Keys”页面 add ssh key
git remote add origin [url]
第一次把本地库的所有内容推送到远程库 git push -u origin master
把本地master分支的最新修改推送至GitHub git push origin master
忽略特殊文件
在Git工作区的根目录下创建一个特殊的.gitignore文件,然后把要忽略的文件名填进去,Git就会自动忽略这些文件
克隆
克隆Master
克隆分支
1
| git clone -b branchA [url]
|
增加/删除文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| git add [file1] [file2] ...
git add [dir]
git add .
git add -p
git rm [file1] [file2] ...
git rm --cached [file]
git mv [file-original] [file-renamed]
|
代码提交
1 2 3 4 5 6 7 8 9 10 11 12 13
| git commit -m [message]
git commit [file1] [file2] ... -m [message]
git commit -a
git commit -v
git commit --amend -m [message]
git commit --amend [file1] [file2] ...
|
分支
查看分支
1 2 3 4 5 6
| git branch
git branch -r
git branch -a
|
创建分支
1 2 3 4 5 6
| git checkout -b feature/20200610/custom_native_landing_page // 或者
git branch dev git checkout dev
|
比较分支
1 2
| git diff [branchA] [branchB] git diff [branchA] [branchB] >>d:/diff/exportname.diff
|
master改动,更新dev
1 2 3 4 5
| git checkout master git pull git checkout dev git merge master git push -u origin dev
|
dev达到标准合并到master
1 2 3 4 5
| git checkout dev git pull git checkout master git merge dev git push -u origin master
|
查看信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| git status
git log
git log --stat
git log -S [keyword]
git log [tag] HEAD --pretty=format:%s
git log [tag] HEAD --grep feature
git log --follow [file] git whatchanged [file]
git log -p [file]
git log -5 --pretty --oneline
git shortlog -sn
git blame [file]
git diff
git diff --cached [file]
git diff HEAD
git diff [first-branch]...[second-branch]
git diff --shortstat "@{0 day ago}"
git show [commit]
git show --name-only [commit]
git show [commit]:[filename]
git reflog 可以得到cimmit id
git rebase [branch]
|
远程同步
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| $ git remote update --更新远程仓储
$ 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
$ git push [remote] --all
|
撤销
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| $ git checkout [file]
$ git checkout [commit] [file]
$ git checkout .
$ git reset [file]
$ git reset --hard
$ git reset [commit]
$ git reset --hard [commit]
$ git reset --keep [commit]
$ git revert [commit]
$ git stash $ git stash pop
|
其他
参考文献
git命令大全