了解Git

首先

我试着把Git初学者对实际工作流程的想象写下来了。如果有错误或误解之处,请您指正,我将不胜感激!

参考网站:
https://backlog.com/ja/git-tutorial/
https://qiita.com/nnahito/items/565f8755e70c51532459

环境:git版本为2.20.1(苹果Git-117)。

2019/5/21更新

将环境变量配置文件排除在git管理之外

由于在.gitignore中进行了管理,所以根据需要进行编辑。
本次使用.env文件来管理环境变量,而在Laravel中,默认情况下.env是受到管理的,所以不需要额外的处理。

参考网站:
http://www-creators.com/archives/1662

在本地代码库中创建文件并提交

# ローカルの任意の場所にディレクトリを作成
$ mkdir tutorial

# 作成したディレクトリに移動
$ cd tutorial

# Git初期化
$ git init
Initialized empty Git repository in /Users/***/tutorial/.git/

# ファイルを作成(任意のテキストファイル)
$ vim sample.txt

# 状態を確認
$ git status
On branch master
No commits yet
Untracked files:
  (use "git add <file>..." to include in what will be committed)
    sample.txt
nothing added to commit but untracked files present (use "git add" to track)

# 作成したファイルをインデックスに登録する
$ git add sample.txt

# 状態を確認
$ git status
On branch master
No commits yet
Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
    new file:   sample.txt

# commitする
$ git commit -m "first commit"
[master (root-commit) 0b0d8ff] first commit
 1 file changed, 1 insertion(+)
 create mode 100644 sample.txt

# 状態を確認
$ git status
On branch master
nothing to commit, working tree clean

# ログを確認
$ git log
commit 0b0d8ff22e1e5f70434a591fc7ed8603ba486408 (HEAD -> master)
Author: ryoko-i8713 <49637061+ryoko-i8713@users.noreply.github.com>
Date:   Wed May 15 11:03:08 2019 +0900
    first commit

创建远程仓库到推送完成

创建远程仓库

スクリーンショット 2019-05-15 11.59.08.png
スクリーンショット 2019-05-15 11.59.40.png
スクリーンショット 2019-05-15 11.59.55.png

将所创建的远程代码库的名称进行登记

创建远程存储库会生成一个URL。
为了避免每次推送本地存储库时都需要指定URL,可以事先注册一个名称。


$ git remote add [名前] [リモートリポジトリ作成時に発行されたURL]

# originという名前で登録
$ git remote add origin https://github.com/***/tutorial.git

将远程仓库推送

因为这次是第一次将在本地仓库创建的文件推送,所以分支将是主分支(master)。

$ git push [送信先のリポジトリ名] [ブランチ名]

# 前項でリモートリポジトリをoriginで登録しているので、ローカルリポジトリをoriginにmasterとしてpushする。
$ git push origin master
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 264 bytes | 264.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To /Users/ryoko/tutorial-shared
 * [new branch]      master -> master

尝试作为另一个用户从远程仓库进行克隆

如果您想要一次性下载整个项目,请使用克隆(clone)命令。
拉取(pull)命令只下载远程库和本地库之间的差异部分。

我会以第三人称的身份尝试克隆刚刚推送的教程。

$ git clone [リモートリポジトリURL] [ディレクトリ名]

# 第三者用のディレクトリ「tutorial2」にリモートリポジトリを複製する
$ git clone https://github.com/ryoko-i8713/tutorial.git tutorial2
Cloning into 'tutorial2'...
remote: Enumerating objects: 6, done.
remote: Counting objects: 100% (6/6), done.
remote: Compressing objects: 100% (2/2), done.
Unpacking objects: 100% (6/6), done.
remote: Total 6 (delta 1), reused 6 (delta 1), pack-reused 0

# 中身を確認
$ cd tutorial2
$ ls -a
.       ..      .git        sample.txt  sample2.txt

克隆存储库〜进行更改〜推送


# tutorial2のsample.txtを編集する
$ vim sample.txt

# インデックスに登録する
$ git add sample.txt

# 状態を確認
$ git status
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)
    modified:   sample.txt

# commit
$ git commit -m "addの説明を追加"
[master e78c86b] addの説明を追加
 1 file changed, 1 insertion(+)

# リモートリポジトリ名を忘れたので確認しておく
$ git remote -v
origin  https://github.com/ryoko-i8713/tutorial.git (fetch)
origin  https://github.com/ryoko-i8713/tutorial.git (push)

# pushしてデータを送信(リモートリポジトリ名は既にoriginとして登録されているのでgit remote addは必要ない)
$ git push origin master
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 4 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 401 bytes | 401.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/ryoko-i8713/tutorial.git
   5d305d0..e78c86b  master -> master
スクリーンショット 2019-05-15 13.04.06.png

回到原本的自己,从远程存储库拉取。

由于第三方进行了更改,导致本地存储库和远程存储库产生了差异,因此从远程存储库拉取更新。

$ git pull [リモートリポジトリ名] [ブランチ名]

# 自分のディレクトリtutorialでpull
$ git pull origin2 master
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From https://github.com/ryoko-i8713/tutorial
 * branch            master     -> FETCH_HEAD
   5d305d0..e78c86b  master     -> origin2/master
