1
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 5 years have passed since last update.

Gitコマンドを恐れずに使えるようになる!

Posted at

TortoiseGitから操作することが多く、コマンドを忘れそうになったので忘れないようにまとめる! ( ..)φメモメモ

※コマンドのオプション等も随時更新予定:arrows_counterclockwise:

===前提===

  • Githubも使っている
  • ターミナルはGit Bash

ファイルを編集する前の準備

git clone

リモートリポジトリを丸ごと複製して、自分の手元(ローカル)へ置く。

$ git clone [URL]
Cloning into 'リポジトリ名'...
remote: Enumerating objects: 23, done.
remote: Counting objects: 100% (23/23), done.
remote: Compressing objects: 100% (17/17), done.
remote: Total 23 (delta 2), reused 6 (delta 0), pack-reused 0
Receiving objects: 100% (23/23), 5.91 KiB | 1007.00 KiB/s, done.
Resolving deltas: 100% (2/2), done.

[URL]はGIthubのClone or downloadをクリックすると表示される。
git_clone.jpg

git pull

リモートリポジトリからローカルリポジトリへダウンロードすること。
リモートリポジトリの方が変更履歴が新しいはずなので、その内容をローカルリポジトリに取り込む。

$ git pull
※以下は表示例
remote: Enumerating objects: 1, done.
remote: Counting objects: 100% (1/1), done.
remote: Total 1 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (1/1), 652 bytes | 59.00 KiB/s, done.
From https://github.com/ユーザー名/リポジトリ名
   de13370..fc71cca  master     -> origin/master
Updating de13370..fc71cca
Fast-forward
 test/index.html         |  23 ++
既に最新の状態である場合
$ git pull
Already up to date.

git status

ファイルの状態を確認する

ファイルの変更が無い場合
$ git status
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean
  • On branch XXX
    • 「現在はXXXというブランチにいる」の意

git branch

ブランチ名を一覧で表示する

$ git branch
* master

:asterisk:マークがついているのが現在いるブランチ

git branch ブランチ名

新しいブランチを作成する

$ git branch test

git checkout ブランチ名

ブランチを切り替える

$ git checkout test
Switched to branch 'test'

切り替わっているか確認する

$ git branch
  master
* test

:asterisk:マークがtestに付いているので、無事に切り替わった

ファイルに変更を加えたら

git status

$ git status
On branch test
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:   css/style.css
        modified:   index.html

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        css/reset.css

no changes added to commit (use "git add" and/or "git commit -a")
  • Changes not staged for commit
    • ステージング環境に追加されていないファイルを表示
  • Untracked files
    • 新しく追加したファイルで、Gitでまだ管理されていないファイルを表示

git add

編集・追加したファイルをステージング環境へ追加する

$ git add .

git add .で、カレントディレクトリにある全てのファイルを追加

git status

$ git status
On branch test
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   css/reset.css
        modified:   css/style.css
        modified:   index.html

  • Changes to be committed
    • これからコミットするファイルを表示

git commit

このコマンドを実行するとvimに切り替わる

ここはvim
コミットタイトル
[空行]
コミットコメント
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# On branch test
# Changes to be committed:
#       new file:   css/reset.css
#       modified:   css/style.css
#       modified:   index.html
#

編集が終わったら以下のように表示される

$ git commit
[test 0b6049e] コミットタイトル
 3 files changed, 172 insertions(+), 47 deletions(-)
 create mode 100644 css/reset.css
 rewrite css/style.css (92%)

git status

$ git status
On branch test
nothing to commit, working tree clean

無事にコミットできた(コミットするファイルが無くなった)

いざリモートリポジトリへプッシュ!

git push

git pushだけだとエラーになってしまうので、

$ git push
fatal: The current branch test has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin test

git push origin ブランチ名とする。

$ git push origin test
Enumerating objects: 10, done.
Counting objects: 100% (10/10), done.
Delta compression using up to 8 threads
Compressing objects: 100% (6/6), done.
Writing objects: 100% (6/6), 1.61 KiB | 826.00 KiB/s, done.
Total 6 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
remote:
remote: Create a pull request for 'test' on GitHub by visiting:
remote:      https://github.com/ユーザー名/リポジトリ名/pull/new/test
remote:
To https://github.com/ユーザー名/リポジトリ名.git
 * [new branch]      test -> test

成功するとGithubに表示される(黄色の所)
git_push.jpg

あとはGithubのCompare & pull requestから操作して作業を完了させる。

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