Git入门

官网地址:https://www.git-scm.com/

Git入门

将下来下来的安装包,一直下一步默认安装,直到完成。

GIT初始化


  1. git config --global user.name "郭靖"
  2. git config --global user.email "[email protected]"

  1. [email protected]-PC /C/Users/guojinlong/Desktop (master)
  2. $ git config --list
  3. core.symlinks=false
  4. core.autocrlf=true
  5. color.diff=auto
  6. color.status=auto
  7. color.branch=auto
  8. color.interactive=true
  9. pack.packsizelimit=2g
  10. help.format=html
  11. http.sslcainfo=/bin/curl-ca-bundle.crt
  12. sendemail.smtpserver=/bin/msmtp.exe
  13. diff.astextplain.textconv=astextplain
  14. rebase.autosquash=true
  15. user.name=郭金龙
  16. user.email=[email protected].cn
  17. gui.recentrepo=D:/workspace/idea/tdm
  18. core.repositoryformatversion=0
  19. core.filemode=false
  20. core.bare=false
  21. core.logallrefupdates=true
  22. core.symlinks=false
  23. core.ignorecase=true
  24. core.hidedotfiles=dotGitOnly
  25. gui.wmstate=normal
  26. gui.geometry=799x475+130+130 159 214

GIT理论

  1. GIT将每个版本独立保存
  2. GIT分为三棵树,每别为工作区域、暂存区域和GIT仓库
    Git入门
    Git的工作流程一般是:
  3. 在工作目录中添加、修改文件
  4. 将需要进行版本管理的文件放入暂存区域
  5. 将暂存区域的文件提交到GIT仓库

Git管理的文件有三种状态:

  • 已修改(modified)
  • 已暂存(staged)
  • 已提交(committed)

GIT实操

1、在任意盘符创建一个工作目录 如:D:\gitlearn
2、执行git init 初始化一个GIT仓库


  1. [email protected]-PC /d/gitlearn
  2. $ pwd
  3. /d/gitlearn
  4. [email protected]-PC /d/gitlearn
  5. $ git init
  6. Initialized empty Git repository in d:/gitlearn/.git/

会在gitlearn目录下生成一个.git的隐藏文件夹,用来记录版本信息,不要修改。Git入门3、在gitlearn创建一个readme.md文件,然后编辑这个文件写入一点内容。此时这个readme.md文件相当于在工作区域的文件夹


  1. [email protected]-PC /d/gitlearn (master)
  2. $ touch README.md
  3. [email protected]-PC /d/gitlearn (master)
  4. $ ls
  5. README.md
  6. [email protected]-PC /d/gitlearn (master)
  7. $ vim README.md
  8. [email protected]-PC /d/gitlearn (master)

4、执行git add README.md,将README.md添加到暂存区域,控制台不会打印任何消息。


  1. [email protected]-PC /d/gitlearn (master)
  2. $ git add README.md
  3. [email protected]-PC /d/gitlearn (master)

5、执行git commit -m “add a readme file “ README.md,将README.md添加到GIT本地仓库区域 -m选项:添加本次提交的说明。


  1. [email protected]-PC /d/gitlearn (master)
  2. $ git commit -m "add a readme file " README.md
  3. [master (root-commit) d2143a1] add a readme file
  4. 1 file changed, 1 insertion(+)
  5. create mode 100644 README.md

提示成功添加了一个文件,100644:100表示是一个普通文件,644表示权限

6、查看git仓库的状态。git status


  1. [email protected]-PC /d/gitlearn (master)
  2. $ git status
  3. On branch master
  4. nothing to commit, working directory clean

提示当前master分支目录是干净的,没有需要提交的文件。

7、新增加一个版权文件


  1. [email protected]-PC /d/gitlearn (master)
  2. $ touch LICENSE
  3. [email protected]-PC /d/gitlearn (master)
  4. $ vim LICENSE
  5. [email protected]-PC /d/gitlearn (master)

8、再次查看仓库状态
Git入门
提示在master分支未跟踪文件LICENS,指新添加的文件没有添加到暂存区或提交到GIT仓库。括号内的内容是GIT提供的建议。

9、按照提示将LICENSE添加到暂存区,再次查看状态
Git入门
提示可以使用git reset HEAD<file> to unstage
意思是将最后一次提交到暂存区域的文件恢复为先前的状态。可以指定文件名。如果不指定是所有内容。
10、恢复暂存区,再次查看状态
Git入门
可以看到LINCESE文件又恢复为未跟踪状态了。

11、将LICENSE文件添加到暂存区并提交到GIT仓库后,再次修改LICENSE文件。再观察git仓库的状态


  1. $ git add LICENSE
  2. $ git commit -m "add a License File"
  3. [master 776aaa8] add a License File
  4. 1 file changed, 1 insertion(+)
  5. create mode 100644 LICENSE
  6. vim LICENSE

Git入门由于对工作目录的文件进行了修改,导至文件和暂存区域对应的文件不匹配。GIT给出了两条建议。第一条:使用add命令将目录中的新版覆盖暂存区域。第二条:用checkout 用暂存区域的旧版本覆盖工作目录中的新版本。该命令是GIT中少数有威胁的命令。

12、先查看当前LICENSE文件的内容。再执行第二条建议,进行比对。Git入门


  1. $ git checkout -- LICENSE

Git入门