Git基本操作

一、git init初始化

$ mkdir demo
$ cd demo/
注:使用git命令必须在初始化目录下

初始化一个Git仓库
[root@foundation61 demo]$ git init
Initialized empty Git repository in /home/kiosk/demo/.git/
查看多了一个隐藏目录/.git,是Git的版本库。没事千万不要手动修改这个目录里面的文件,不然改乱了,就把Git仓库给破坏了。

查看隐藏目录
[root@foundation61 demo]$ l.
.  ..  .git
[root@foundation61 demo]$ cd .git/
[root@foundation61 .git]# ls
branches  config  description  HEAD  hooks  info  objects  refs

这里写图片描述

二、添加文件到Git仓库

新建文件readme.md
[root@foundation61 .git]$ cd ..
[root@foundation61 demo]$ echo westos > readme.md
[root@foundation61 demo]$ git status -s
?? readme.md

第一步,用命令git add告诉Git,把文件添加到仓库;实际上就是把文件修改添加到暂存区:
[root@foundation61 demo]$ git add readme.md
[root@foundation61 demo]$ git status -s
A  readme.md

第二步,用命令git commit告诉Git,把文件提交到仓库;-m后面输入的是本次提交的说明;实际上就是把暂存区的所有内容提交到当前分支
因为我们创建Git版本库时,Git自动为我们创建了唯一一个master分支,所以,现在,git commit就是往master分支上提交更改。
[root@foundation61 demo]$ git commit -m "add readme.md"  ##这里会提示我们需要认证
进行认证
[root@foundation61 demo]$ git config --global user.name wxj
[root@foundation61 demo]$ git config --global user.email "573245320@qq.com"
认证完后在根下生成.gitconfig文件,查看里面有认证内容
[root@foundation61 demo]$ cd
[root@foundation61 ~]$ cat .gitconfig 

这里写图片描述

这里写图片描述

三、git status -s 命令

运行 git status -s ,状态报告输出如下:
$ git status -s
 M README
MM Rakefile
A  lib/git.rb
M  lib/simplegit.rb
?? LICENSE.txt

新添加的未跟踪文件前面有 ?? 标记,新添加到暂存区中的文件前面有 A 标记,修改过的文件前面
有 M 标记。 你可能注意到了 M 有两个可以出现的位置,出现在右边的 M 表示该文件被修改了但
是还没放入暂存区,出现在靠左边的 M 表示该文件被修改了并放入了暂存区。 例如,上面的状态
报告显示: README 文件在工作区被修改了但是还没有将修改后的文件放入暂存区,lib/simplegit.rb 
文件被修改了并将修改后的文件放入了暂存区。 而 Rakefile 在工作区被修改并提交到暂存区后
又在工作区中被修改了,所以在暂存区和工作区都有该文件被修改了的记录。

四、查看日志

[root@foundation61 demo]$ git log
[root@foundation61 demo]# git log --pretty=oneline

这里写图片描述

这里写图片描述

五、忽略文件

建立隐藏文件
[root@foundation61 ~]$ cd demo/
[root@foundation61 demo]# touch .file1
查看状态
[root@foundation61 demo]# git status -s
?? .file1
编辑隐藏文件
[root@foundation61 demo]# vim .gitignore
.*
[root@foundation61 demo]# l.
.  ..  .file1  .git  .gitignore
查看状态时没有内容
[root@foundation61 demo]# git status -s

六、撤销修改

1、git reset --hard HEAD^   ##把暂存区的修改撤销掉,回退到上一个版本。注:上一个版本就是HEAD^,上上一个版本就是HEAD^^,当然往上100个版本写100个^比较容易数不过来,所以写成HEAD~100。

2、git reflog  ##如果在回退以后又想再次回到之前的版本,git reflog 可以查看所有分支的所有操作记录(包括commit和reset的操作),包括已经被删除的commit记录,git log则不能察看已经删除了的commit记录

3、git checkout -- file   ##文件在工作区的修改全部撤销。这里有两种情况:

一种是readme.txt自修改后还没有被放到暂存区,现在,撤销修改就回到和版本库一模一样的状态;

一种是readme.txt已经添加到暂存区后,又作了修改,现在,撤销修改就回到添加到暂存区后的状态。

实验:
git reset –hard HEAD^

查看有2个git add操作
[root@foundation61 demo]# git log --pretty=oneline
3b1dd18257f942d835d7c6d61b2468444e3d6788 add test.txt
69ba5e53566352ef4c62dc1b0037211bf3184eef add readme.md
回退到上一个版本
[root@foundation61 demo]$ git reset --hard HEAD^  回退到上一个版本"add readme.md"
查看只有一个文件
[root@foundation61 demo]$ ls
readme.md
查看操作记录
[root@foundation61 demo]$ git reflog 
59209c5 HEAD@{0}: reset: moving to HEAD^
a1876a5 HEAD@{1}: commit: add test.txt
59209c5 HEAD@{2}: commit (initial): add readme.md
回到之前的版本  这里hard后面跟的ID为回到add test.txt版本的前面的ID
[root@foundation61 demo]$ git reset --hard a1876a5
HEAD is now at a1876a5 add test.txt
再次查看操作记录
[root@foundation61 demo]$ git reflog
a1876a5 HEAD@{0}: reset: moving to a1876a5
59209c5 HEAD@{1}: reset: moving to HEAD^
a1876a5 HEAD@{2}: commit: add test.txt
59209c5 HEAD@{3}: commit (initial): add readme.md
[root@foundation61 demo]$ ls
readme.md  test.txt

git checkout – file
第一种:修改后还没有被放到暂存区,现在撤销修改

编辑test.txt
[root@foundation61 demo]# vim test.txt 
[root@foundation61 demo]# git status -s
 M test.txt
[root@foundation61 demo]# cat test.txt 
haha
haha
撤销修改
[root@foundation61 demo]# git checkout -- test.txt
[root@foundation61 demo]# cat test.txt 
haha

这里写图片描述
第二种:已经添加到暂存区后,又作了修改,现在,撤销修改就回到添加到暂存区后的状态

[root@foundation61 demo]# git add test.txt 
[root@foundation61 demo]# vim test.txt 
[root@foundation61 demo]# git status
[root@foundation61 demo]# git checkout -- test.txt 
[root@foundation61 demo]# git status

这里写图片描述

七、删除文件

先添加文件test.txt到Git并且提交
$ git add test.txt
$ git commit -m "add test.txt"
删除文件
[root@foundation61 demo]# rm -f test.txt 
查看哪些文件被删除了
[root@foundation61 demo]# git status
现在你有两个选择,一是确实要从版本库中删除该文件,那就用命令git rm删掉,并且git commit:
$ git rm test.txt
$ git commit -m "remove test.txt"
另一种情况是删错了,因为版本库里还有呢,所以可以很轻松地把误删的文件恢复到最新版本:
恢复误删文件
[root@foundation61 demo]# git checkout -- test.txt
查看
[root@foundation61 demo]# ls
readme.md  test.txt

恢复误删文件

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值