LoginSignup
0
0

More than 3 years have passed since last update.

Gitをやってみよう(リモートブランチを作るとマージしてみる)

Last updated at Posted at 2020-11-22

リモートブランチを作ろう

とりあえずフォルダとファイルを作る

image.png

GitHubのページに行き、右上の New repositoryをクリックする
image.png

ローカルのフォルダと同じ名前をいれる、Create repositoryをおす

image.png
gitを使えるようにする(空のgitリポジトリを作りましたよん😘)って書いてある

% git init
Initialized empty Git repository in /Users/numataseiji/Documents/practice/GitPractice/.git/

まずローカルにcommitして、リモートにpushする

% git add neko.html

% git commit -m"first commmit"
[master (root-commit) b026deb] first commmit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 neko.html
% git remote add origin https://github.com/SeijiNumata/GitPractice.git

% git push -u origin master
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 198 bytes | 198.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/SeijiNumata/GitPractice.git
 * [new branch]      master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

いい感じ

image.png


次、remoteのbranch作ってみっか
ローカルにブランチを作ってチェックアウト
する

% git branch
* master
% git checkout -b develop  #git branch develop してからgit checkout developするのと同じ効果
Switched to a new branch 'develop'
% git branch
* develop
  master

いっけえ(remoteに登録)

% git push -u origin develop 
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
remote: 
remote: Create a pull request for 'develop' on GitHub by visiting:
remote:      https://github.com/SeijiNumata/GitPractice/pull/new/develop
remote: 
To https://github.com/SeijiNumata/GitPractice.git
 * [new branch]      develop -> develop
Branch 'develop' set up to track remote branch 'develop' from 'origin'.

できたあ

image.png


マージをしてみよう

とりあえずmergeできるように準備しよう。
この状態を目指す
git pull と git pull –rebase の違いって?図を交えて説明します!より

一回マスターブランチに戻ろう

% git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.

ファイルを編集する

<body>
  <p>Hello World</p>
  <p>猫の形にした</p>
  <p>目をつけた</p>  
  <p>にゃああ</p>
</body>

commit しておく

% git add neko.html
% git commit -m"make cat and eyes"
[master 91fec54] make cat and eyes
 1 file changed, 3 insertions(+)

branchきる(ひげブランチ)

% git checkout -b makeHige 
Switched to a new branch 'makeHige'
 % git branch
  develop
* makeHige
  master

htmlを編集する

<body>
  <p>Hello World</p>
  <p>猫の形にした</p>
  <p>目をつけた</p>  
  <p>ひげつけます</p>
  <p>6本いいですか?</p>
  <p>にゃああ</p>
</body>

コミットしておく

% git add neko.html
% git commit -m"make beard"
[makeHige ae44f75] make beard
 1 file changed, 5 insertions(+)

またマスターにチェックアウトする。

% git checkout master
Switched to branch 'master'

ファイルを編集

<body>
  <p>Hello World</p>
  <p>猫の形にした</p>
  <p>目をつけた</p>  
  <p>にゃああ</p>
  <p>口を作った</p>
</body>
% git add neko.html
% git commit -m"make mouth"
[master 91f2876] make mouth
 1 file changed, 1 insertion(+)

ここまででやっと準備が完了した。
差分を確認してみると、口はあるけどひげがないマスターブランチと、ひげはあるけど口がないひげブランチがある。これを合体させて口もあるしひげもあるファイルを作る。

% git diff makehige
diff --git a/neko.html b/neko.html
index 06e3ee2..7d94e24 100644
--- a/neko.html
+++ b/neko.html
@@ -28,9 +28,8 @@
   <p>Hello World</p>
   <p>猫の形にした</p>
   <p>目をつけた</p>  
-  <p>ひげつけます</p>
-  <p>6本いいですか?</p>
   <p>にゃああ</p>
+  <p>口を作った</p>

今回はマスターブランチにひげブランチをマージする。

% git merge makehige
Auto-merging neko.html
CONFLICT (content): Merge conflict in neko.html
Automatic merge failed; fix conflicts and then commit the result.

コンフリクトした。手動で直してあげる。

<body>
  <p>Hello World</p>
  <p>猫の形にした</p>
  <p>目をつけた</p>  
<<<<<<< HEAD
  <p>にゃああ</p>
  <p>口を作った</p>
=======
  <p>ひげつけます</p>
  <p>6本いいですか?</p>
  <p>にゃああ</p>
>>>>>>> makehige
</body>

<body>
  <p>Hello World</p>
  <p>猫の形にした</p>
  <p>目をつけた</p>  
  <p>口を作った</p>
  <p>ひげつけます</p>
  <p>6本いいですか?</p>
  <p>にゃああ</p>
</body>

いいかんじにしてあげたら、またアドしてコミットする

% git add .                                 
% git commit -m"merge hige and fix conflict"
[master c60d1b9] merge hige and fix conflict
 1 file changed, 1 deletion(-)

一応マージできたので、リモートにプッシュする。
ひげブランチはもう不要なので削除する。

% git branch
  develop
  makeHige
* master
% git branch -d makehige
Deleted branch makehige (was ae44f75).
% git branch
  develop
* master
% git push origin master

できました。

image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0