点击小眼睛开启蜘蛛网特效

git最快速入门指南

Git is what?

《git最快速入门指南》

git是分布式版本代码储存库,熟练使用它后,不管是一个团队还是个人的开发效率来说提升的作用都很大。重要性自然不多说,这篇文章以最快的方式学会使用最基本的git命令。
安装git从官网入手即可:https://git-scm.com/
官方快速上手教程:https://try.github.io/levels/1/challenges/1
github图文教程:http://rogerdudler.github.io/git-guide/
不想看英文的可以看一下下方的git一般操作步骤,看一遍后就会基本地使用git命令了。

git工作区、暂存区、仓库区

先了解一下基本概念,git的工作区域一般分为3个,分别是工作区(Working Area)、暂存区(Staging Area)、仓库区(Repository)。

也就是下图从左向右顺序。

《git最快速入门指南》

git一般操作步骤

1 创建目录

guoyanzongdeMacBook-Pro:TempFile oldpan$ mkdir git-test

2 进入目录

guoyanzongdeMacBook-Pro:TempFile oldpan$ cd git-test/
guoyanzongdeMacBook-Pro:git-test oldpan$ ls

3 使当前目录变为git仓库

guoyanzongdeMacBook-Pro:git-test oldpan$ git init
Initialized empty Git repository in /Users/oldpan/Documents/TempFile/git-test/.git/

4 查看当前有没有提交记录,当然显示没有任何记录

guoyanzongdeMacBook-Pro:git-test oldpan$ git log
fatal: your current branch 'master' does not have any commits yet

5 查看当前git状态

guoyanzongdeMacBook-Pro:git-test oldpan$ git status
On branch master

No commits yet

nothing to commit (create/copy files and use "git add" to track)

6 查看此时目录下有什么文件,发现多了一个.git文件

guoyanzongdeMacBook-Pro:git-test oldpan$ ls -a
.	..	.git

7 在git-test目录下创建一个txt文件

guoyanzongdeMacBook-Pro:git-test oldpan$ vim myfile.txt
# myfile的内容
hello
~
~
~
~
~

8 再次使用git log命令查看

guoyanzongdeMacBook-Pro:git-test oldpan$ git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)

	myfile.txt

nothing added to commit but untracked files present (use "git add" to track)

9 但此时myfile文件并没有被git加入到暂存区,所以使用add命令

guoyanzongdeMacBook-Pro:git-test oldpan$ git add myfile.txt
guoyanzongdeMacBook-Pro:git-test oldpan$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

	new file:   myfile.txt

10 现在再创建一个文件yourfile.txt,内容为pig

guoyanzongdeMacBook-Pro:git-test oldpan$ vim yourfile.txt
...
pig
~
~
~

11 再次使用git status观察此时文件的情况

guoyanzongdeMacBook-Pro:git-test oldpan$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

	new file:   myfile.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)

	yourfile.txt

12 进行git commit,此时将之前通过add命令把暂存区的myfile.txt进行commit,并填写commit信息

guoyanzongdeMacBook-Pro:git-test oldpan$ git commit
...
commit myfilt.txt # 在这一行填写commit信息,填写完保存即可
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# On branch master
#
# Initial commit
#
# Changes to be committed:
#       new file:   myfile.txt
#
# Untracked files:
#       yourfile.txt
#
...
[master (root-commit) ab5b577] commit myfilt.txt
 1 file changed, 1 insertion(+)
 create mode 100644 myfile.txt

13 将另一个文件yourfile.txt也进行add并commit

guoyanzongdeMacBook-Pro:git-test oldpan$ git add yourfile.txt
guoyanzongdeMacBook-Pro:git-test oldpan$ git commit
# 填写commit信息,略
[master 88ca0a5] commit yourfile.txt
 1 file changed, 1 insertion(+)
 create mode 100644 yourfile.txt
guoyanzongdeMacBook-Pro:git-test oldpan$ git status # 此时再看已经没有需要commit的文件了
On branch master
nothing to commit, working tree clean

14 查看当前分支,发现我们只有一个分支,那就是master

guoyanzongdeMacBook-Pro:git-test oldpan$ git branch
* master

15 创建一个新的分支,名称叫做’new-branch’

guoyanzongdeMacBook-Pro:git-test oldpan$ git branch new-branch
guoyanzongdeMacBook-Pro:git-test oldpan$ git branch
* master
  new-branch

16 利用checkout命令切换当’new-branch’这个分支

guoyanzongdeMacBook-Pro:git-test oldpan$ git branch new-branch
guoyanzongdeMacBook-Pro:git-test oldpan$ git branch
* master
  new-branch
