Git 本地仓库常用操作

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zc_ad/article/details/84951478

1.安装git

在Ubuntu上安装git

sudo apt-get install git

2.初始化git 

#告诉git,自己的名字与邮箱
git config --global user.name "Your Name"
git config --global user.email "[email protected]"

3.安装完后,在Ubuntu中创建版本库

#创建git仓库文件
cd /root/xichuan
mkdir git
cd git
#初始化仓库,告诉git,/root/xichuan/git就是版本仓库
git init    

4.尝试在仓库中添加一个文件,并提交到本地仓库

touch test.txt
#将文件添加到暂存区
git add test.txt
#git status命令可以让我们时刻掌握仓库当前的状态
git status
#进行提交, -m后面输入的是本次提交的说明,可以输入任意内容,当然最好是有意义的
git commit -m "this is the first doc"  

#这样就将所创建的文件提交到本地仓库上了

 

5.再次在文件中添加文字

#在test.txt中添加一行文字
vi /test.txt
this is the test word!
##会发现git提示readme.txt文件被修改
git status   

##git告诉我们,test.txt修改了哪几行
git diff test.txt 

#提交本次操作
git add test.txt
git commit -m 'this is the second connit'

6.版本回退

#查看提交日志
git log
#查看简洁的回退信息
git log --pretty=oneline

#回退到上一次提交
git reset --hard HEAD^

#会退到任一节点
#git reset --hard 'commintId'  #commintId 是在git log命令查询的

我们继续使用git log命令:

扫描二维码关注公众号,回复: 4693952 查看本文章

发现第二次提交的记录已经不见了,此时我们想取消版本回退怎么办?

使用git reflog命令查找我们命令操作记录,上面会有对应的每次操作的commitId

#git reflog       #第一排是commitId
git rest --hard 'a588b5f'    #此commitId是git reflog查询得到的

7.撤销修改

git checkout -- test.txt   ##取消修改


两种情况:
一种是test.txt自修改后还没有被放到暂存区,现在,撤销修改就回到和版本库一模一样的状态;
一种是test.txt已经添加到暂存区后,又作了修改,现在,撤销修改就回到添加到暂存区后的状态。

演示第二种情况:

1.先在test.txt中添加文字:this is the second word!!!

2.添加暂存区:

3.在test.txt文件中再添加文字:this is the third word!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

4.撤销修改:git checkout -- test.txt

发现,工作区文件的内容和第一次提交后在暂存区的内容一样

但使用git status 命令发现,文件还在暂存区中

把暂存区的修改回退到工作区:git reset HEAD test.txt

git reset HEAD test.txt

8.删除文件

rm test.txt  #先将工作区的文件删除
git status   #查看状态
#git checkout -- test.txt   #可选此命令,将删掉的test.txt从版本库中恢复出来
git rm text.txt  #告诉git暂存区将test.txt文件删除
#git rest HEAD test.txt  #可选此命令,取消git删除
git commit -m 'rm test.txt'   #提交此删除操作


命令总结:

git config --global user.name "Your Name"            #初始化姓名
git config --global user.email "[email protected]"   #初始化邮箱


git init   #创建本地仓库


git add test.txt  #将添加操作提交到暂存区
git rm text.txt   #将删除操作提交到暂存区
git diff test.txt #查看工作区与本地仓库的不同
git status        #查看仓库状态
git commit -m "co.."  #将修改提交到本地仓库


git log  #查看提交日志
git log --pretty=oneline   #查看提交的简洁日志
git reflog   #查看操作日志


git reset --hard HEAD^    #回退到上次提交
git rest --hard 'commitId'   #会退到指定的提交节点


git checkout -- readme.txt  #撤销修改
git reset HEAD readme.txt   #把暂存区的修改回退到工作区
 

#---------远程仓库后的操作
git clone [email protected]:/home/git/mytest   #克隆远程仓库
git push origin master                         #提交到远程仓库



#-------分支管理的操作
git checkout -b dev  ##创建并切换为dev分支

git branch dev   #创建dev分支
git checkout dev #切换到dev分支

git branch   #查看所有分支
#git checkout master #切换到master分支 

git merge dev  #将dev分支的代码合并到当前分支

git branch -d dev #删除dev分支

资料:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000

猜你喜欢

转载自blog.csdn.net/zc_ad/article/details/84951478