Git命令行的使用

Git是什么?

Git 属于分散型版本管理系统,是为版本管理而设计的软件。

远程仓库

创建公开密钥认证所需的ssh key

1
$ ssh-keygen -t rsa -C "your_email@example.com"
2
Generating public/private rsa key pair.
3
Enter file in which to save the key
4
(/Users/your_user_directory/.ssh/id_rsa): 按回车键
5
Enter passphrase (empty for no passphrase): 输入密码
6
Enter same passphrase again: 再次输入密码

id_rsa 文件是私有密钥,id_rsa.pub 是公开密钥。

添加公开密钥

登录github,点击右上角头像,选择Settings,再点击SSH and GPG keys,设置SSH keys。点击New SSH key 把id_rsa.pub 文件里的内容添加进去。完成以上设置后,就可以用手中的私人密钥GitHub 进行认证和通信了。让我们来实际试一试。

1
$ ssh -T git@github.com
2
The authenticity of host 'github.com (207.97.227.239)' can't be established.
3
RSA key fingerprint is fingerprint值 .
4
Are you sure you want to continue connecting (yes/no)? 输入yes
5
Hi hirocastest! You've successfully authenticated, but GitHub does not
6
provide shell access.

配置

1
git config --global user.name xxx #方便产品经理找(怼)你
2
git config --global user.email yyy #方便产品经理找(怼)你
3
git config --global push.default simple 
4
git config --global core.quotepath false #防止文件名变成数字
5
git config --global core.editor "vim" #使用vim编辑提交信息

基本操作

git init——初始化仓库

要使用Git 进行版本管理,必须先初始化仓库。

1
mkdir git-demo
2
cd git-demo
3
git init
4
Initialized empty Git repository in /Users/hirocaster/github/github-book
5
/git-tutorial/.git/

git status——查看仓库的状态

git status命令用于显示Git 仓库的状态。

1
$ git status
2
# On branch master
3
#
4
# Initial commit
5
#
6
nothing to commit (create/copy files and use "git add" to track)

git add——向暂存区中添加文件

1
$ git add README.md
2
$ git status
3
# On branch master
4
#
5
# Initial commit
6
#
7
# Changes to be committed:
8
# (use "git rm --cached <file>..." to unstage)
9
#
10
# new file: README.md
11
#

git commit——保存仓库的历史记录

git commit命令可以将当前暂存区中的文件实际保存到仓库的历史记录中。

1
$ git commit -m "first"
2
[master (root-commit) 9f129ba] first
3
1 file changed, 0 insertions(+), 0 deletions(-)
4
create mode 100644 README.md

-m 参数后的”First commit”称作提交信息,是对这个提交的概述。

git log——查看提交日志

git log命令可以查看以往仓库中提交的日志。

1
$ git log
2
commit 33c1c74e376fd66d0747a8093c4c73b7e9d6427a
3
Author: wang7211401 <wang7211401@163.com>
4
Date:   Thu Jul 6 16:04:15 2017 +0800
5
first

git diff——查看更改前后的差别

git diff命令可以查看工作树、暂存区、最新提交之间的差别。

git remote add——添加远程仓库

在GitHub 上创建的仓库路径为“git@github.com:用户名/git-tutorial.git”。现在我们用git remote add命令将它设置成本地仓库的远程仓库。

1
$ git remote add origin git@github.com:github-book/git-tutorial.git

git push——推送至远程仓库

推送至master 分支

1
$ git push -u origin master
2
Counting objects: 20, done.
3
Delta compression using up to 8 threads.
4
Compressing objects: 100% (10/10), done.
5
Writing objects: 100% (20/20), 1.60 KiB, done.
6
Total 20 (delta 3), reused 0 (delta 0)
7
To git@github.com:github-book/git-tutorial.git
8
* [new branch] master -> master
9
Branch master set up to track remote branch master from origin.

git pull——获取最新的远程仓库分支

git clone——获取远程仓库

git branch——显示分支一览表

git branch命令可以将分支名列表显示,同时可以确认当前所在分支。

1
$ git branch
2
* master

git checkout -b——创建、切换分支

1
$ git checkout -b feature-A
2
Switched to a new branch 'feature-A'

git merge——合并分支

1
$ git checkout master
2
Switched to branch 'master'
3
$ git merge --no-ff feature-A

git log –graph——以图表形式查看分支

git reset——回溯历史版本

1
$ git reset --hard 33c1c7

git reflog ——查看当前仓库执行过的操作的日志。

1
$ git reflog
2
33c1c74 HEAD@{0}: checkout: moving from feature-A to master
3
33c1c74 HEAD@{1}: checkout: moving from master to feature-A
4
33c1c74 HEAD@{2}: commit (initial): first

git rebase -i——压缩历史

git stash——用于保存和恢复工作进度

原则

1.git push 之前必须 git pull
2.git pull 之前必须 git commit
3.git commit 之前有时必须 git add