0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

プロジェクト(リポジトリ)をGitHubに反映したい

Posted at

前置き

  • ここでは特定の場合を除いて、「リポジトリ」を「プロジェクト」として書いています
  • この記事はubuntuターミナル上で動かすことを想定しています
    ubuntuの入手はこちらから

この記事の対象

この記事は、次の人を中心に想定して書いています。

  • GitHubを初めて使った人
  • GitHubは触っているが、既存のプロジェクトを新規でpushしたことがない人

上級者の方や使い慣れている方へ

 自身の知識のアウトプットを兼ねて書いた記事です。もし、間違いや疑問があればコメントのほうから教えていただけるとありがたいです。適宜対応します。

書かないこと

  • GitHubの登録方法

プロジェクトを新規作成

新しい Git リポジトリを作成

git init

※プロジェクトがもうある場合は実行しなくても大丈夫です。

変更をステージング

ステージングとは

ステージングとは、修正を確定することだと認識しておいて間違いはないと思います。ペーパーテストでいえば、間違えている問題を解きなおして、箱に入れるイメージです。1
GitHubからpullしていない人はこちら

ステージングの方法

ひとことでステージングと言っても、方法はいくつかあります。

  • すべてのファイルをステージング
git add .
  • 特定のファイルをステージング
git add [ステージングしたいファイル名]

ファイル名が分からないとき

ファイル名が分からないときは、次のコマンドで状態確認をすることができます。

git status

実行例と結果例

user@name:~/directory$ git status # ここが実行例。下はすべて結果。
On branch main
Your branch is ahead of 'origin/main' by 1 commit.
  (use "git push" to publish your local commits)

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   index.html

no changes added to commit (use "git add" and/or "git commit -a")

解説

On branch main
この行は現在のブランチ名が書いてあります。(現在はmainブランチ)

--
Your branch is ahead of 'origin/main' by 1 commit.
(use "git push" to publish your local commits)
ここは、現在コミットされているファイルの数が書いてあります。1commit と書いてある場合は、まだGitHub に反映できていないので、[ ここ ]の手順でpushしましょう。

--

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   index.html

changes no staged for commit
コミット前のステージングがされていないことを示しています。

(use "git add <file>..." to update what will be committed)
変更をステージングするときは ここ のコマンドを実行します。

(use "git restore <file>..." to discard changes in working directory)
作業ディレクトリ内の変更を取り消す場合は次のコマンドを実行します。

git restore [変更を取り消したいファイル名]

コミットをする

コミットとは

コミットとは、修正したデータを入れた箱を、閉じる段階です。
テストを第三者の機関に送る、出荷直前の段階です。

コミットの方法

次のコマンドを実行することでコミットをすることができます。

git commit -m "feature/change-details"

change-detailsの部分は変更内容を書いてください。

初めての場合

初めて(作りたて)の場合、いくつかの変更前に上のコードではなく、次のコードを実行しておきましょう。

git commit -m "Initial commit"

ここからは初めての人のみの作業

主軸ブランチの変更

 ブランチについては、こちら を参考にしてください。
 ブランチは通常、「master」がはじめの主軸のブランチになっています。今後の利便性を考えて、主軸ブランチを「main」に変更しておきましょう。次のコードを実行してください。

git branch -M main

-M オプション
 このオプションは-m と異なり、既にmain ブランチができていても上書きするオプションとなっています。初期の頃はなるべく使用しないように注意してください。

リモートブランチとの連携

git remote add origin https://github.com/your-username/your-repo.git

変更の適用

 リモートリポジトリ(GitHubなど)にも変更を適用したい場合、git push -u origin main を実行する必要があります。

git push -u origin main

 以上の作業により、GitHubに反映させることができます。これからはどんどん作業をして使い慣れていきましょう✨

  1. 後輩のためのGit講座(応用編) written by @ko-tarou

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?