Git入门
官网地址:https://www.git-scm.com/
将下来下来的安装包,一直下一步默认安装,直到完成。
GIT初始化
git config --global user.name "郭靖"
git config --global user.email "[email protected]"
[email protected]-PC /C/Users/guojinlong/Desktop (master)
$ git config --list
core.symlinks=false
core.autocrlf=true
color.diff=auto
color.status=auto
color.branch=auto
color.interactive=true
pack.packsizelimit=2g
help.format=html
http.sslcainfo=/bin/curl-ca-bundle.crt
sendemail.smtpserver=/bin/msmtp.exe
diff.astextplain.textconv=astextplain
rebase.autosquash=true
user.name=郭金龙
user.email=[email protected].cn
gui.recentrepo=D:/workspace/idea/tdm
core.repositoryformatversion=0
core.filemode=false
core.bare=false
core.logallrefupdates=true
core.symlinks=false
core.ignorecase=true
core.hidedotfiles=dotGitOnly
gui.wmstate=normal
gui.geometry=799x475+130+130 159 214
GIT理论
- GIT将每个版本独立保存
- GIT分为三棵树,每别为工作区域、暂存区域和GIT仓库
Git的工作流程一般是: - 在工作目录中添加、修改文件
- 将需要进行版本管理的文件放入暂存区域
- 将暂存区域的文件提交到GIT仓库
Git管理的文件有三种状态:
- 已修改(modified)
- 已暂存(staged)
- 已提交(committed)
GIT实操
1、在任意盘符创建一个工作目录 如:D:\gitlearn
2、执行git init 初始化一个GIT仓库
[email protected]-PC /d/gitlearn
$ pwd
/d/gitlearn
[email protected]-PC /d/gitlearn
$ git init
Initialized empty Git repository in d:/gitlearn/.git/
会在gitlearn目录下生成一个.git的隐藏文件夹,用来记录版本信息,不要修改。3、在gitlearn创建一个readme.md文件,然后编辑这个文件写入一点内容。此时这个readme.md文件相当于在工作区域的文件夹
[email protected]-PC /d/gitlearn (master)
$ touch README.md
[email protected]-PC /d/gitlearn (master)
$ ls
README.md
[email protected]-PC /d/gitlearn (master)
$ vim README.md
[email protected]-PC /d/gitlearn (master)
4、执行git add README.md,将README.md添加到暂存区域,控制台不会打印任何消息。
[email protected]-PC /d/gitlearn (master)
$ git add README.md
[email protected]-PC /d/gitlearn (master)
5、执行git commit -m “add a readme file “ README.md,将README.md添加到GIT本地仓库区域 -m选项:添加本次提交的说明。
[email protected]-PC /d/gitlearn (master)
$ git commit -m "add a readme file " README.md
[master (root-commit) d2143a1] add a readme file
1 file changed, 1 insertion(+)
create mode 100644 README.md
提示成功添加了一个文件,100644:100表示是一个普通文件,644表示权限
6、查看git仓库的状态。git status
[email protected]-PC /d/gitlearn (master)
$ git status
On branch master
nothing to commit, working directory clean
提示当前master分支目录是干净的,没有需要提交的文件。
7、新增加一个版权文件
[email protected]-PC /d/gitlearn (master)
$ touch LICENSE
[email protected]-PC /d/gitlearn (master)
$ vim LICENSE
[email protected]-PC /d/gitlearn (master)
8、再次查看仓库状态
提示在master分支未跟踪文件LICENS,指新添加的文件没有添加到暂存区或提交到GIT仓库。括号内的内容是GIT提供的建议。
9、按照提示将LICENSE添加到暂存区,再次查看状态
提示可以使用git reset HEAD<file> to unstage
意思是将最后一次提交到暂存区域的文件恢复为先前的状态。可以指定文件名。如果不指定是所有内容。
10、恢复暂存区,再次查看状态
可以看到LINCESE文件又恢复为未跟踪状态了。
11、将LICENSE文件添加到暂存区并提交到GIT仓库后,再次修改LICENSE文件。再观察git仓库的状态
$ git add LICENSE
$ git commit -m "add a License File"
[master 776aaa8] add a License File
1 file changed, 1 insertion(+)
create mode 100644 LICENSE
vim LICENSE
由于对工作目录的文件进行了修改,导至文件和暂存区域对应的文件不匹配。GIT给出了两条建议。第一条:使用add命令将目录中的新版覆盖暂存区域。第二条:用checkout 用暂存区域的旧版本覆盖工作目录中的新版本。该命令是GIT中少数有威胁的命令。
12、先查看当前LICENSE文件的内容。再执行第二条建议,进行比对。
$ git checkout -- LICENSE