git其实是一种多人开发项目时候的版本控制系统,是由LINUX之父Linus开发的,与SVN最大的区别在于可以支持离线操作。
首先安装:我用的网易的yum源http://mirrors.163.com/centos/6/os/x86_64/
然后yum install git -y
- Git初始化
[[email protected] ~]#git --version 查看git版本
[[email protected] ~]#git config --global user.name dangwanqiang 当前用户姓名和邮箱
[[email protected] ~]#git config --global color.ui true 在git输出中开启颜色显示
[[email protected] ~]#git config --list
user.name=dangwanqiang [email protected] color.ui=true
实际也是写入文件中去
[[email protected] ~]#cat ~/.gitconfig
[user] name = dangwanqiang email = [email protected] [color] ui = true
- 建立一个工作目录
[[email protected] ~]#git init github
[[email protected] ~]#cd github
[[email protected] github]#ls -A
.git 可以看到隐藏的目录.git(Git版本库,repository)
- 在工作目录下的建立操作
三部曲:1.init 2.add 3.commit
[[email protected] github]# touch READ.txt a.py //创建自己的工作文件
[[email protected] github]# git status
# On branch master # # Initial commit # # Untracked files: //未添加到版本库的文件 # (use "git add <file>..." to include in what will be committed) # #READ.txt #a.py nothing added to commit but untracked files present (use "git add" to track)
[[email protected] github]# git add a.py //将a.py加到版本库
[[email protected] github]# git status # On branch master # # Initial commit # # Changes to be committed: # (use "git rm --cached <file>..." to unstage) # #new file: a.py # # Untracked files: # (use "git add <file>..." to include in what will be committed) # #READ.txt
[[email protected] github]# git add READ.txt //将READ.txt加到版本库
[[email protected] github]# git status
# On branch master # # Initial commit # # Changes to be committed: # (use "git rm --cached <file>..." to unstage) # #new file: READ.txt #new file: a.py #
[[email protected] github]# git commit -m ‘init commit‘ //-m参数指定提交说明
[master (root-commit) 9f045e6] init commit 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 READ.txt create mode 100644 a.py
[[email protected] github]# git status
# On branch master nothing to commit, working directory clean
[[email protected] github]# vi a.py //对a.py再次进行修改
[[email protected] github]# git status -s
M a.py
注:工作区与暂缓区不同,即M标志位在第二位,也意味着需要执行add
[[email protected] github]# 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 # no changes added to commit (use "git add" and/or "git commit -a")
[[email protected] github]# git diff //查看工作区与暂缓区具体不同
diff --git a/a.py b/a.py index 72943a1..dbee026 100644 --- a/a.py +++ b/a.py @@ -1 +1,2 @@ aaa +bbb
[[email protected] github]# git add a.py
[[email protected] github]# git status -s
M a.py
注:暂缓区与最终版本库不同,即M标志位在第一位,也意味着需要执行commit
[[email protected] github]# git status
# On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # #modified: a.py #
[[email protected] github]# git diff --staged //查看暂缓区与最终版本库的具体不同
diff --git a/a.py b/a.py index 72943a1..dbee026 100644 --- a/a.py +++ b/a.py @@ -1 +1,2 @@ aaa +bbb
- 在工作目录下的撤销误操作
- 在工作目录下的删除重命名操作
[[email protected] github]# git rm a.py //删除缓存区和工作区的文件
rm ‘a.py‘
[[email protected] github]# git status
# On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # #deleted: a.py #
[[email protected] github]# git status -s
D a.py
[[email protected] github]# ls
READ.txt
[[email protected] github]# git commit -m ‘delete a.py‘ //将删除提交到最终版本库
[master 480eda1] delete a.py 1 file changed, 2 deletions(-) delete mode 100644 a.py
[[email protected] github]# git status
# On branch master nothing to commit, working directory clean
[[email protected] github]# git rm --cached READ.txt //只删除暂存区的文件
rm ‘READ.txt‘
[[email protected] github]# ls
READ.txt
[[email protected] github]# git reset READ.txt //从最终版本库恢复文件到暂存区
[[email protected] github]# ls
READ.txt
[[email protected] github]# git status
# On branch master
[[email protected] github]# git mv READ.txt READ.LL
[[email protected] github]# git commit -m ‘rename read.txt‘