2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

VScode×Git×GitHub初期設定

Posted at

備忘録

毎回調べていると時間がかかって仕方がないので
コマンドなどをシンプルに書きました。

前提

・Gitインストール&基本設定が完了済
・VScodeインストール済

Gitローカルリポジトリ作成

ターミナル.ローカルリポジトリの初期化
git init

→出力
Initialized empty Git repository...

ローカルリポジトリにステージング

「index.html」などのファイルを作成。
・ソース管理>+ボタンを押す。
→メッセージ入れてコミット

GitHubと連携

・GitHubリモートリポジトリを作成

リモートリポジトリ紐づけ
VScode.ターミナル
git remote add origin https://github.com/ユーザー名/リポジトリ名.git
VScode.ターミナル
git remote -v

→出力
origin https://github.com/~~.git (fetch)
origin https://github.com/~~.git (push)

VScode.ターミナル
git branch -M main
VScode.ターミナル
git push -u origin main

GitHubの反映確認

・リポジトリのコードを閲覧する

その他

出現したエラーと対処
(人によって異なる部分は全て~~で記載)

VScode.ターミナル.push失敗
~~ % git push origin main
To https:~.git
! [rejected]        main -> main (non-fast-forward)
error: failed to push some refs to '~~.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

↓その後pullを試みた

VScode.ターミナル.pull失敗
~~ % git pull
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.

    git pull <remote> <branch>

If you wish to set tracking information for this branch you can do so with:

    git branch --set-upstream-to=origin/<branch> main

↓fetchしてmergeすることに

VScode.ターミナル.mergeが失敗
~~ % git fetch
~~ % git merge origin/main  
fatal: refusing to merge unrelated histories

↓対処

VScode.ターミナル.オプションを使用して実行
~~ % git merge --allow-unrelated-histories --no-edit origin/main
Merge made by the 'ort' strategy.
 README.md | 1 +
 1 file changed, 1 insertion(+)
 create mode ~~ README.md

push成功

VScode.ターミナル
~~ % git push origin main
Enumerating objects: ~, done.
Counting objects: 100% (~/~), done.
Delta compression using up to ~ threads
Compressing objects: 100% (~/~), done.
Writing objects: 100% (~/~), ~ bytes | ~ KiB/s, done.
Total ~ (delta ~), reused ~ (delta ~), pack-reused ~
remote: Resolving deltas: 100% (~/~), done.
To https://github.com~~.git
   ~..~  main -> main

参考文献

2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?