7
2

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.

脱SourceTree👋🏻Gitコマンドを知る⚙️

Last updated at Posted at 2023-09-04

◉はじめに

現在私はSourceTreeを使用してコミットやプッシュしたりしているのですが、まだまだ未熟ながらもそのうち超エンジニアっぽくスパパパーンッとTerminalにGitコマンドを打ち込んで使いこなせたらいいな〜と思ってます🤤
ということで、普段から使えるGitコマンドを教えていただいたので記録しました📝

◉Gitコマンド一覧(よく使いそう厳選)◉

Commitしたのを希望のところに戻す

git reset HEAD^

コミットしたのを希望のところに戻せます。
一番右の^の数で戻る個数が決まります!

例:^^2つだと2つ前のコミットに戻せる

使用例

  • 最新のコミットを取り消す
git reset HEAD^
  • 最新のコミットを取り消し、変更を修正して再コミット
git reset HEAD^
# 変更を修正
git commit -c ORIG_HEAD

変更したファイルをステージングに追加する

git add -u

新しいファイルをステージングエリアに追加せず、既存の変更をステージングしたい場合に便利だそうです。

使用例

  • 変更されたファイルをステージングエリアに追加
git add -u
  • ディレクトリ内の変更をステージング
git add -u path/to/directory

現在の変更の状態を確認する

git status

どのファイルが変更されたかや、commitされていない変更がないか等状態を確認できます。

使用例

git status
  • コミット前の確認
  • 変更の差分を表示
  • ステージングの状態を確認

Commitする

git commit -m “コメント”

Commitする際に変更した箇所などのコメントを記載してCommitできます。

使用例

  • 変更をコミット
git commit -m "⚪︎⚪︎⚪︎を修正"
  • 複数のファイルを一度にコミット
git add file1.txt file2.txt
git commit -m "ファイル1とファイル2の変更をコミット"

プッシュする

git push

プッシュできます。

使用例

  • リモートリポジトリへのプッシュ
    ▼mainブランチをプッシュ
git push origin main
  • 強制プッシュ
    →誤ってコミットした変更を修正する場合などに使用される
    ▼originというリモートリポジトリ
git push -f origin main

💡他にどんなのがあるのかchatGPTに聞いてみました

新しいGitリポジトリを初期化する

git init

使用例

  • 新しいGitリポジトリの作成
cd /path/to/your/project
git init
  • 既存のプロジェクトをGitで管理を開始
cd /path/to/existing/project
git init

リモートリポジトリからローカルにコピーを作成する

git clone <リモートリポジトリのURL>

使用例

  • GitHub上のプロジェクトをクローンする
git clone https://github.com/ユーザー名/リポジトリ名.git
  • 特定のブランチをクローン
git clone -b develop https://github.com/ユーザー名/リポジトリ名.git
  • クローン先のディレクトリを指定する
git clone https://github.com/ユーザー名/リポジトリ名.git my-project

ブランチの一覧を表示する

git branch

使用例

  • ブランチの一覧を表示
git branch
  • 新しいブランチを作成
git branch new-feature
  • ブランチ名の変更
git branch -m old-branch-name new-branch-name)

ブランチを切り替える

git checkout <ブランチ名>

使用例

  • 既存ブランチに切り替える
git checkout 既存ブランチ名
  • 新しいブランチを作成して切り替える
git checkout -b 作成するブランチ名

ブランチを統合する

git merge <ブランチ名>

使用例

  • ブランチを現在のブランチにマージ
    ▼feature-branchブランチをmainブランチにマージ
git checkout main
git merge feature-branch
  • 過去のコミットをマージ
    ▼特定のコミットをmainブランチにマージ
git checkout main
git merge 3a3b12c

コミット履歴を表示する

git log

使用例

  • コミットの差分を表示
git log -p
7
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
7
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?