自分のローカルのマシンにディレクトリを作り、
GitHubでTestProjectというレポジトリを作ると、複数のヒントが表示される。
(1) ... or create a new repository on the command line
echo "# TestProject" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin https://github.com/[ ... ]/TestProject.git
git push -u origin main
(2) ... or push an existing repository from the command line
git remote add origin https://github.com/[ ... ]/TestProject.git
git branch -M main
git push -u origin main
これらを順番にテストしてみる。
(1) ローカルでフォルダを作り、そこからGitHubに送る。
$ cd GitHub
$ mkdir TestProject
$ cd TestProject/
$ echo "# TestProject" >> README.md
$ git init
Initialized empty Git repository in /Users/kohei/Documents/GitHub/TestProject/.git/
$ git add README.md
$ git commit -m "first commit"
[master (root-commit) 5155190] first commit
Committer:
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:
git config --global user.name "Your Name"
git config --global user.email you@example.com
After doing this, you may fix the identity used for this commit with:
git commit --amend --reset-author
1 file changed, 1 insertion(+)
create mode 100644 README.md
$ git branch -M main
$ git remote add origin https://github.com/[ .... ]/TestProject.git
$ git push -u origin main
Counting objects: 3, done.
Writing objects: 100% (3/3), 227 bytes | 227.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/[ .... ]/TestProject.git
* [new branch] main -> main
Branch 'main' set up to track remote branch 'main' from 'origin'.
さらに、新しいファイルをpushする
$ ls -l
README.md
sample_code.py # これが新しいファイル
$ git add .
$ git commit -m "Add existing file"
$ git push origin main
既存のプロジェクウトのブランチを作る。
その上で、ローカルのディレクトリにおいて以下のようにする。
# ブランチを作成する。
$ git branch feature-branch1
# 現在自分がいるブランチがどれなのかを表示する
$ git branch
feature-branch1
* main
# 新しいブランチに移動する。
$ git checkout feature-branch1
Switched to branch 'feature-branch1'
# 新しいブランチに移動していることを確認する。
$ git branch
* feature-branch1
main
# 何か新しいファイルを作ってみる。
$ echo "# Add a file for new branch." >> test.txt
$ ls -l
README.md
sample_code.py
test.txt # 新しいファイル
$ git add --all
$ git commit -m "push to a new branch named feature-branch1."
[feature-branch1 3580623] push to a new branch named feature-branch1.
1 file changed, 1 insertion(+)
create mode 100644 test.txt
$ git push origin feature-branch1
Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 372 bytes | 372.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/[ ... ]/TestProject.git
41cd612..3580623 feature-branch1 -> feature-branch1
GiyHubのレポジトリのページで、"Find or create a branch..."のところに新しいブランチ名 feature-branch1
を入れてEnterを押すことで、新しいブランチを作ることもできる。