guoyanzongdeMacBook-Pro:git-test oldpan$ git checkout new-branch
Switched to branch 'new-branch'
guoyanzongdeMacBook-Pro:git-test oldpan$ git branch
  master
* new-branch

17 此时,我在’new-branch’这个新的分支上修改myfile文件并提交它,通过这个命令git log --graph --oneline master new-branch来查看两个分支的commit详情(master 和 new-branch是两个分支的名称)。

guoyanzongdeMacBook-Pro:git-test oldpan$ git checkout new-branch
M	myfile.txt
Switched to branch 'new-branch'
guoyanzongdeMacBook-Pro:git-test oldpan$ vim myfile.txt
guoyanzongdeMacBook-Pro:git-test oldpan$ git add myfile.txt
guoyanzongdeMacBook-Pro:git-test oldpan$ git commit myfile.txt
[new-branch d608dab] add another new line
 1 file changed, 2 insertions(+)
guoyanzongdeMacBook-Pro:git-test oldpan$ git log
commit d608dab1c9b0e91ebc8a2fc2dcde43f0c0f575ea (HEAD -> new-branch)
Author: oldpan <295484914@qq.com>
Date:   Sat May 12 15:43:07 2018 +0800

    add another new line

commit 88ca0a56781142d60e095784f29a58caac2375e2 (master)
Author: oldpan <295484914@qq.com>
Date:   Wed May 9 18:57:00 2018 +0800

    commit yourfile.txt

commit ab5b5771550dbf67cb4a649b1b120b4d18bc9863
Author: oldpan <295484914@qq.com>
Date:   Wed May 9 18:52:52 2018 +0800

    commit myfilt.txt
guoyanzongdeMacBook-Pro:git-test oldpan$ git log --graph --oneline master new-branch
* d608dab (HEAD -> new-branch) add another new line
* 88ca0a5 (master) commit yourfile.txt
* ab5b577 commit myfilt.txt

相关命令

git log:查看commit记录。
git diff: 对比两个文件,显示两个文件中内容的不同地方(增加或者减少一些文字),使用不同参数的diff命令比较的对象不同。

git diff   # 比较工作区和暂存区的文件区别 
-------在工作区中修改文件---------------------
guoyanzongdeMacBook-Pro:git-test oldpan$ vim myfile.txt
guoyanzongdeMacBook-Pro:git-test oldpan$ git diff
diff --git a/myfile.txt b/myfile.txt
index ce01362..fcb9da8 100644
--- a/myfile.txt
+++ b/myfile.txt
@@ -1 +1,2 @@
 hello
+add a new line
------------------------------------------
git diff --staged  # 比较暂存区和commit区的文件
-------------将上面的myfile.txt文件进行add---
guoyanzongdeMacBook-Pro:git-test oldpan$ git diff --staged
guoyanzongdeMacBook-Pro:git-test oldpan$ ls
myfile.txt	yourfile.txt
guoyanzongdeMacBook-Pro:git-test oldpan$ git add myfile.txt
guoyanzongdeMacBook-Pro:git-test oldpan$ git diff --staged
diff --git a/myfile.txt b/myfile.txt
index ce01362..fcb9da8 100644
--- a/myfile.txt
+++ b/myfile.txt
@@ -1 +1,2 @@
 hello
+add a new line
-------------------------------------------
git diff commit1 commit2  # 比较两个commit文件的内容 commit1和commit2分别是两个commit的编号

git中需要注意的地方

  • 在合并文件的决策中有几点是需要注意的,假如有两个人a和b,他们正在分别独立地编辑同一个文件的两个副本,a和b每天在都编辑这个文件,有时候在文件中增加一些行,有时候删除一些行,总之a和b分别对这个文件的副本进行编辑。然而某一天他们两个人编辑好的文件要进行合并。
  • 在产品实际操作中,一般只会存在一个master分支。这个master分支是稳定的、面向用户的分支,在实际编写中不会对其有特殊的修改。但是你如果有一个新想法或者想要实现新功能,那么可以使用命令’git branch’开发一个新的分支,这个分支和master分支是并行的,互不影响。
  点赞
本篇文章采用 署名-非商业性使用-禁止演绎 4.0 国际 进行许可
转载请务必注明来源: https://oldpan.me/archives/git-super-quick-guide

   关注Oldpan博客微信公众号,你最需要的及时推送给你。


 猜你喜欢

  1. b
    bt110说道:

    谢谢博主热心分享,留言支持一下,也欢迎回访一下我的网站

    1. O
      Oldpan说道:

      谢谢!