Table of Contents
本日の問題: 誤ったブランチにcommitしてしまっていた
問題設定
git repositoryを新規に立ち上げけたとします.当初はREADME.mdなどを編集したfirst commitの後,新たにtestブランチを切って
作業を実施する予定でしたが,実際にはmainブランチで引き続き操作をしてしまっていました.
このとき,以下を実行するためのgit commandを答えよ:
first commitからの一連のcommit-idの列を保持しつつ,当初意図した形ブランチの形に持っていく- つまり,
first commitのところまでresetして,同じ作業を新しいbranchで再現するのはダメ - working branchを
testブランチへcheckoutする
▶ 意図していた操作
gitGraph
commit id: "813faa2"
branch test
commit id: "28a8bf7"
commit id: "466fbe7"
commit id: "30bf56a"
commit id: "6159140"
▶ 実際の操作
gitGraph
commit id: "813faa2"
commit id: "28a8bf7"
commit id: "466fbe7"
commit id: "30bf56a"
commit id: "6159140"
Solution: git resetを用いる
方針
- 現在のHEADの状態をベースに
testブランチを新規作成する mainブランチをfirst commit直後の状態にな戻すtestブランチに切り替える
▶ Commands
1
2
3
4
5
6
7
8
## 現在のHEADの状態をベースに`test`ブランチを新規作成
% git branch test
## mainブランチをfirst commit直後の状態に戻す
% git reset 813faa2 --hard
## testブランチに切り替える
% git switch test
▶ 解説
git branch <branch-name>で現在のHEADをベースにswitchすることなく新規ブランチが作成できますgit reset --hardとしているのはデフォルトではgit reset --softとなってしまい,currentブランチにインデックス,ファイルの変更は残ってしまうからです- currentブランチに修正ファイルが残ってしまうと,
git stashorgit cleanの実施が必要となります
警告 !
- もし,commit-id
6159140の時点ですでにremote repositoryへpushしてしまっていたならば,他の開発者の迷惑になるので上記Solutionは実行しないほうが良いです
References
統計
Python
math
Linux
Ubuntu 20.04 LTS
Shell
English
git
方法論
Ubuntu 22.04 LTS
統計検定
競技プログラミング
フーリエ解析
前処理
SQL
coding
コミュニケーション
Network
ssh
将棋
Data visualization
Docker
Econometrics
VSCode
statistical inference
GitHub Pages
apt
development
システム管理
Coffee
cloud
数値計算
素数
Book
Font
Metrics
Poetry
Ubuntu 24.04 LTS
architecture
aws
shell
systemctl
テンプレート
データ構造
ポワソン分布
会計分析
文字コード
環境構築
論文
App
Bayesian
Dynamic Programming
Keyboard
Processing
R
Steam
filesystem
quarto
regex
(注意:GitHub Accountが必要となります)