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?

More than 3 years have passed since last update.

[1O1]ローカルで作成したファイルをgitでpushする流れ

Last updated at Posted at 2021-02-07

目的

ローカルで作成したプログラムファイルなどをgithubのリモートリポジトリにpushする。
なおシェルはgit bashを利用する。

git init

ローカルリポジトリを初期化する。
ソースファイルの置かれたフォルダをまるごとgitで管理するため、cdで目的のディレクトリまで移動する。
デフォルトだとbash上のカレントワークディレクトリ内に作成され、隠しファイルとして.gitが作成される。

$ git init 

git add

インデックスにファイルを登録(ステージング)する。
git add .でディレクトリ内のすべてのファイルを指定する。git add 'ファイル名'で特定のファイルのみ指定する。

$ git add .

git commit

ローカルリポジトリにaddしたファイルをコミットする。引数にコミットメッセージを入力しないと完了できない。

$ git commit -m "first commit"

このとき

Author identity unknown
please tell me who you are

というエラーが出たらgitに個人情報が登録されていない状態なので以下のコマンドで名前とアドレスをconfigに登録し、再度commitをする。

$ git config --global user.name [hogehoge]
$ git config --global user.email [hoge@hoge.com]

リモートリポジトリの登録

まずgithubのマイページにて新規リポジトリを作成する。プロジェクト名(リポジトリ名)を入力し、初期化(init)の項目はいずれもチェックしないようにする。
次に以下のコマンドでリモートリポジトリを登録する。URLを含むリポジトリ名をoriginとする。
[注意]リポジトリのURLはSSHではなくHTTPSの方を選択する。(sshの場合リモート接続用のセキュリティを設定する必要があるため)

$ git remote add origin https://github.com/[your-username]/[project-name].git

git push

masterブランチをリモートリポジトリ(origin)にpushする。
初回の場合(?)githubへのサインインが要求される。

☆なお、originとはリモートリポジトリの名前であり、github側のリポジトリを指定していることになる。masterはブランチの名前であり、デフォルトではmasterだがブランチを切った場合は当然違う名前を入力することになる。

$ git push origin master

変更ファイル、コミットのログの確認

# コミットのログ(メッセージや履歴など)を確認できる
$git log

# 変更ファイルや新規ファイルが赤文字で出力される
$git status

2回目以降にadd,commit,pushするとき

commitの-aコマンドではコミットしたことのあるファイルに変更があった場合そのすべてをインデックスにステージングする(要はcommitとaddを同時におこなう)。
その後は初回同様にgit pushをおこなう。

$git commit -a -m "second commit"

リモートリポジトリのファイルを削除するとき

$git rm --cached [filename]
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?