原文:廖雪峰的Git教程
1.设置用户名和邮箱
1 | git config --global user.name "Your Name" |
2.pwd
命令用于显示当前目录
1 | mkdir learngit |
3.初始化仓库,生成.git文件
1 | git init |
4.将文件添加到暂存区
1 | git add <file> |
5.提交,-m
后面输入的是本次提交的说明
1 | git commit -m <message> |
6.查看当前git库状态
1 | git status |
7.git diff
顾名思义就是查看difference,修改了哪些内容
1 | git diff <file> |
8.查看日志
1 | git log |
9.查看日志massage
1 | git log --pretty=oneline |
10.会退到上一个版本
Git必须知道当前版本是哪个版本,在Git中,用HEAD表示当前版本,也就是最新的提交1094adb...
(注意我的提交ID和你的肯定不一样),上一个版本就是HEAD^
,上上一个版本就是HEAD^^
,当然往上100个版本写100个^比较容易数不过来,所以写成HEAD~100
。1
2
3
4
5 git reset --hard HEAD^
HEAD is now at e475afc add distributed
或
git reset --hard 1094a
HEAD is now at 83b0afe append GPL
11.查看命令日志
1 | git reflog |
12.把文件在工作区的修改全部撤销,其实是用版本库里的版本替换工作区的版本
1 | git checkout -- <file> |
13.把暂存区的修改撤销掉
1 | git reset HEAD <file> |
14.删除
1 | git rm test.txt |
15.创建SSH key,在用户主目录下,看看有没有.ssh目录,如果有,再看看这个目录下有没有id_rsa
和id_rsa.pub
这两个文件
1 | ssh-keygen -t rsa -C "youremail@example.com" |
16.github管理本地仓库,并将本地仓库内容推送到远程库中
由于远程库是空的,我们第一次推送master分支时,加上了-u参数,Git不但会把本地的master分支内容推送的远程新的master分支,还会把本地的master分支和远程的master分支关联起来,在以后的推送或者拉取时就可以简化命令。1
2
3
4
5
6
7
8
9
10
11
12 git remote add origin https://github.com/Bored-AnnoYing/learngit.git
git push -u origin master
Enumerating objects: 13, done.
Counting objects: 100% (13/13), done.
Delta compression using up to 4 threads.
Compressing objects: 100% (8/8), done.
Writing objects: 100% (13/13), 1.04 KiB | 55.00 KiB/s, done.
Total 13 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), done.
To https://github.com/Bored-AnnoYing/learngit.git
* [new branch] master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.
17.本地提交到远程库
1 | git push origin master |
18.克隆一个本地库
1 | git clone https://github.com/Bored-AnnoYing/gitskills.git |
19.创建并切换到分支,-b
参数表示创建并切换
1 | git checkout -b dev |
20.查看当前分支,git branch
命令会列出所有分支,当前分支前面会标一个*
号
1 | git branch |
21.合并指定分支到当前分支, 请注意--no-ff
参数,表示禁用Fast forward
,因为本次合并要创建一个新的commit,所以加上-m
参数,把commit描述写进去
1 | git merge dev |
22.删除分支
1 | git branch -d dev |
23.将当前工作“储藏”起来,等以后恢复现场后继续工作
1 | git stash |
24.查看“储藏”起来的工作
1 | git stash list |
25.恢复并删除stash内容
1 | git stash apply |
26.强制删除分支
1 | git branch -D feature-vulcan |
27.查看远程仓库信息, -v
显示更详细
1 | git remote -v |
28.从远程仓库抓取到本地,如果git pull
提示no tracking information
,则说明本地分支和远程分支的链接关系没有创建,用命令git branch --set-upstream-to <branch-name> origin/<branch-name>
1 | git pull |
29.指定本地dev
分支与远程origin/dev
分支的链接
1 | git branch --set-upstream-to=origin/dev dev |
30.变基,rebase操作可以把本地未push的分叉提交历史整理成直线
1 | git rebase |
31.标签
==注意:标签总是和某个commit挂钩。如果这个commit既出现在master分支,又出现在dev分支,那么在这两个分支上都可以看到这个标签。 #ff0000==
1
2
3
4
5
6
7
8
9
10 git tag v1.0
使用commit id打标签
git tag v0.9 f52c633
创建带有说明的标签
git tag -a v0.1 -m "version 0.1 released" 1094adb
查看所有标签
git tag
32.查看标签信息
1 | git show <tagname> |
33.删除标签
1 | git tag -d v0.1 |
34.推送标签到远程仓库
1 | git push origin v1.0 |
35.推送所有标签到远程仓库
1 | git push origin --tags |
36.删除远程仓库标签,先从本地删除,然后,从远程删除。删除命令也是push
1 | git tag -d v0.9 |
忽略某些文件时,需要编写.gitignore
37.检查.gitignore
文件哪行做了限制
1 | git check-ignore -v App.class |
38.配置命令别名,--global
参数是全局参数,每个仓库的Git配置文件都放在.git/config
文件中
1 | git config --global alias.st status |