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

Git操作 実務で生じた事象と対応のメモ

Posted at

developからブランチ切って作業すべきところ、mainからブランチ切って作業してしまった場合

図で説明すると以下の通り(前提:今のブランチ名を feature/foo と仮定)

# 現状(間違って master から切って作業中)
master
  A---B
         \
          C---D   ← feature/foo(ここで作業中)

# やりたい形(develop をベースにしたかった)
develop
  E---F---G
           \
            C'--D' ← feature/foo(履歴を載せ替えたい)

対応は以下の手順

# 1. developブランチを最新に更新
git checkout develop
git pull origin develop

# 2. 間違ってmasterから切って作業していたブランチに戻る
git checkout feature/foo

# 3. 現在の変更をdevelopブランチの上に載せ替える
git rebase develop

以下のコマンドによって、feature/foo が develop の延長線上にあるかを確認できます。

git log --oneline --graph --all

ブランチ切って作業している時に、突発的な別タスクが発生した場合

# 1. 今の作業内容を退避(未追跡ファイルも含めて)
git stash -u
※ -u オプションで git add していない 新規ファイルも一緒に退避される

# 2. develop にチェックアウトして最新化
git checkout develop
git pull origin develop

# 3. 突発タスク用の新しいブランチを切る
git checkout -b new-task

# 4. そのタスクの作業を行い、コミット・プッシュ
git add .
git commit -m "Fix urgent issue"
git push origin new-task

# 5. 元の作業に戻る
git checkout feature/original-task
git stash pop

git hooksを用いたpre-commit / pre-pushをスキップしたい場合

#  pre-commit をスキップする(例: lint チェック等を飛ばしたい場合)
git commit -m "commit message" --no-verify

# pre-push をスキップする(例: テストやCIチェックを飛ばしたい場合)
git push origin HEAD --no-verify
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?