安装git
[root@localhost ~]# yum install -y git[root@localhost ~]# mkdir -p /data/gitroot[root@localhost ~]# cd /data/gitroot/[root@localhost gitroot]# git init #初始化仓库Initialized empty Git repository in /data/gitroot/.git/ #初始化空的Git版本库/data/gitroot/.git/ [root@localhost gitroot]# ls -latotal 4drwxr-xr-x. 3 root root 17 Aug 3 12:36 .drwxr-xr-x. 3 root root 20 Aug 3 12:34 ..drwxr-xr-x. 7 root root 4096 Aug 3 12:36 .git[root@localhost gitroot]# echo "test" > 1.txt[root@localhost gitroot]# git add 1.txt #add结束后必须要commit才算真正把文件提交到git仓库里[master (root-commit) 9ee5ccd] add 1.txt Committer: rootYour name and email address were configured automatically basedon your username and hostname. Please check that they are accurate.You can suppress this message by setting them explicitly: git config --global user.name "Your Name" git config --global user.email you@example.comAfter doing this, you may fix the identity used for this commit with: git commit --amend --reset-author 1 file changed, 1 insertion(+) create mode 100644 1.txt
再次修改
[root@localhost gitroot]# vi 1.txt添加test[root@localhost gitroot]# git add 1.txt [root@localhost gitroot]# git commit -m "add 1.txt agin"[master 23509ff] add 1.txt agin Committer: rootYour name and email address were configured automatically basedon your username and hostname. Please check that they are accurate.You can suppress this message by setting them explicitly: git config --global user.name "Your Name" git config --global user.email you@example.comAfter doing this, you may fix the identity used for this commit with: git commit --amend --reset-author 1 file changed, 1 insertion(+)[root@localhost gitroot]# git status# On branch master #位于分支masternothing to commit, working directory clean #无文件要提交,干净的工作区
再次修改,不添加
[root@localhost gitroot]# vi 1.txt添加test[root@localhost gitroot]# git status# On branch master# Changes not staged for commit: #尚未暂存以备提交的变更:# (use "git add..." to update what will be committed) #使用*更新要提交的内容# (use "git checkout -- ..." to discard changes in working directory) #*丢弃工作区的改动## modified: 1.txt #修改:1.txt#no changes added to commit (use "git add" and/or "git commit -a") #修改尚未加入提交
使用diff查看变更情况
[root@localhost gitroot]# git diff 1.txt diff --git a/1.txt b/1.txtindex dec2cbe..0867e73 100644--- a/1.txt+++ b/1.txt@@ -1,2 +1,3 @@ test test+test
回退版本
[root@localhost gitroot]# git add 1.txt [root@localhost gitroot]# git commit -m "add 1.txt agin"[master 561671d] add 1.txt agin Committer: rootYour name and email address were configured automatically basedon your username and hostname. Please check that they are accurate.You can suppress this message by setting them explicitly: git config --global user.name "Your Name" git config --global user.email you@example.comAfter doing this, you may fix the identity used for this commit with: git commit --amend --reset-author 1 file changed, 1 insertion(+)[root@localhost gitroot]# vi 1.txt 删除第二行test[root@localhost gitroot]# git add 1.txt ; git commit -m "ch 1.txt agin"[master c246d69] ch 1.txt agin Committer: root Your name and email address were configured automatically basedon your username and hostname. Please check that they are accurate.You can suppress this message by setting them explicitly: git config --global user.name "Your Name" git config --global user.email you@example.comAfter doing this, you may fix the identity used for this commit with: git commit --amend --reset-author 1 file changed, 1 deletion(-)
查看变更记录
[root@localhost gitroot]# git logcommit c246d692affa5da9098d55cca8e8ef30f83b7594Author: rootDate: Fri Aug 3 12:49:42 2018 +0800 ch 1.txt agincommit 561671dc73e8a45d62d7b9d0910cde2386c6db15Author: root Date: Fri Aug 3 12:47:51 2018 +0800 add 1.txt agincommit 23509ff7c57c3ce0afc869ca9fb512fef8685227Author: root Date: Fri Aug 3 12:42:03 2018 +0800 add 1.txt agincommit 9ee5ccdfd609b0f26bca77bd8d4161a6d6e48c73Author: root Date: Fri Aug 3 12:38:49 2018 +0800 add 1.txt(END)
查看变更记录格式的文件
[root@localhost gitroot]# ls .git/branches COMMIT_EDITMSG config description HEAD hooks index info logs objects refs[root@localhost gitroot]# cat .git/config [core] repositoryformatversion = 0 filemode = true bare = false logallrefupdates = truecat /root/.gitconfig没有找到
简明显示
[root@localhost gitroot]# git log --pretty=onelinec246d692affa5da9098d55cca8e8ef30f83b7594 ch 1.txt agin561671dc73e8a45d62d7b9d0910cde2386c6db15 add 1.txt agin23509ff7c57c3ce0afc869ca9fb512fef8685227 add 1.txt agin9ee5ccdfd609b0f26bca77bd8d4161a6d6e48c73 add 1.txt[root@localhost gitroot]# git reset --hard 561671dc73e8a45d62d7b9d0910cde2386c6db15HEAD is now at 561671d add 1.txt agin #HEAD现在位于561671d add 1.txt agin> 这里可以只取前面一部分,如:git reset --hard 561671d> 执行命令必须要做该目录下进行操作,否会出现fatal: This operation must be run in a work tree[root@localhost gitroot]# cat 1.txt testtesttest[root@localhost gitroot]# git log --pretty=oneline561671dc73e8a45d62d7b9d0910cde2386c6db15 add 1.txt agin23509ff7c57c3ce0afc869ca9fb512fef8685227 add 1.txt agin9ee5ccdfd609b0f26bca77bd8d4161a6d6e48c73 add 1.txt[root@localhost gitroot]# git reset --hard 23509ff7c5HEAD is now at 23509ff add 1.txt agin[root@localhost gitroot]# git log --pretty=oneline23509ff7c57c3ce0afc869ca9fb512fef8685227 add 1.txt agin9ee5ccdfd609b0f26bca77bd8d4161a6d6e48c73 add 1.txt若想恢复到最原始的一条记录,需要记住ID若没有记住,可以使用所有记录git reflog[root@localhost gitroot]# git reflog23509ff HEAD@{0}: reset: moving to 23509ff7c5561671d HEAD@{1}: reset: moving to 561671dc73e8a45d62d7b9d0910cde2386c6db15c246d69 HEAD@{2}: commit: ch 1.txt agin561671d HEAD@{3}: commit: add 1.txt agin23509ff HEAD@{4}: commit: add 1.txt agin9ee5ccd HEAD@{5}: commit (initial): add 1.txt[root@localhost gitroot]# git reset --hard c246d69HEAD is now at c246d69 ch 1.txt agin[root@localhost gitroot]# cat 1.txt testtest[root@localhost gitroot]# git log --pretty=onelinec246d692affa5da9098d55cca8e8ef30f83b7594 ch 1.txt agin561671dc73e8a45d62d7b9d0910cde2386c6db15 add 1.txt agin23509ff7c57c3ce0afc869ca9fb512fef8685227 add 1.txt agin9ee5ccdfd609b0f26bca77bd8d4161a6d6e48c73 add 1.txt
误删除
[root@localhost gitroot]# rm -f 1.txt [root@localhost gitroot]# ls[root@localhost gitroot]# git checkoutD 1.txt[root@localhost gitroot]# git checkout -- 1.txt[root@localhost gitroot]# ls1.txt> 文件还存在版本库里add但没有commit[root@localhost gitroot]# vi 1.txt添加111[root@localhost gitroot]# git add 1.txt
恢复上一次状态
[root@localhost gitroot]# git reset HEAD 1.txt Unstaged changes after reset: #重置后撤出暂存区的变更:M 1.txt[root@localhost gitroot]# git checkout -- 1.txt[root@localhost gitroot]# cat 1.txt testtest
删除git
[root@localhost gitroot]# git rm 1.txt rm '1.txt'[root@localhost gitroot]# ls> 但没有在库里删除[root@localhost gitroot]# git commit -m "delete 1.txt"[master f569f97] delete 1.txt Committer: rootYour name and email address were configured automatically basedon your username and hostname. Please check that they are accurate.You can suppress this message by setting them explicitly: git config --global user.name "Your Name" git config --global user.email you@example.comAfter doing this, you may fix the identity used for this commit with: git commit --amend --reset-author 1 file changed, 2 deletions(-) delete mode 100644 1.txtroot@localhost gitroot]# git log --pretty=onelinef569f973b61ffac815cead93cab8a046e1fa47fd delete 1.txtc246d692affa5da9098d55cca8e8ef30f83b7594 ch 1.txt agin561671dc73e8a45d62d7b9d0910cde2386c6db15 add 1.txt agin23509ff7c57c3ce0afc869ca9fb512fef8685227 add 1.txt agin9ee5ccdfd609b0f26bca77bd8d4161a6d6e48c73 add 1.txt
测试恢复
[root@localhost gitroot]# git reset --hard f569f973HEAD is now at f569f97 delete 1.txt[root@localhost gitroot]# ls[root@localhost gitroot]# git checkout -- 1.txterror: pathspec '1.txt' did not match any file(s) known to git.[root@localhost gitroot]# git checkout> 失败
恢复
[root@localhost gitroot]# git log --pretty=onelinef569f973b61ffac815cead93cab8a046e1fa47fd delete 1.txtc246d692affa5da9098d55cca8e8ef30f83b7594 ch 1.txt agin561671dc73e8a45d62d7b9d0910cde2386c6db15 add 1.txt agin23509ff7c57c3ce0afc869ca9fb512fef8685227 add 1.txt agin9ee5ccdfd609b0f26bca77bd8d4161a6d6e48c73 add 1.txt[root@localhost gitroot]# git reset --hard c246d692aHEAD is now at c246d69 ch 1.txt agin[root@localhost gitroot]# ls1.txt[root@localhost gitroot]# cat 1.txt testtest> 上述恢复的ID为delete,所以才会报错