Git总结

这篇博客全面总结了Git的各项操作,包括文件的三种状态、提交更新、忽略文件、查看历史、重命名、远程仓库管理、标签及分支操作等。通过实例详细解释了如何在不同场景下使用Git进行版本控制,特别强调了处理冲突和合并分支的策略。
摘要由CSDN通过智能技术生成

Git中的文件有三种状态:

  1. 已提交(commited)
  2. 已暂存(staged)相当于记录本地文件的一个快照,会随着之后的提交更新本地仓库的状态。
  3. 已修改(modified)

从现有工作目录中初始化一个仓库

git init

克隆一个仓库到本地

git clone [url]

初始化用户信息(很重要)这些信息会写入到你的没一次提交当中

git config --global user.name "chen run"
git config --global user.email 247858512@qq.com

git config --list显示Git的所有配置信息

git help <verb>获取帮助

记录每次更新到仓库

git status查看文件状态

git status -s or git status --short显示一种更为紧凑的文件状态

git add <filename>开始跟踪一个文件

git add .

忽略文件

创建.gitignore文件

查看已暂存和未暂存的修改

git diff查看工作目录相比于暂存区更新的内容(尚未暂存的改动)

git diff --cached查看暂存区中更新的内容(上次commit后暂存区的所有改动)

提交更新

git commit -m "message"

git commit -a -m "message" Git会把所有已跟踪过的文件全部提交到repository而忽略git add步骤

移除文件

git rm <filename>移除后该文件不再纳入版本管理(工作目录上该文件也会被删除)

git rm --cached <filename>将文件从Git仓库移除但仍保存在工作目录

重命名文件

git mv <oldFileName> <newFileName>

查看提交历史

git log

git log -p显示每次提交的差异

撤销

你提交后发现忘记了暂存某些需要的修改,可以像下面这样操作:

git commit -m 'initial commit'
git add forgotten_file
git commit --amend

有时候已经修改了两个文件并且想要将它们作为两次独立的修改提交,但是却意外地输入了git add * 暂存了它们两个。 如何只取消暂存两个中的一个呢?

git restore --staged <file>

将未暂存的文件恢复为最近一次暂存时候的状态

"git restore <file>

远程仓库

查看远程仓库

git remote

git remote -v会显示对应的url

git remote show <remote-name>查看远程仓库的更多信息

添加远程仓库

git remote add <shortname> <url>

git fetch <shortname>从shortname仓库中获取数据

推送到远程仓库

git push <remote-name> <branch-name>

远程仓库的重命名

git remote rename <oldname> <newname>

标签

列出标签

git tag

创建标签

git tag -a v1.4 -m "my version 1.4"
git show v1.4

分支

##分支的新建、合并、删除

在master分支下创建一个test.txt文件,写入内容如下:

test.txt

this is txt file
created by master branch

然后通过git add .git commit -m "注释"操作将文件提交到本地仓库,之后创建test分支,并切换到test分支下:

git checkout -b test

在test分支下修改test.txt文件,新的txt文件为

test.txt

this is txt file
created by master branch
modified by test branch

同样git add .git commit -m "注释"同步到本地仓库然后我们返回master分支

git checkout master

此时工作目录下的test.txt文件内容也发生了改变,也就是切换分支后git会重置你的工作目录,使其看起来像回到了你那个分支最后一次提交时候的样子。

test.txt

this is txt file
created by master branch

返回到了master分支的快照,该快照比test的快照旧一个版本。

如果我们再切回到test分支下,并在test.txt文件内新增一条语句:

test.txt

this is txt file
created by master branch
modified by test branch
modified by test branch again

然后我们不git add .也不git commit -m "注释"直接git checkout master则会报错:

$ git checkout master
error: Your local changes to the following files would be overwritten by checkout:
        CONTRIUBTIING.md
Please commit your changes or stash them before you switch branches.
Aborting

这里可以看出如果对文件进行了修改,则必须提交后才能切换分支。

我们对test.txt文件进行git add .git commit -m "注释"操作:此时master分支和test分支下test.txt文件的快照内容分别为:

master:

this is txt file
created by master branch

test:

this is txt file
created by master branch
modified by test branch
modified by test branch again

我们切换回master分支,执行如下操作将test分支合并到master分支

git merge test

此时test分支和master分支的快照其实就是同一个了。

我们在master分支下的快照(也同样是test的快照)下新建一个demo分支并修改test.txt文件为

demo(注意:前四行是原来就有的内容,第五行才是demo新增的):

this is txt file
created by master branch
modified by test branch
modified by test branch again
modified by demo branch 

然后切换到test分支下修改文件为

test(注意:前四行是原来就有的内容,第五行才是demo新增的):

this is txt file
created by master branch
modified by test branch
modified by test branch again
modified by test branch again and again

然后我们切换回master分支,并在此快照上合并demo分支

