首先我将MSYS中的几个小工具COPY进了GIT\BIN目录,如head,tail,sha1sum等
这个批处理就是模拟了书中的例子,省去了观察状态的命令,如git status,git log 等
@echo off
rem 初始设置
rem git config --global user.email
[email protected]
rem git config --global user.name ddatsh
rm -r -f z:\dd 2>nul
md z:\dd 2>nul
cd /d z:\dd
rem 三部曲:init,add,commit
cmd /c git init
echo hello>welcome.txt
cmd /c git add welcome.txt
cmd /c git commit -m init
rem 空提交
rem git config --unset --global user.email
rem git config --unset --global user.name
cmd /c git commit -m "who does commit?" --allow-empty
rem git log 看见类似 Author: unknown <Administrator@(none)>
rem git config --global user.email
[email protected]
rem git config --global user.name ddatsh
rem 重置作者
cmd /c git commit --amend --reset-author --allow-empty -m "who does commit"
echo nice to meet you>> welcome.txt
rem 未git add前,commit时不会被提交
rem 现在git status和commit 命令后git status状态一样,在commit 命令执行时就提示了状态
rem git commit -m "append 1 line"
cmd /c git add welcome.txt
echo bye bye>> welcome.txt
rem 此时welcome.txt就有三个状态:工作区,暂存区,库
rem git status ,提示有 Changes to be committed 和 Changes not staged for commit两个
rem git diff,diff head,diff --cached查看,三种结果
rem goto end
cmd /c git commit -m "which version checked in?"
rem git diff 和diff head一样,bye bye
rem git diff --cached 为空
mkdir a\b\c
echo hello> a\b\c\hello.txt
cmd /c git add a\b\c\hello.txt welcome.txt
echo bye bye>> a\b\c\hello.txt
rem welcome.txt和hello.txt的区别是前者在版本库,存在三种状态,后者没在版本库(没commit过),两种状态
rem goto end
rem cmd /c git status
cmd /c git stash -q
rem git stash pop和git stash pop --index 后 git status 的区别
rem 第6章 sha1生成
rem git对象有commit,tree,blob
rem git cat-file commit head|master|refs/heads/master都一样
rem git cat-file commit head |wc -c 显示字符数
rem (printf "commit 大小\000";git cat-file commit head)|sha1sum
rem git rev-parse head 两者一样
rem git cat-file blob head:welcome.txt与git cat-file blob :welcome.txt 分别查看库与暂存区内容
rem git cat-file tree "head^{head}"
rem 第7章 git reset
touch new-commit.txt >nul
cmd /c git add new-commit.txt
rem 前后两次查看,值不同,被更新了
rem cat .git/refs/heads/master
cmd /c git commit -m "does master follow this new commit?"
rem cat .git/refs/heads/master
rem 回到上一次
cmd /c git reset --hard head~
rem 回到第一次commit
cmd /c git log --pretty=format:%%h |tail -1 |xargs git reset --hard
rem 进行恢复
rem master@{2}是通过git reflog 看的
cmd /c git reset --hard master@{2}
rem 第8章
rem checkout操作
rem 分离头指针
cmd /c git checkout head~
touch detached-commit.txt >nul
cmd /c git add detached-commit.txt
cmd /c git commit -m "commit in detached head mode"
rem 得到此时的commit id,存变量
for /f "usebackq " %%i in (` "git log --pretty=format:%%h |head -1"`) do @set var=%%i
cmd /c git checkout master
rem 分离的修改合并入master
cmd /c echo %var% |xargs git merge
rem git log --oneline --graph
rem git cat-file -p head 有两个parent,这就是merge的奥秘
rem 第9章 stash
rem stash pop和stash pop --index 的区别,用git diff,diff head,diff --cached查看,和stash apply区别?
cmd /c git stash pop -q
rem goto end
cmd /c git commit -m "add new file a/b/c/hello.txt,but leave welcome.txt alone"
rem 撤销上次提交,但暂存,工作区都不变
cmd /c git reset --soft head~
rem 清理工作区
cmd /c git co head .
cmd /c git reset
cmd /c git clean -fd
cmd /c echo bye bye>>welcome.txt
cmd /c echo hello>hack-1.txt
cmd /c git add hack-1.txt
cmd /c git stash save "hack-1: hacked welcome.txt,newfile hack-1.txt"
cmd /c echo fix>hack-2.txt
cmd /c git stash -u
cmd /c git stash apply stash@{1} -q
cmd /c git stash clear
rem 第10章 基本操作
rem tag
cmd /c git tag -m "say bye-bye to all previous practice" old_practice
rem git describe
goto end
rem delete操作
cmd /c git stash
cmd /c rm *.txt
rem git ls-files 文件都在,说明直接在工作区删除,对版本库和暂存区无影响
rem git status
cmd /c git checkout .
cmd /c git rm detached-commit.txt hack-1.txt new-commit.txt welcome.txt
cmd /c git commit -m "delete trash files (using git rm)"
cmd /c git reset --hard head~
cmd /c git stash apply -q
rm *.txt
rem git add -u 更智能,省去在rm 命令后,再要git rm重复一次
cmd /c git add -u
cmd /c git commit -m "delete trash files (using git add -u)"
cmd /c git checkout head~1 welcome.txt
cmd /c git commit -m "restore file: welcome.txt"
rem 改名,两种方法 git mv 和直接mv命令,然后git add -A
cmd /c git mv welcome.txt readme
cmd /c git commit -m "rename welcome.txt to readme"
cmd /c git reset --hard head~
mv welcome.txt readme
rem git status后 delete welcome.txt,untracked readme
echo bye bye>> readme
cmd /c git add -A
rem git add -A后,智能的变成了rename
cmd /c git commit -m "readme is from welcome.txt"
mkdir src
touch src\main.c >nul
touch src\makefile >nul
touch src\version.h.in >nul
cmd /c git add .
cmd /c git commit -m "Hello world initialized."
cmd /c git tag -m "Set tag hello_1.0." hello_1.0
echo *.o>src\.gitignore
cmd /c git add src\.gitignore
cmd /c git commit -m "ignore object files"
cmd /c git mv src\.gitignore src\..
cmd /c git commit -m "move .gitignore outside also works."
echo /* test */>src\hello.h
cmd /c git add src\hello.h
cmd /c git commit -m "add hello.h"
echo /* end */>>src\hello.h
cmd /c git commit -a -m "using commit -a"
rm src\hello.h
echo wait>> readme
cmd /c git add -u
cmd /c git commit -m qgit
cmd /c git checkout head~ -- src\hello.h
cmd /c git commit --amend -m "commit with --amend test"
cmd /c git reset --soft head~~
cmd /c git commit -m "modify hello.h"
cmd /c git tag F
cmd /c git tag E head~
cmd /c git tag D head~~
cmd /c git tag C head~~~
cmd /c git tag B head~~~~
cmd /c git tag A head~~~~~
cmd /c git checkout C
cmd /c git cherry-pick master~
cmd /c git cherry-pick master
cmd /c git checkout master
cmd /c git reset --hard head@{1}
:end