GIT的使用--以及idea上的使用

GIT的使用

git是一个版本管理的仓库,接下来就是版本的一些实战信息。

git 中文件的状态有 :暂存(staged)、已经修改(modified)、已经提交(commited)

下载安装好我们的git后,创建一个空文件夹1.然后进入这个文件夹里面,使用 **git init **创建仓库,使用命令后就会存在.git的文件夹

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5pSjjbMc-1637327120959)(GIT的使用.assets/image-20211119110856301.png)]

本地管理

创建一个文件1.txt

image-20211119111012589

这个时候文件并不属于这个版本管理仓库,因为还没有执行文件的三个状态。使用git status查看这个目录中的文件状态。表示这个文件还没有被追踪。使用 git add 1.txt将文件加到暂存区中。

image-20211119111139868

再次查询状态,发现暂存区中已经有了一个文件,现在我们可以对这个文件进行提交或者删除。

image-20211119111318142

但我们修改这个文件中的内容后,再次查看状态,会发现这个已经被修改了。但是我们依旧可以提交我们暂存区的内容git commit -m ‘first use’ 。命令中的m表示注释,这里的注释的内容是文本中的内容。

image-20211119111503272

再次查询状态,发现暂存区中已经没有文件了,因为已经被提交了。

image-20211119111740053

通过命令git log可以看到我们的提交日志

image-20211119111853166

接下来将修改的文件第二次提交git commit -m ‘first use1’ 。注释依旧是文本中的内容。查看提交日志可以看到我们每次提交的信息

image-20211119112120302

image-20211119113539468

当我们提交的次数很多的时候,我们可以对git log -条数 --pretty=oneline进行参数的控制。

image-20211119112556677

上图我们会发现我们的head指针是指向我们最后一次提交的,所以要想控制版本,我们将head指向其他的就行。

image-20211119113900383

版本控制

版本的回退

我们的版本目前有2个:first use1和first use。**git reset --hard HEAD^该命令表示将指针指向上一次的提交:first use。^**控制版本回退的次数:**git reset --hard HEAD^^**表示退回到当前版本的前2个版本【3->1】

也可以使用git reset --hard HEAD~数量,表示回退的版本数量

image-20211119114119435

image-20211119114158637

这个时候我们的1.txt中的内容也发生了改变

image-20211119114421043

跳到未来的版本

当我们跳到之前的版本的时候,使用git log会显示不出未来的日志的,所以使用新的命令查看操作日志 reglog

#查看所有的执行log
git reflog
df4c499 (HEAD -> master) HEAD@{0}: reset: moving to HEAD^
ba1a4f1 HEAD@{1}: commit: first use1
df4c499 (HEAD -> master) HEAD@{2}: commit (initial): first use

找到想要跳的版本号,执行命令就可

$ git reset --hard "ba1a4f1"
HEAD is now at ba1a4f1 first use1

查询日志,可以看到Head指向了first use1。此时我们的文件内容已经发生了改变

$ git log -2 --pretty=oneline
ba1a4f1f40bde45fdf3f9cbbfbdaa0695d350625 (HEAD -> master) first use1
df4c49908a0ea3d3372fd8f38621d7309f3c6707 first use

image-20211119120443969

检出版本

当我们本地文件的1.txt文件被删除的时候,我们可以使用checkout 命令将删除的文件进行恢复

$ git checkout -- 1.txt(文件名)

删除文件

先将一个新的文件2.txt提交到仓库。使用 git ls-files查看仓库中的所有文件

$ git ls-files
1.txt
2.txt

使用命令rm删除仓库的内容,同时删除我们本地的内容。此时就不能再检出了

$ git rm 2.txt
rm '2.txt'
$ git ls-files
1.txt
$ git commit

远程仓库

将项目拷贝到本地

$ git clone url

然后和上面一样操作一些commit

将本地库推送到云端

百度ssh配置git