git merge demo

合并很顺利

然后合并test分支

git merge test

噢噢,报错了,原来是出现了冲突,冲突出现在第五行,因为两个快照第五行的内容各不相同,git不知道用哪个分支的快照。。。

$ git merge test
Auto-merging test.txt
CONFLICT (content): Merge conflict in test.txt
Automatic merge failed; fix conflicts and then commit the result.

然后打开我们的test.txt文件,发现文件发生了改变:

test.txt

this is txt file
created by master branch
modified by test branch
modified by test branch again
<<<<<<< HEAD
modified by demo branch
=======
modified by test branch again and againv
>>>>>>> test

很形象的显示了处突发生的位置,这个时候我们就手动修改test.txt文件为我们想要的样子,比如我们保留test分支的内容,并删除其他多余的字段:

test.txt

this is txt file
created by master branch
modified by test branch
modified by test branch again
modified by test branch again and againv

然后git add .git commit -m "注释"一下,大功告成~(.md文件不用关闭打开就能自动刷新)

然后我们执行如下操作删除demo分支

git branch -d demo

分支管理

查看本地所有分支:

git branch
$ git branch
  demo
* master
  merged
  test

表示有三个分支,分别是master、demo、test。*表示现在我们再master分支下,其中demo分支已经合并到了master分支,test分支尚未合并到master分支,我们可以删除demo分支,但是如果我们删除test分支的话git就会报错,因为删除没有何如的分支会损失内容,这一点看git还是挺安全的。

我们也可以分别查看已合并到当前分支的分支和尚未合并到当前分支的分支:

git branch --merged
git branch --no-merged

我们可以查看所以本地分支最后一次commit的注释信息

git branch -v

远程分支

本章详细内容可参考ProGit的分支开发工作流和远程分支章节。

首先明确一个概念,当我们从一个远程仓库中clone下来一个项目的时候,git会在本地创建两个分支master和origin/master,其中master分支是我们默认的本地分支,origin/master被叫做远程跟踪分支,相当于一个指向origin远程仓库master分支快照的一个指针,当本地仓库不和远程仓库交互的时候,这个指针会一直指向在clone时候master的那个版本快照,在这段时期我们可以在master分支下修改我们的项目,别人也可以更新origin仓库的master分支

在这里插入图片描述

查看远程仓库:

git remote
git remote show 

同步远程仓库:

git fetch [remote repository]
//比如拉取origin仓库的内容
git fetch orgin

这里的fetch相当于只是更新了origin/master(因为我们当前在master分支下),工作目录的内容是没有变的,这个时候我们需要合并origin/master分支到我们工作的master分支:

git merge origin/master

注意:git pull origin/master相当于git fetch origin/mastergit merge origin/master两者的结合。

更新远程分支:

git push [remote] [branch]
//例如更新origin的master分支。
git push origin master

如果你觉得麻烦,你也可以设置origin/master“远程跟踪分支”为“跟踪分支”,跟踪分支是与远程分支有直接关系的本地分支,如果在一个跟踪分支上输入git pull,git能自动识别去哪个服务器上抓取,合并到哪个分支,因为我么这里的master分支是clone命令自动创建的,本身已经是一个跟踪分支了,但我们先假设它不是一个跟踪分支。可以通过如下命令设置为一个跟踪分支:

// 方法1:
git branch -u origin/master
// 方法2:
git branch --set-upstream-to origin/master

设置好master为远程master的追踪分支后可以简化git push origin/mastergit push

如果想要查看所有的追踪分支使用git branch -vv输出为:

$ git branch -vv
iss53 7e424c3 [origin/iss53: ahead 2] forgot the brackets
master 1ae2a45 [origin/master] deploying index fix
* serverfix f8674d9 [teamone/server-fix-good: ahead 3, behind 1] this
should do it
testing 5ea463a trying something new

这里可以看到 iss53 分支正在跟踪 origin/iss53 并且 “ahead” 是 2,意味着本地有两个提交还没有推送 到服务器上。 也能看到 master 分支正在跟踪 origin/master 分支并且是最新的。 接下来可以看到 80 serverfix 分支正在跟踪 teamone 服务器上的 server-fix-good 分支并且领先 3 落后 1,意味着服务器上有一次提交还没有合并入同时本地有三次提交还没有推送。 最后看到 testing 分支并没有 跟踪任何远程分支。

需要重点注意的一点是这些数字的值来自于你从每个服务器上最后一次抓取的数据。 这个命令并没有连接服务 器,它只会告诉你关于本地缓存的服务器数据。 如果想要统计最新的领先与落后数字,需要在运行此命令前抓 取所有的远程仓库。 可以像这样做:

git fetch --all
git git branch -vv

添加一个新的远程仓库引用到当前的项目:

git remote add [repository_name]

删除一个远程分支:

git push origin --delete master
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值