Updating 5d305d0..e78c86b
Fast-forward
 sample.txt | 1 +
 1 file changed, 1 insertion(+)

# ログを確認してみる
$ git log
commit e78c86bfedbfaa96017e95f9da9aa43059c3d5d9 (HEAD -> master, origin2/master)
Author: ryoko-i8713 <49637061+ryoko-i8713@users.noreply.github.com>
Date:   Wed May 15 14:20:47 2019 +0900

    addの説明を追加

commit 5d305d011d87ee809e5ce58ad9042303a630a407
Author: ryoko-i8713 <49637061+ryoko-i8713@users.noreply.github.com>
Date:   Wed May 15 12:58:31 2019 +0900

    first commit no,2

commit 0b0d8ff22e1e5f70434a591fc7ed8603ba486408 (origin/master)
Author: ryoko-i8713 <49637061+ryoko-i8713@users.noreply.github.com>
Date:   Wed May 15 11:03:08 2019 +0900

    first commit

# ファイルを開いて確認
$ less sample.txt

如果一个第三方在我推送之后进行了推送,远程仓库会被更新。

在这种情况下,即使自己尝试推送,也会被拒绝=有冲突。
此时需要进行合并操作。
基本上,合并操作会自动进行集成,但如果同一部分有更改,则需要手动修复。


# tutorialディレクトリでpushする
$ git push origin2 master
To https://github.com/ryoko-i8713/tutorial.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://github.com/ryoko-i8713/tutorial.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

# tutorialディレクトリでpullしてみる
$ git pull origin2 master
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From https://github.com/ryoko-i8713/tutorial
 * branch            master     -> FETCH_HEAD
   e78c86b..9440d34  master     -> origin2/master
Auto-merging sample.txt
CONFLICT (content): Merge conflict in sample.txt
Automatic merge failed; fix conflicts and then commit the result.

# tutorialディレクトリのsample.txtを開き、<<<<<<<と>>>>>>>で囲まれた範囲が競合した箇所なので、修正する。
$ vim sample.txt

# インデックス登録〜commitする
$ git add sample.txt
$ git commit -m "マージ"

# ログで確認
$ git log --graph --oneline
*   6564673 (HEAD -> master) マージ
|\  
| * 9440d34 (origin2/master) pullの説明を追加
* | 21a1654 commitの説明を追加
|/  
* e78c86b addの説明を追加
* 5d305d0 first commit no,2
* 0b0d8ff (origin/master) first commit

# 再度pushしてみる
$ git push origin2 master

分支

从大元(master)中复制(创建分支),在创建的工作分支上进行功能添加和错误修复。


# ブランチの作成
$ git branch test branch

# ブランチ一覧(*がついているものが選択されているブランチ)
$ git branch
* master
  testbranch

# ブランチの切り替え
$ git checkout [切り替え先のブランチ名]
$ git checkout testbranch
Switched to branch 'testbranch'

# 選択しているブランチが変更された
$ git branch
  master
* testbranch

# ブランチの作成と切り替えを同時に
$ git checkout -b 

# sample.txtを編集する
$ vim sample.txt

サルでもわかるGitコマンド
add 変更をインデックスに登録する
commit インデックスの状態を記録する
pull リモートリポジトリの内容を取得する
test ブランチ テスト!

# インデックスに登録
$ git add sample.txt

# commit
$ git commit -m "ブランチテストを追加"
[testbranch 2ad2455] ブランチテストを追加
 1 file changed, 1 insertion(+)

# ブランチをtestbranchで指定してpush
$ git push origin2 testbranch
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 4 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 386 bytes | 386.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
remote: 
remote: Create a pull request for 'testbranch' on GitHub by visiting:
remote:      https://github.com/ryoko-i8713/tutorial/pull/new/testbranch
remote: 
To https://github.com/ryoko-i8713/tutorial.git
 * [new branch]      testbranch -> testbranch

确认远程存储库

スクリーンショット 2019-05-15 17.23.38.png

将创建的分支合并到主分支master


# ブランチを結合先のブランチに切り替える
$ git checkout master
Switched to branch 'master'

# 確認
$ git branch
* master
  testbranch

# ブランチを結合する
$ git merge [結合したいブランチ名]
$ git merge testbranch
Updating 6564673..2ad2455
Fast-forward
 sample.txt | 1 +
 1 file changed, 1 insertion(+)

# 結合したmasterブランチをリモートリポジトリにpushする
$ git push origin2 master
Total 0 (delta 0), reused 0 (delta 0)
To https://github.com/ryoko-i8713/tutorial.git
   6564673..2ad2455  master -> master

确认远程代码库

スクリーンショット 2019-05-15 17.39.23.png

删除分支

删除已完成集成的分支

$ git branch -d [ブランチ名]

# testbranchを削除する
$ git branch -d testbranch
Deleted branch testbranch (was 2ad2455).

# 確認
$ git branch
* master

有关分支机构的运营方式

我已经学会了Git的基本操作,但在何时创建分支的时机方面感到困惑,看到了这篇文章后真正理解了,因此我参考了它。

有关Git分支模型的信息,请参考https://qiita.com/okuderap/items/0b57830d2f56d1d51692

bannerAds