一个很适合入门的教程。即使你不知道什么叫版本控制,看完也应该能会使用Git了。笔记以后再做吧。
一下内容比较混乱,强烈建议直接阅读上面的网址链接中的 Git相关部分。
windows 8 的 PowerShell 还是挺不错的。中文支持也挺好~
> mkdir g_test # 创建 目录 g_test
> cd g_test # 切换到 目录 g_test
> git init
几个常用命令:
> git init # 初始化一个git仓库
> vim a.py # 创建一个文件
>
> git add a.py # 将a.py 文件,添加到暂存区。
>
> git commit -m 'init' # 将暂存区的所有文件添加到当前分支,
> git status # 查看当前仓库状态,是否有文件改动没有提交
> git status
On branch master
nothing to commit, working directory clean
> vim a.py
> git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: a.py
Untracked files:
(use "git add <file>..." to include in what will be committed)
a.py~
no changes added to commit (use "git add" and/or "git commit -a")
# 想要查看 a.py 到底修改了哪些内容,使用如下命令
> git diff a.py
WARNING: terminal is not fully functional
diff --git a/a.py b/a.py
index 49033be..016ad7e 100644
--- a/a.py
+++ b/a.py
@@ -2,3 +2,4 @@ import socket
if __name__ == '__main__':
print socket.gethostname()
+ print 'hi'
# 查看日志
> git log
# 所有执行命令日志
> git reflog # 返回记录执行的每一条命令
工作区与暂存区、
工作区(Working Directory):就是能看到的文件夹。或者说是在使用 git init 的文件夹,除了里面隐藏的 .git 文件夹。
版本库(Repository):就是一个隐藏目录 .git。
在 .git 版本库中存在很多信息。
stage 或者叫做 index 的暂存区。
还有git默认创建的一个分支 master。以及指向这个分支master的指针 HEAD。
在使用 git add xx 时,就是将文件添加到stage暂存区。
使用 git commit xx 时,将暂存区的所有内容提交到当前分支。
(因为在git init 时,GIT自动创建了 master 分支)
管理修改:
git 管理的是修改,而不是文件。可以通过一个小实验进行验证。
对一个已经提交的文件进行修改--》执行git add --》再次对该文件进行修改 --》 再执行 git commit。
执行 git diff HEAD -- xx.x 会发现两个文件时不同的,并且能看到文件与第一次修改后的文件时相同的。因此可以得知,git commit 提交的只是在add之前对文件进行的修改。
对文件的修改,如果不add到缓存区,是不会到加入到commit中的。
> git add a.py # 将a.py 或 对a.py的修改 提交到缓存区
> git commit # 将修改提交到当前分支
> git diff HEAD -- a.py # 查看分支中文件 与 当前工作区中文件 的区别
撤销修改:
在修改完工作区文件时,使用 git status 可以看到如下信息。
> git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: a.py
Untracked files:
(use "git add <file>..." to include in what will be committed)
a.py~
no changes added to commit (use "git add" and/or "git commit -a")
# 在 master 分支中
# 一些修改没有为提交到分支中而放到缓冲区中的东西
# (可以看到提示:使用 git add <file> 去更新将要被提交到分支中的文件)
# (使用 git checkout -- <file> 丢弃在工作区中的文件修改)
# 被修改的: a.py
# 没有被跟踪的文件
# (使用 git add <file> 加入到将会被提交到分支中去)
# a.py
# ... 深深的被自己的翻译水平折服,简直不能直视 ~~~~~
删除文件:
工作区删除文件之后,使用 git status
> git status
On branch master
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: a.py
deleted: js.py
Untracked files:
(use "git add <file>..." to include in what will be committed)
a.py~
no changes added to commit (use "git add" and/or "git commit -a")
# 使用 git rm <file> 删除文件
# 使用 git checkout -- <file> 覆盖工作区文件
# 使用温 git rm <file> 只是将暂存区中的操作,还需要将修改提交到当前分支中。
# git commit -m 'del <file>'