Git简单使用


Git是一个免费的开源分布式版本控制系统,旨在快速高效地处理从小型项目到大型项目的所有问题。Git易于学习,占用空间小,闪电般的快速性能。它超越了Subversion,CVS等SCM工具 ,Perforce和ClearCase,具有“廉价“的本地分支,方便的暂存区域和多个工作流等功能。

1. 初始 设置姓名和邮箱地址

$ git config --global user.name "Firstname Lastname"
$ git config --global user.email "your_email@example.com"

2. 设置 SSH Key

$ ssh-keygen -t rsa -C "your_email@example.com"
Generating public/private rsa key pair.
Enter file in which to save the key
(/Users/your_user_directory/.ssh/id_rsa): 按回车键
Enter passphrase (empty for no passphrase): 输入密码
Enter same passphrase again: 再次输入密码
Your identification has been saved in /Users/your_user_directory/.ssh/id_rsa.
Your public key has been saved in /Users/your_user_directory/.ssh/id_rsa.pub.
The key fingerprint is:
fingerprint值 your_email@example.com
The key's randomart image is:
+--[ RSA 2048]----+
| .+ + |
| = o O . |
......

粘贴id_rsa.pub

测试是否添加成功:

$ ssh -T git@github.com
The authenticity of host 'github.com (207.97.227.239)' can't be established.
RSA key fingerprint is fingerprint值 .
Are you sure you want to continue connecting (yes/no)? 输入yes

出现以下内容则表示成功

Hi hirocastest! You've successfully authenticated, but GitHub does not
provide shell access.

3. clone 已有仓库

$ git clone git@github.com:hirocastest/Hello-World.git
Cloning into 'Hello-World'...
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.

4. 状态查看命令

$ git status

5. 提交

$ git add [file name]
eg:
$ git add hello.c
$ git add ./*       //表示提交当前目录全部文件

6. 保存仓库的历史记录

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

7. 进行push

$ git push
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 328 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:hirocastest/Hello-World.git
46ff713..d23b909 master -> master

8. 初始化仓库

$ mkdir git-tutorial
$ cd git-tutorial
$ git init
Initialized empty Git repository in /Users/hirocaster/github/github-book
/git-tutorial/.git/

9. 查看操作日志

$ git reflog
4096d9e HEAD@{0}: commit: Fix B
fd0cbf0 HEAD@{1}: checkout: moving from master to fix-B
fd0cbf0 HEAD@{2}: reset: moving to fd0cbf0d4a25f747230694d95cac1be72d33441d
83b0b94 HEAD@{3}: merge feature-A: Merge made by the 'recursive' strategy.
fd0cbf0 HEAD@{4}: checkout: moving from feature-A to master
8a6c8b9 HEAD@{5}: checkout: moving from master to feature-A
fd0cbf0 HEAD@{6}: checkout: moving from feature-A to master
8a6c8b9 HEAD@{7}: commit: Add feature-A
fd0cbf0 HEAD@{8}: checkout: moving from master to feature-A
fd0cbf0 HEAD@{9}: commit: Add index
9f129ba HEAD@{10}: commit (initial): First commit

10. 回溯历史版本

$ git reset --hard fd0cbf0d4a25f747230694d95cac1be72d33441d
HEAD is now at fd0cbf0 Add index

11.参考文献

GitHub入门与实践 /( 日) 大塚弘记著;支鹏浩,刘斌译. – 北京:人民邮电出版社,2015.7

This entry was posted in 默认分类 and tagged . Bookmark the permalink.