Git的使用方法

由于学习了Git,我将学到的内容整理并发布在这里。
我整理了一些简单且常用的内容。

Git是什么?

Git是一种版本管理系统,可以轻松地进行文件版本管理的工具。
版本指的是对文件进行更新或更改的内容。
经常使用Office的人们也常用Git。

    • 〇〇設計書_20220511.xlsx

 

    ××報告書_v2.xlsx

我认为这个做法是把日期放在文件名中,或者给文件命名为v2这样的名字,然后将旧文件作为备份放在old文件夹中。
然而,在这种情况下,如果多个人同时更新同一个文件,就无法知道是谁在哪里进行了更新。
特别是在多人开发时,Git常被使用。
(例如在谷歌的电子表格中,可以看到谁修改了哪部分内容。
不过如果写错了就可以立刻知道是谁干的了、、、)

通过使用Git,可以让多个人同时更新一个文件,并且还可以回退到过去的文件版本。

关于GitHub和GitLab的区别

在代码管理方面存在着GitHub和GitLab两个选项。
这两个都用于将代码存储到远程仓库中,
但公开范围和功能设置有所不同。
以下是其使用情景的想象图。

    • GitHub:一般ユーザー向けで、インターネットに公開することができる。

 

    GitLab:業務向けで、社内サーバ−に配置することでインターネットには公開されない。

本地存储库和远程存储库

本地仓库指的是存在于本地环境中的仓库。
本地环境是指当前使用的服务器或个人计算机。
文件和文件更新记录将存储在本地仓库中。

将源代码信息存储在像GitHub这样的远程仓库中,并在互联网上公开。
要将源代码存储到远程仓库中,需要创建本地仓库并上传至远程仓库。

经常使用的Git命令

以下是常用的git命令列表。

关闭Git

从远程代码库复制文件到自己的设备上,执行如下操作。

$ git close <対象URL>
スクリーンショット 2022-05-12 22.17.34.png

初始化Git

在使用Git时,首先要创建本地仓库。

$ git init

将文件添加到Git仓库

更新文件后,首先将其添加到索引中,然后将其作为提交目标。接下来,将当前目录中的文件和目录作为提交目标。

$ git add .

如果要将一个文件作为提交对象,则需要指定文件名。

$ git add index.html

提交 git

可以保存履历情报。
您可以用-m留下评论。

$ git commit -m "first commit"

git状态

你可以确认当前状态。
如果没有更新,将以以下方式显示。

$ git status
On branch main
nothing to commit, working tree clean

创建文件后,它会显示您需要进行更改并执行git add的必要性。

$ touch test
$ git status
On branch main
Untracked files:
  (use "git add <file>..." to include in what will be committed)
	test

nothing added to commit but untracked files present (use "git add" to track)

在执行git add命令之后,将会显示如下内容,并显示commit预定文件。

$ git add .
$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	new file:   test

提交 Git commit 后可以返回到初始状态。

$ git commit -m "test"
$ git status
On branch master
nothing to commit, working tree clean

查询Git日志

可以通过更改日志和commitID来确认。
通过确认commitID,可以将其恢复到那个时间的状态。

$ git log
naoki@:~/git_test/test$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	new file:   test

naoki@:~/git_test/test$ git commit -m "test"
[master 285d070] test
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 test
naoki@:~/git_test/test$ git status
On branch master
nothing to commit, working tree clean
naoki@:~/git_test/test$ 
naoki@:~/git_test/test$ 
naoki@:~/git_test/test$ git log
commit 285d07091f6dea52dd5fb296e68f1fd69d97de8b (HEAD -> master)
Author: UserID <メールアドレス>
Date:   Thu May 12 22:34:26 2022 +0900

    test

commit 9888a3a9cbfb2947c6b6a2084a51965e12e54c68
Author: UserID <メールアドレス>
Date:   Wed May 11 22:04:35 2022 +0900

    3回目

commit 7ea2edacd09ea23cc224b120f6412574d5d89078
Author: UserID <メールアドレス>
Date:   Wed May 11 22:03:56 2022 +0900

    2回目のcommit

commit 2b5ac10b0320d780b34fcee44584fef533adac1a
Author: UserID <メールアドレス>
Date:   Wed May 11 22:02:30 2022 +0900

    新しいファイル

提交git

在上传文件到GitHub之前,您需要使用命令git add remote origin <要注册的存储库的URL>去注册远程存储库。

$ git add remote origin <URL>
$ git push origin main

切换到另一个分支

通过指定commitID,可以返回该时刻的信息。
可以通过git log查看commitID。

$ git checkout 9888a3a9cbfb2947c6b6a2084a51965e12e54c68

另外,通过命名,可以创建一个新的分支。

$ git checkout -b dev

感谢大家的阅读。再次感谢!

广告
将在 10 秒后关闭
bannerAds