Git

 

git

git是一个强大的版本记录命令,笔者发现每次记忆git命令后都会在一段时间后遗忘,记录如下,由于笔者并没有使用git进行大规模多人合作,仅记录了单仓库单分支的简单命令.

常用命令:

初始化及添加和提交

    git config --global user.name "a-little-cat" 
    git config --global user.email 13211126@buaa.edu.cn  这两条命令设置的信息会显示在提交记录中,如果不加global只会对当前git仓库生效
    git init  在项目目录下执行,初始化
    git add *.c  添加所有以.c为后缀的文件到暂存区
    git rm file  从仓库中移除文件,同时从磁盘移除文件
    git rm --cached file 从仓库中移除文件,但不从磁盘移除
    git mv file_from file_to 重命名文件
    git commit -m "初始化了一个仓库" 保存暂存区的文件到git仓库
    git commit -a -m "update" 自动添加所有已追踪的文件到暂存区 并提交
    git push/pull 推送/拉取
    git rm file.c 从仓库中移除文件
    git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch test.file' --prune-empty --tag-name-filter cat -- --all  在所有版本中删除某个文件
    git push --all --force 强行提交

进阶

git reset HEAD <file>  将暂存区的文件移出暂存区  但不影响工作区的修改
git checkout -- <file> 丢弃工作区的修改 恢复为上一次commit的状态
git tag -a v1.2 9fceb02  对之前某个哈希以9fceb02开头的历史版本打轻量tag 
git config --global alias.unstage 'reset HEAD --'  可以让下面两条命令等价
git reset HEAD -- <file>
git unstage <file>
git config --global --unset-all alias.unstage 移除某个config设置

git checkout -b iss53   新建并切换到新的分支 等价于
git branch iss53
git checkout iss53
git merge iss53  合并当前目录(如master)和iss53
git branch -d iss53  删除iss53

.gitignore例子

# 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原理

git的文件区域分为git仓库,已暂存区域,工作目录.

git仓库是git用来保存所有信息的地方,git会根据你的命令从中选择某个版本的快照显示在文件夹中,供用户操作.

工作目录是git从仓库中提出的某个版本的快照,以及用户在这个版本上的操作.

已暂存区域是git临时保存修改的地方,用户可以在工作目录修改一些文件,然后把这些修改(包括添加和删除)添加到已暂存区域,最后把已暂存区域的所有修改生成快照,保存到git仓库