LoginSignup
1
2

GitHubでソースファイルを管理する

Last updated at Posted at 2020-10-02

目的

プログラミング作成時に自分が以前に作成したものを参考に作ることが多い。GitHubで管理すれば、ネットさえあればどこからでも確認できることを想定して利用する。

準備

プロジェクト作成時の手順

  • GitHubでリポジトリを作成する
git init
git add .(a)
git commit -m "コメント"(b)
git branch -M main …(c)
git remote add origin [URL] …(d)
git push -u origin main…(e)

##補足
※(a)git addのオプションはこちらgit add -u と git add -A と git add . の違い

(a)で下記が発生する場合は

warning: LF will be replaced by CRLF in <ファイル名>

下記を実行する

 git config --global core.autocrlf false

※(b)commit時に下記発生

*** Please tell me who you are.

Run

  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: unable to auto-detect email address (got 'xxxxxxxxxxxxxxxx@xxxxxxxxxxxxx.(none)')

gitで起きたエラーの解決法を参照してメールアドレスとユーザー名を設定し解決

※(c)githubのデフォルトはmainブランチ,gitのデフォルトはmasterブランチの為gitのデフォルトブランチを変更しておく。"git config --global init.defaultBranch main"も試したが失敗
【参照】Gitのデフォルト・ブランチ名を変更する方法

※(d)"fatal:remote origin already exists."となった場合は

git remote rm origin

リモートリポジトリのURLはgithubで確認できる。
これでもう怖くない、Git/GitHubにおけるリモートリポジトリの作成、確認、変更、更新時の基本5コマンド (3/3)の『リモートリポジトリのURLを確認する』を参照

※(e)パスワードが通らないので下記を参考にGitをバージョンアップ
Git PushしたらLogon failed, use ctrl+c to cancel basic credential prompt.が出た時の対処法

 再度(e)を実行するとrejectされる。下記を参考に
(git pushがrejectされた時の対処法)[https://qiita.com/cedric-ryo/items/4a6ed57549a3008fcb6a]

ソースの更新ごとに実施

git add -A(f)
git commit -m "コメント"
git push  

補足

※(f)git addのオプションはこちらgit add -u と git add -A と git add . の違い

#リモートリポジトリからローカルリポジトリを作成

git clone [URL]

リモートリポジトリから最新版を取得

git pull

#コミットはせずに変更を退避したいとき

git stash

※git pullで"Your local changes to the following files would be overwritten by merge"が発生。ローカルの変更を反映する必要がない場合は上記でOK

git pushがrejectされたとき

git pull origin main

git pushがrejectされたときの対応方法

追跡済のファイルを対象からはずす

git rm --cached <FILE_NAME>

【gitをソフト開発で使いこなそう!:最終回】.gitignoreの書き方を徹底解説!

.gitignoreで追跡対象からはずす

【gitをソフト開発で使いこなそう!:最終回】.gitignoreの書き方を徹底解説!

git addを取り消す

※一度もcommitしてない場合は別のやり方なので注意
下記でadd対象ファイルを確認する

git status

下記のように表示される。この場合、対象はjs/AutofreeGiftPost.js

Your branch is ahead of 'origin/main' by 1 commit.
  (use "git push" to publish your local commits)

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   js/AutofreeGiftPost.js

コマンド

下記2通りあり。今回はresetを使って取り消しを行う

git restore --staged [ファイル名]
git reset HEAD [ファイル名]

取り消し具体例

下記で取り消しできる

git reset HEAD js/AutofreeGiftPost.js

git statusで確認すると以下の様に

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:   js/AutofreeGiftPost.js

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

参考

2022-09-02
【Git入門】git addを取り消したい!そんなお悩みの解決法をパターン別にご紹介!

参考

【初心者向け】Gitのインストール方法をわかり易く解説(画面付き)
【0から始めるGit】Gitをインストールしてみた...インストールオプションの設定がこちらが詳しいです
今さら聞けない!GitHubの使い方【超初心者向け】
初めてGitを使う人向けに情報をまとめてみた(基本編) ※とても、わかりやすく、かかれています。とくに『2-2. Gitの構造』

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