检查ssh账户配置
#验证SSH是否成功
$ ssh -T git@gitee.com 
如果有提示,直接yes,再次执行会提示 successful
执行流程
git init
git add .
git commit -m '注释'
#远程的url绑定到该项目,同时取别名 origin
git remote add origin url
#将本地的仓库推送到url上
git push -u origin master


#查看本地仓库对应的远程仓库

$ git remote -v

当我们使用远程地址绑定的时候,需要注意远程仓库的地址:https还有ssh,如果配置类ssh就可以使用ssh

$ git remote add feature1 git@gitee.com:P65782152/java_study_heima.git

执行推送的时候报错:需要我们先pull一下

$ git push -u feature1 master
    To gitee.com:P65782152/java_study_heima.git
     ! [rejected]        master -> master (non-fast-forward)
    error: failed to push some refs to 'git@gitee.com:P65782152/java_study_heima.git'
    hint: Updates were rejected because the tip of your current branch is behind
    hint: its remote counterpart. Integrate the remote changes (e.g.
    hint: 'git pull ...') before pushing again.
    hint: See the 'Note about fast-forwards' in 'git push --help' for details.

使用pull报错:说明两个仓库不相关,拒绝merge 不相关的历史, 因为本地也有提交。

$ git pull feature1 feature #第一个feature1是地址的别名,feature是远程地址的分支名
    From gitee.com:P65782152/java_study_heima
     * branch            feature    -> FETCH_HEAD
    fatal: refusing to merge unrelated histories
解决办法

使用允许无关联的历史记录的参数拉取之后再 push

$ git pull feature1 feature --allow-unrelated-histories
    From gitee.com:P65782152/java_study_heima
     * branch            feature    -> FETCH_HEAD
    Merge made by the 'recursive' strategy.
     .gitignore | 23 +++++++++++++++++++++++
     1 file changed, 23 insertions(+)
     create mode 100644 .gitignore

或者使用如下命令

git push -u origin master -f

再次执行push

$ git push feature1 master
    Enumerating objects: 13, done.
    Counting objects: 100% (13/13), done.
    Delta compression using up to 8 threads
    Compressing objects: 100% (7/7), done.
    Writing objects: 100% (12/12), 1.08 KiB | 553.00 KiB/s, done.
    Total 12 (delta 0), reused 0 (delta 0)
    remote: Powered by GITEE.COM [GNK-6.2]
    To gitee.com:P65782152/java_study_heima.git
       6263f15..f58c100  master -> master

分支管理

git checkout branch #切换分支
git checkout -b new_brach #新建分支且切换到新分支
git checkout -d brach #删除分支
git branch  # 查看所有的分支 *为当前分支
git merge branch # 合并分支
git branch -m|-M oldbranch newbranch  #重命名分支

本地的分支

$ git branch
	* master
$ git checkout -b dev
$ git branch
    * dev
      master
      
#创建文件2
$ touch 2.txt
$ git add 2.txt
$ git commit -m '分支dev的提交文件2'
$ git ls-files
    .gitignore
    1.txt
    2.txt

#切换到master分支
$ git checkout master
	Switched to branch 'master'
$ git ls-files
    .gitignore
    1.txt
    
#可以看到主分支是没有2.txt文件的,所以我们可以执行合并 merge
$ git merge dev
    Updating f58c100..6cf309b
    Fast-forward
     2.txt | 0
     1 file changed, 0 insertions(+), 0 deletions(-)
     create mode 100644 2.txt
 $ git ls-files
    .gitignore
    1.txt
    2.txt

image-20211119135035298

分支的Push和PUll

git branch -a #查看所有的分支
git push origin beanchname #推送本地分支到远程
gti push origin :remotebranch #删除远程分支(本地分支还在)
git checkout -b localbranch origin/remotebranch # 拉取远程的分支并且在本地创建分支

标签管理

tag

idea的使用

idea中创建仓库->add->commit->remote->push

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

魔芋小灰菜

不要下次一定,要一键三连

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值