记录Git的安装过程、常用的命令和GitHub的使用。

安装
去官网下载
双击安装,默认选项就行。
在Mac下,Xcode内置了Git,只要装Command Line Tools就可以了,安装命令xcode-select --install,查看是否安装了的命令是xcode-select -p,如果出现了/Applications/Xcode.app/Contents/Developer就表示已经安装了。
设置
设置用户名和邮箱
1 | git config --global user.name "Mark" |
global选项表示这些设置信息是全局的,所有的项目默认使用这些设置。如果不想使用这些设置,只要在特定的项目去掉--global重新设置即可。
设置命令的高亮显示
1 | git config --global color.ui true |
设置SSH
如果通过SSH来链接远程仓库的话,需要设置SSH。使用HTTPS则不用设置,每次push会提示输入用户名和密码。
在Git命令行下输入1
ssh-keygen -t rsa -C "shuyumark@gmail.com"
回车,会提示保存位置,原先没有生成过SSH密钥可以直接默认回车就好。然后提示输入passphrase,就是密码一样的东西,有这个会更加安全,但是我这里就直接空着了,回车完成。
然后将/Users/Mark/.ssh/id_rsa.pub中的内容添加到GitHub中.
然后测试
输入ssh -T git@github.com回车
一开始会有警告,输入yes回车就可以了。如果出现了自己的用户名和
1 | You've successfully authenticated, but GitHub does not provide shell access. |
就表明成功了。
命令
git init
用法
1 | git init |
初始化一个本地仓库,在项目文件下会产生一个.git的隐藏目录。
详见git init
git clone
用法
1 | git clone <url> <dir> |
<url>表示远程仓库的URL,<dir>表示将要拷贝到本地的目录名,可以省略,省略后本地目录名就是远程仓库的项目名称。
将远程仓库拷贝到本地。
git add
用法
1 | git add <filename>|. |
<filename>表示文件名,.表示所有文件。
将文件添加到stage area或者说index。
详见git add
git rm
用法
1 | git rm [--cached] <filename> |
[--cached]加上该参数表示文件移出Git。不加该参数表示删除该文件,也从文件系统中删除该文件。
详见git rm
git commit
用法
1 | git commit -m <msg> |
将index中的文件提交到本地仓库,<msg>表示提交信息。
git status
用法
1 | git status |
查看当前分支的仓库信息。
git branch
用法
1 | git branch [-d] [<branch-name>] |
git branch不加任何参数,表示显示所有的分支,当前分支前会加*显示。git branch <branch-name>表示新建名为branch-name的分支。git branch -d <branch-name>表示删除名为branch-name的分支。
git checkout
用法
1 | git checkout [-b] <branch-name> |
不加-b参数表示切换到branch-name分支。加上-b参数表示新建branch-name分支并切换到该分支。
git merge
用法
1 | git merge <branch-one> <branch-two> ... |
将branch-one和branch-two分支合并到当前分支。
git reset
用法
1 | git reset [--soft] [--hard] [<commit>] |
git reset表示将index恢复到上一次提交时的样子,此时,index是没有文件准备提交的。git reset HEAD <file>表示将file文件从index移到working directory。git reset --soft <commit>表示将commit这一次提交的内容恢复到index。git reset --hard <commit>将当前分支恢复到commit提交后的样子。
git rebase
用法
1 | git rebase <branch> |
合并分支的另一种方式,表示将branch分支合并到当前分支。如果合并的时候出现冲突,可以解决冲突后git rebase --continue继续合并,或者跳过或者终止合并。
git stash
用法
1 | git stash |
git stash表示暂时保存未提交的修改,这样,就可以切换到其他分支了。git stash apply <stash>表示恢复到指定的stash,不加参数就默认恢复到最近的一次stash。
git tag
用法
1 | git tag [-a] <tag-name> [-m <msg>] |
git tag -a <tag-name> -m <msg>表示建一个标签,会生成一个tag对象。git tag <tag-name>表示建一个标签,单不会生成一个tag对象。
详见git tag
git fetch
用法
1 | git fetch <remote> |
表示从远程仓库获取更新。
git pull
用法
1 | git pull <remote> <branch> |
将远程仓库的branch分支合并到本地当前分支。相当于git fetch <remote>和git merge <remote>/<branch>的组合。
详见git pull
git push
用法
1 | git push [<remote>] [<branch>] |
将本地仓库推送到远程仓库。
详见git push
git remote
用法
1 | git remote [-v] [add] [rename] [remove] [set-url] |
不加任何参数表示显示远程仓库。-v参数表示显示详细信息,add表示添加远程仓库,rename表示修改远程仓库的名字,remove表示删除远程仓库,set-url表示更改远程仓库的URL。
git log
用法
1 | git log |
查看提交日志,有很详细配置参数。
详见git log
git diff
用法
1 | git diff [--cached] [branch1..branch2] |
git diff表示working directory和index之间的差别。git diff --cached表示index与last commit直接的差别。git diff HEAD表示working directory和last commit之间的差别。git diff branch1..branch2表示branch1和branch2之间的差别。
详见git diff
Github
在Github如何进行pull request?
首先要fork对方的项目,之后你就能看到一个在你名下的一个和对方项目一模一样的项目。
然后,在本地clone你名下的那个项目
1 | git clone your-git-repo |
要从原仓库获得更新
1 | git remote add upstream origin-git-repo |
更新
1 | git fetch upstream |
推送到自己的仓库
1 | git push origin master |
最后在建一个pull request。