Git 基本操作
<div style="color: black; text-align: left; margin-bottom: 10px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Git 的工作<span style="color: black;">便是</span>创建和<span style="color: black;">保留</span>你项目的快照及与之后的快照进行对比。本章将对<span style="color: black;">相关</span>创建与提交你的项目快照的命令作介绍。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">获取与创建项目命令</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">git init</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">用 git init 在目录中创建新的 Git 仓库。 你<span style="color: black;">能够</span>在任何时候、任何目录中这么做,完全是本地化的。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在目录中执行 git init,就<span style="color: black;">能够</span>创建一个 Git 仓库了。<span style="color: black;">例如</span><span style="color: black;">咱们</span>创建 run 项目:</p>$ mkdir runoob
$ cd runoob/$ git initInitialized empty Git repository in /Users/tianqixin/www/runoob/.git/# 在 /www/run/.git/ 目录初始化空 Git 仓库完毕。<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">此刻</span>你<span style="color: black;">能够</span>看到在你的项目中生<span style="color: black;">成为了</span> .git 这个子目录。 这<span style="color: black;">便是</span>你的 Git 仓库了,所有<span style="color: black;">相关</span>你的此项目的快照数据都存放在<span style="color: black;">这儿</span>。</p>ls -a. .. .git<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">git clone</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">运用</span> git clone 拷贝一个 Git 仓库到本地,让自己能够查看该项目,<span style="color: black;">或</span>进行修改。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">倘若</span>你需要与他人合作一个项目,<span style="color: black;">或</span>想要复制一个项目,<span style="color: black;">瞧瞧</span>代码,你就<span style="color: black;">能够</span>克隆那个项目。 执行命令:</p>git clone <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 为你想要复制的项目,就<span style="color: black;">能够</span>了。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">例如<span style="color: black;">咱们</span>克隆 Github 上的项目:</p>$ git clone git@github.com:schacon/simplegit.gitCloning into simplegit...remote: Counting objects: 13, done.remote: Total 13 (delta 0), reused 0 (delta 0), pack-reused 13Receiving objects: 100% (13/13), done.Resolving deltas: 100% (2/2), done.Checking connectivity... done.<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">克隆完成后,在当前目录下会生成一个 simplegit 目录:</p>$ cd simplegit/$ ls
README Rakefile lib<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">以上</span>操作将复制该项目的<span style="color: black;">所有</span>记录。</p>$ ls -a. .. .git README Rakefile lib
$ cd .git
$ ls
HEAD description info packed-refs
branches hooks logs refs
config index objects<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">默认<span style="color: black;">状况</span>下,Git 会<span style="color: black;">根据</span>你<span style="color: black;">供给</span>的 URL 所指示的项目的名<span style="color: black;">叫作</span>创建你的本地项目目录。 <span style="color: black;">一般</span><span style="color: black;">便是</span>该 URL 最后一个 / 之后的项目名<span style="color: black;">叫作</span>。<span style="color: black;">倘若</span>你想要一个不<span style="color: black;">同样</span>的名字, 你<span style="color: black;">能够</span>在该命令后加上你想要的名<span style="color: black;">叫作</span>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">基本快照</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Git 的工作<span style="color: black;">便是</span>创建和<span style="color: black;">保留</span>你的项目的快照及与之后的快照进行对比。本章将对<span style="color: black;">相关</span>创建与提交你的项目的快照的命令作介绍。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">git add</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">git add 命令可将该文件添加到缓存,如<span style="color: black;">咱们</span>添加以下两个文件:</p>$ touch README
$ touch hello.php
$ ls
README hello.php
$ git status -s?? README?? hello.php
$<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">git status 命令用于查看项目的当前状态。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">接下来<span style="color: black;">咱们</span>执行 git add 命令来添加文件:</p>$ git add README hello.php<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">此刻</span><span style="color: black;">咱们</span>再执行 git status,就<span style="color: black;">能够</span>看到这两个文件<span style="color: black;">已然</span>加上去了。</p>$ git status -s
A README
A hello.php
$<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">新项目中,添加所有文件很<span style="color: black;">广泛</span>,<span style="color: black;">咱们</span><span style="color: black;">能够</span><span style="color: black;">运用</span> <strong style="color: blue;">git add .</strong> 命令来添加当前项目的所有文件。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">现在<span style="color: black;">咱们</span>修改 README 文件:</p>$ vim README<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在 README 添加以下内容:<strong style="color: blue;"># Runoob Git 测试</strong>,<span style="color: black;">而后</span><span style="color: black;">保留</span>退出。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">再执行一下 git status:</p>$ git status -s
AM README
A hello.php<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">"AM" 状态的意思是,这个文件在<span style="color: black;">咱们</span>将它添加到缓存之后又有改动。改动后<span style="color: black;">咱们</span>在执行 git add 命令将其添加到缓存中:</p>$ git add .$ git status -s
A README
A hello.php<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">当你要将你的修改<span style="color: black;">包括</span>在即将提交的快照里的时候,需要执行 git add。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">git status</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">git status 以查看在你上次提交之后<span style="color: black;">是不是</span>有修改。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">我演示该命令的时候加了 -s 参数,以<span style="color: black;">得到</span>简短的结果输出。<span style="color: black;">倘若</span>没加该参数会<span style="color: black;">仔细</span>输出内容:</p>$ git statusOn branch masterInitial commitChanges to be committed:
(use "git rm --cached <file>..." to unstage) new file: README
new file: hello.php<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">git diff</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">执行 git diff 来查看执行 git status 的结果的<span style="color: black;">仔细</span>信息。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">git diff 命令<span style="color: black;">表示</span>已写入缓存与已修改但尚未写入缓存的改动的区别。git diff 有两个<span style="color: black;">重点</span>的应用场景。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">尚未缓存的改动:<strong style="color: blue;">git diff</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">查看已缓存的改动: <strong style="color: blue;">git diff --cached</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">查看已缓存的与未缓存的所有改动:<strong style="color: blue;">git diff HEAD</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">表示</span>摘要而非<span style="color: black;">全部</span> diff:<strong style="color: blue;">git diff --stat</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在 hello.php 文件中输入以下内容:</p><?php
echo :www.run.com;?>$ git status -s
A README
AM hello.php
$ git diff
diff --git a/hello.php b/hello.php
index e69de29..69b5711 100644--- a/hello.php+++ b/hello.php@@ -0,0 +1,3 @@+<?php+echo :www.run.com;+?><p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">git status <span style="color: black;">表示</span>你上次提交更新后的更改<span style="color: black;">或</span>写入缓存的改动, 而 git diff 一行一行地<span style="color: black;">表示</span>这些改动<span style="color: black;">详细</span>是啥。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">接下来<span style="color: black;">咱们</span>来查看下 git diff --cached 的执行效果:</p>$ git add hello.php
$ git status -s
A README
A hello.php
$ git diff --cached
diff --git a/README b/READMEnew file mode 100644index 0000000..8f87495--- /dev/null+++ b/README@@ -0,0 +1 @@+# Runoob Git 测试diff --git a/hello.php b/hello.phpnew file mode 100644index 0000000..69b5711--- /dev/null+++ b/hello.php@@ -0,0 +1,3 @@+<?php+echo 菜鸟教程:www.runoob.com;+?><p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">git commit</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">运用</span> git add 命令将想要快照的内容写入缓存区, 而执行 git commit 将缓存区内容添加到仓库中。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Git 为你的每一个提交都记录你的名字与电子邮箱<span style="color: black;">位置</span>,<span style="color: black;">因此</span><span style="color: black;">第1</span>步需要配置用户名和邮箱<span style="color: black;">位置</span>。</p>$ git config --global user.name run$ git config --global user.email test@run.com<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">接下来<span style="color: black;">咱们</span>写入缓存,并提交对 hello.php 的所有改动。在首个例子中,<span style="color: black;">咱们</span><span style="color: black;">运用</span> -m 选项以在命令行中<span style="color: black;">供给</span>提交注释。</p>$ git add hello.php
$ git status -s
A README
A hello.php
$ $ git commit -m <span style="color: black;">第1</span>次版本提交 <span style="color: black;">第1</span>次版本提交
2 files changed, 4 insertions(+)
create mode 100644 README
create mode 100644 hello.php<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">此刻</span><span style="color: black;">咱们</span><span style="color: black;">已然</span>记录了快照。<span style="color: black;">倘若</span><span style="color: black;">咱们</span>再执行 git status:</p>$ git status# On branch masternothing to commit (working directory clean)<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">以上输出说明<span style="color: black;">咱们</span>在<span style="color: black;">近期</span>一次提交之后,<span style="color: black;">无</span>做任何改动,是一个"working directory clean:干净的工作目录"。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">倘若</span>你<span style="color: black;">无</span>设置 -m 选项,Git 会尝试为你打开一个编辑器以填写提交信息。 <span style="color: black;">倘若</span> Git 在你对它的配置中找不到<span style="color: black;">关联</span>信息,默认会打开 vim。屏幕会像<span style="color: black;">这般</span>:</p># Please enter the commit message for your changes. Lines starting# with # will be ignored, and an empty message aborts the commit.# On branch master# Changes to be committed:# (use "git reset HEAD <file>..." to unstage)## modified: hello.php#~~".git/COMMIT_EDITMSG" 9L, 257C<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">倘若</span>你觉得 git add 提交缓存的流程太过繁琐,Git <span style="color: black;">亦</span><span style="color: black;">准许</span>你用 -a 选项跳过这一步。命令格式如下:</p>git commit -a<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">咱们</span>先修改 hello.php 文件为以下内容:</p><?php
echo :www.run.com;echo 我爱学习:www.run.com;?><p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">再执行以下命令:</p>git commit -am 修改 hello.php 文件 修改 hello.php 文件
1 file changed, 1 insertion(+)<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">git reset HEAD</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">git reset HEAD 命令用于取消已缓存的内容。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">咱们</span>先改动文件 README 文件,内容如下:</p># Run Git 测试#<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">hello.php 文件修改为:</p><?php
echo :www.run.com;echo :www.run.com;echo :www.run.com;?><p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">此刻</span>两个文件修改后,都提交到了缓存区,<span style="color: black;">咱们</span><span style="color: black;">此刻</span>要取消其中一个的缓存,操作如下:</p>$ git status -s
M README
M hello.php
$ git add .$ git status -s
M README
M hello.pp
$ git reset HEAD -- hello.php
Unstaged changes after reset:M hello.php
$ git status -s
M README
M hello.php<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">此刻</span>你执行 git commit,只会将 README 文件的改动提交,而 hello.php 是<span style="color: black;">无</span>的。</p>$ git commit -m 修改 修改
1 file changed, 1 insertion(+)$ git status -s
M hello.php<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">可以看到 hello.php 文件的修改并未提交。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">此时</span><span style="color: black;">咱们</span><span style="color: black;">能够</span><span style="color: black;">运用</span>以下命令将 hello.php 的修改提交:</p>$ git commit -am 修改 hello.php 文件 修改 hello.php 文件
1 file changed, 1 insertion(+)$ git statusOn branch master
nothing to commit, working directory clean<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">简而言之,执行 git reset HEAD 以取消之前 git add 添加,但不<span style="color: black;">期盼</span><span style="color: black;">包括</span>在下一提交快照中的缓存。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">git rm</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">倘若</span>只是简单地从工作目录中手工删除文件,运行 git status 时就会在 Changes not staged for commit 的提示。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">要从 Git 中移除某个文件,就必须要从已跟踪文件<span style="color: black;">名单</span>中移除,<span style="color: black;">而后</span>提交。<span style="color: black;">能够</span>用以下命令完成此项工作</p>git rm <file><p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">倘若</span>删除之前修改过并且<span style="color: black;">已然</span>放到暂存区域的话,则必须要用强制删除选项 -f</p>git rm -f <file><p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">倘若</span>把文件从暂存区域移除,但仍然<span style="color: black;">期盼</span><span style="color: black;">保存</span>在当前工作目录中,换句话说,仅是从跟踪<span style="color: black;">名单</span>中删除,<span style="color: black;">运用</span> --cached 选项<span style="color: black;">就可</span></p>git rm --cached <file><p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">如<span style="color: black;">咱们</span>删除 hello.php文件:</p>$ git rm hello.php
rm hello.php$ ls
README<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">不从工作区中删除文件:</p>$ git rm --cached README
rm README$ ls
README<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">能够</span>递归删除,即<span style="color: black;">倘若</span>后面跟的是一个目录做为参数,则会递归删除<span style="color: black;">全部</span>目录中的所有子目录和文件:</p>git rm –r *<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">进入某个目录中,执行此语句,会删除该目录下的所有文件和子目录。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">git mv</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">git mv 命令用于移动或重命名一个文件、目录、软连接。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">咱们</span>先把刚移除的 README 添加回来:</p>$ git add README<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">而后</span>对其重名:</p>$ git mv README README.md
$ ls
README.md<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/15269595445900af2241110~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1729831813&x-signature=3fRnTL3AVmL69AgvvSZ1vA2Ckrs%3D" style="width: 50%; margin-bottom: 20px;"></p>
</div>
页:
[1]