Git Pro读书笔记(二)安装和基础操作 //Windows和Ubuntu

1.安装


Windows版本下载地址:http://git-scm.com/download/win


Ubuntu版本,必要时加个sudo

# apt-get install git

2.初始配置

用户名邮箱啥的,配置一次

git config --global user.name "Zhu Mingde"
git config --global user.email [email protected]

可以查看配置结果

git config --list


当然也可以查看指定的配置信息

PS C:\WINDOWS\system32> git config user.name
Zhu Mingde
PS C:\WINDOWS\system32> git config user.email
[email protected]


Ubuntu上效果


3.获取帮助文档

例如获取config帮助的指令可以键入

git help config
或者

git config --help

4.现有目录导入所有文件到Git

【Windows版】

转到目录下//注意,cd到目录中有空格的时候需要用引号引起来……

cd "C:\Users\zmdsj\Documents\Visual Studio 2015\Projects\opencvtest"
然后初始化

git init
接着添加文件add以及提交commit

git add *.cpp
git commit -m 'initial project version'
效果如图:(没有LICENSE...)

现在已经有了一个仓库。

【Ubuntu版】

操作一样


5.克隆代码仓

git clone  地址  (名字,可省略)

git clone https://github.com/libgit2/libgit2 Test_zmd

【Windows版】


【Ubuntu】



6.记录每次更新到仓库

查看状态信息

git status

增添一个新的追踪文件

git add README

查看状态的缩写

git status -s



忽略文件

创建一个ignore文件,忽略所有带~的文件以及所有以oa结尾的文件

//修改.gitignore文件里的内容就好

cat .gitignore
*.[oa]
*~
规范

所有空行或者以 # 开头的行都会被 Git 忽略。

可以使用标准的 glob 模式匹配。

匹配模式可以以(/)开头防止递归。

匹配模式可以以(/)结尾指定目录。

要忽略指定模式以外的文件或目录,可以在模式前加上惊叹号(!)取反。
举个栗子

# no .a files
*.a

# but do track lib.a, even though you're ignoring .a files above
!lib.a

# only ignore the TODO file in the current directory, not subdir/TODO
/TODO

# ignore all files in the build/ directory
build/

# ignore doc/notes.txt, but not doc/server/arch.txt
doc/*.txt

# ignore all .pdf files in the doc/ directory
doc/**/*.pdf



更为具体的见 项目


查看修改//已暂存和未暂存的

git diff指令查看哪些更新还没存起来……

git diff


查看已经存起来但是没提交的

git diff --cached

或者git diff --staged



提交更新

git commit

git commit


跳过使用暂存区域//避免繁琐的add

自动把所有已经跟踪过的文件暂存起来一并提交,从而跳过 git add 步骤
git commit -a



移除文件

git rm XXXX
如果删除之前修改过并放入缓存区的,需要加-f强制删除……


从Git里删除但本地保留……

加管检测--cached ....

git rm --cached README


git rm后面可以使用glob模式


移动文件、重命名

git mv file_from file_to

本质上相当于//mv操作,然后删除,再添加,合起来就是git mv

 mv README.md README
 git rm README.md
 git add README


7.查看历史


查看提交历史

git log


-p只显示差异部分  -2只显示两次

git log -p -2

更多详细信息 : 地址

8.撤销


撤销操作

重新上传

git commit --amend

取消暂存

git reset HEAD <file>... 
撤销对文件的修改

 (use "git checkout -- <file>..." to discard changes in working directory)
例子:
$ git checkout -- CONTRIBUTING.md
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    renamed:    README.md -> README


9.远程仓库

查看
git remote -v
添加

git remote add pb https://github.com/paulboone/ticgit


从远程仓库抓取(例如刚刚添加的pb

git fetch [remote-name]


推送到仓库

git push [remote-name] [branch-name]

查看远程仓库

git remote show [remote-name]


远程仓库的移除与重命名

git remote rename XX YY

10.打标签

列出标签
git tag

附注标签
-a 输入版本号  -m 名字

$ git tag -a v1.4 -m 'my version 1.4'
$ git tag
v0.1
v1.3
v1.4
显示版本

git show v1.4

轻量标签

$ git tag v1.4-lw
$ git tag
v0.1
v1.3
v1.4
v1.4-lw
v1.5

后期打标签//后面补上校验码

git tag -a v1.2 9fceb02


共享标签

//push到服务器,标签不会弄上去

git push origin v1.5

批量

 git push origin --tags

检出标签

git checkout -b [branchname] [tagname]


这部分看得有点浮光掠影,要用的时候还是要看,地址


11. Git别名

好方便,相当于快捷键,233

设置方式

git config --global alias.ci commit



先吃晚饭去了,233



猜你喜欢

转载自blog.csdn.net/zmdsjtu/article/details/79094612
今日推荐