<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Git運用 &#8211; CFXLOG</title>
	<atom:link href="https://cfxlog.com/tag/git-workflow/feed/index.xml" rel="self" type="application/rss+xml" />
	<link>https://cfxlog.com/</link>
	<description></description>
	<lastBuildDate>Wed, 18 Nov 2026 21:49:33 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1.4</generator>

<image>
	<url>https://cfxlog.com/wp-content/uploads/2026/07/cfxlog-favicon-150x150.png</url>
	<title>Git運用 &#8211; CFXLOG</title>
	<link>https://cfxlog.com/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>git resetの使い分け完全ガイド｜&#8211;soft・&#8211;mixed・&#8211;hardとrevertの違い</title>
		<link>https://cfxlog.com/git-reset-revert-guide/</link>
		
		<dc:creator><![CDATA[CFXLOG]]></dc:creator>
		<pubDate>Mon, 03 Aug 2026 15:00:00 +0000</pubDate>
				<category><![CDATA[Git]]></category>
		<category><![CDATA[Git運用]]></category>
		<guid isPermaLink="false">https://cfxlog.com/git-reset-revert-guide/</guid>

					<description><![CDATA[<p><img src="https://cfxlog.com/wp-content/uploads/2026/08/git-reset-revert-guide-1024x538.jpg" class="webfeedsFeaturedVisual" /></p>コミットを取り消したいだけなのに、コマンドが複数あって迷う場面はよくあります。 ところが、git resetには--soft・--mixed・--hardという3つのオプションがあり、git revertという別のコマン [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><img src="https://cfxlog.com/wp-content/uploads/2026/08/git-reset-revert-guide-1024x538.jpg" class="webfeedsFeaturedVisual" /></p>
<p class="wp-block-paragraph">コミットを取り消したいだけなのに、コマンドが複数あって迷う場面はよくあります。</p>



<p class="wp-block-paragraph">ところが、<code>git reset</code>には<code>--soft</code>・<code>--mixed</code>・<code>--hard</code>という3つのオプションがあり、<code>git revert</code>という別のコマンドもあります。</p>



<p class="wp-block-paragraph">そこで、この記事では<strong>「取り消したい範囲」と「共有済みかどうか」という2つの判断軸</strong>で、それぞれの使い分けを整理しました。</p>


<div class="swell-block-postLink">			<div class="p-blogCard -internal" data-type="type1" data-onclick="clickLink">
				<div class="p-blogCard__inner">
					<span class="p-blogCard__caption">あわせて読みたい</span>
					<div class="p-blogCard__thumb c-postThumb"><figure class="c-postThumb__figure"><img decoding="async" src="https://cfxlog.com/wp-content/uploads/2026/03/git-basics-cui-300x158.jpg" alt="" class="c-postThumb__img u-obf-cover" width="320" height="180"></figure></div>					<div class="p-blogCard__body">
						<a class="p-blogCard__title" href="https://cfxlog.com/git-basics-cui/">【基本】CUI を使った Git の基本操作</a>
						<span class="p-blogCard__excerpt">独学でプログラミングを始めると、Git はとても身につけにくい技術です。 Git はとても便利な反面、処理のフローが複雑で初学者にとって複雑怪奇。 さらに個人開発では &#8230;</span>					</div>
				</div>
			</div>
		</div>


<p class="wp-block-paragraph">あわせて、まだコミットしていない変更を戻す <code>git restore</code>、<code>git add</code> の取り消し、直前のコミットを直す <code>git commit --amend</code>、消しすぎたときの <code>git reflog</code> も、実際に動かした出力つきでまとめました。</p>



<div class="swell-block-capbox cap_box is-style-onborder_ttl"><div class="cap_box_ttl"><span>この記事で動かした環境（2026年11月18日）</span></div><div class="cap_box_content">
<ul class="wp-block-list">
<li>macOS 27.2</li>

<li>git 2.54.0（Apple Git-157）</li>

<li>練習用に作った一時リポジトリ（ファイルは <code>app.txt</code> と <code>README.md</code> だけ）。push は手元に作った練習用のリモートで試しています</li>
</ul>
</div></div>



<p class="wp-block-paragraph">コミットのハッシュ（<code>a855476</code> など）は環境ごとに変わります。手元で試すときは、自分の <code>git log</code> に出たハッシュに読み替えてください。</p>



<h2 class="wp-block-heading">状況別の早見表</h2>



<h3 class="wp-block-heading">取り消したいもの別のコマンド</h3>



<figure class="wp-block-table"><table><thead><tr><th>取り消したいこと</th><th>使うコマンド</th><th>変更の内容は</th></tr></thead><tbody><tr><td>ファイルの書き換え（まだ add していない）</td><td><code>git restore &lt;ファイル&gt;</code></td><td>消える</td></tr><tr><td>新しく作ったファイル（まだ add していない）</td><td><code>git clean -n</code> で確かめてから <code>git clean -f &lt;ファイル&gt;</code></td><td>消える</td></tr><tr><td><code>git add</code></td><td><code>git restore --staged &lt;ファイル&gt;</code></td><td>作業ツリーに残る</td></tr><tr><td>直前のコミットのメッセージ・入れ忘れ</td><td><code>git commit --amend</code></td><td>コミットが作り直される</td></tr><tr><td>コミット（push 前）・変更はステージに残す</td><td><code>git reset --soft HEAD~1</code></td><td>ステージに残る</td></tr><tr><td>コミット（push 前）・変更は作業ツリーに残す</td><td><code>git reset HEAD~1</code></td><td>作業ツリーに残る</td></tr><tr><td>コミット（push 前）・変更も消す</td><td><code>git reset --hard HEAD~1</code></td><td>消える</td></tr><tr><td>push 済みのコミット</td><td><code>git revert &lt;コミット&gt;</code></td><td>打ち消すコミットができる</td></tr><tr><td>reset しすぎた</td><td><code>git reflog</code> → <code>git reset --hard HEAD@{n}</code></td><td>コミットした内容なら戻る</td></tr><tr><td>作業をいったん退避したい</td><td><code>git stash</code> / <code>git stash pop</code></td><td>退避して、あとで戻せる</td></tr></tbody></table></figure>



<p class="wp-block-paragraph">迷ったときは、まず「<strong>push したかどうか</strong>」で分けます。push する前なら <code>reset</code> や <code>--amend</code> で履歴を書き換えてかまいません。push したあとは、履歴を書き換えずに打ち消す <code>revert</code> を使います。</p>



<h3 class="wp-block-heading">Git の3つの場所を押さえる</h3>



<p class="wp-block-paragraph">取り消しのコマンドを使い分けるには、Git が変更をどこに置いているかを知っておくと分かりやすくなります。</p>



<figure class="wp-block-table"><table><thead><tr><th>場所</th><th>中身</th><th>入れるコマンド</th></tr></thead><tbody><tr><td>作業ツリー</td><td>いま編集しているファイル</td><td>（ファイルを保存する）</td></tr><tr><td>ステージ（インデックス）</td><td>次のコミットに入れる予定の変更</td><td><code>git add</code></td></tr><tr><td>コミット（履歴）</td><td>記録した変更</td><td><code>git commit</code></td></tr></tbody></table></figure>



<p class="wp-block-paragraph"><code>git status --short</code> の表示では、左の列がステージ、右の列が作業ツリーの状態です。<code>M&nbsp;&nbsp;app.txt</code>（M が左）ならステージ済み、<code>&nbsp;M app.txt</code>（M が右）なら作業ツリーだけで変更されている、という意味です。この記事の出力でもこの見方を使います。</p>



<h2 class="wp-block-heading">resetとrevertの違い</h2>



<p class="wp-block-paragraph">どちらも「なかったことにする」コマンドですが、<strong>履歴を消すか、履歴を残すか</strong>が決定的に違います。</p>



<figure class="wp-block-table"><table><thead><tr><th>コマンド</th><th>オプション</th><th>使うタイミングの目安</th><th>何をするか</th></tr></thead><tbody><tr><td>git reset</td><td>&#8211;soft</td><td>直前のコミットメッセージや粒度をやり直したいとき</td><td>コミットだけ取り消す（変更はステージに残る）</td></tr><tr><td>git reset</td><td>&#8211;mixed（デフォルト）</td><td>ステージし直してからコミットしたいとき</td><td>コミットとステージを取り消す（変更はワーキングツリーに残る）</td></tr><tr><td>git reset</td><td>&#8211;hard</td><td>その変更ごと丸ごと不要になったとき</td><td>コミット・ステージ・変更内容すべてを消す</td></tr><tr><td>git revert</td><td>（オプションなし）</td><td>共有済みのブランチで、履歴を残したまま取り消したいとき</td><td>取り消し内容を新しいコミットとして追加する</td></tr></tbody></table></figure>



<p class="wp-block-paragraph"><strong>resetは履歴そのものを書き換え、revertは打ち消しの記録を積み増します。</strong>この一点さえ押さえれば、どちらを使うべきかはほぼ自動的に決まります。</p>



<h2 class="wp-block-heading">git resetの3つのオプション</h2>



<p class="wp-block-paragraph">ここでは、練習用のリポジトリで「4行目を追加」というコミットを取り消して、それぞれの動きを確かめます。<code>--soft</code>・<code>--mixed</code>・<code>--hard</code> のそれぞれを試す前に、4行目のコミットを作り直しています。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Text</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="コピー" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>3e94b82 4行目を追加
a855476 3行目を追加
2c02da1 2行目を追加
aaaed61 最初のコミット</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #D4D4D4">3e94b82 4行目を追加</span></span>
<span class="line"><span style="color: #D4D4D4">a855476 3行目を追加</span></span>
<span class="line"><span style="color: #D4D4D4">2c02da1 2行目を追加</span></span>
<span class="line"><span style="color: #D4D4D4">aaaed61 最初のコミット</span></span></code></pre></div>



<h3 class="wp-block-heading">&#8211;soft：コミットだけ取り消す</h3>



<p class="wp-block-paragraph">直前のコミットを取り消しつつ、変更内容はステージされた状態のまま残します。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="コピー" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git reset --soft HEAD~1</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color:#1E1E1E;color:#D4D4D4" tabindex="0"><code><span class="line"><span style="color:#DCDCAA">git</span><span style="color:#CE9178"> reset</span><span style="color:#569CD6"> --soft</span><span style="color:#CE9178"> HEAD~1</span></span></code></pre></div>



<p class="wp-block-paragraph">コミットメッセージを書き直したい、あるいは複数のコミットを1つにまとめ直したいときに使います。</p>



<p class="wp-block-paragraph">練習用のリポジトリで試すと、次のようになりました。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="コピー" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git reset --soft HEAD~1
git log --oneline -1
git status --short</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">reset</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">--soft</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">HEAD~1</span></span>
<span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">log</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">--oneline</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">-1</span></span>
<span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">status</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">--short</span></span></code></pre></div>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Text</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="コピー" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>a855476 3行目を追加
M  app.txt</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #D4D4D4">a855476 3行目を追加</span></span>
<span class="line"><span style="color: #D4D4D4">M  app.txt</span></span></code></pre></div>



<p class="wp-block-paragraph"><code>HEAD~1</code> は「いまの1つ前のコミット」という意味です。コミットは取り消されましたが、変更は add した状態（M が左）で残っています。このまま <code>git commit</code> すれば、メッセージを変えてコミットし直せます。</p>



<h3 class="wp-block-heading">&#8211;mixed：コミットとステージを取り消す（デフォルト）</h3>



<p class="wp-block-paragraph">オプションを省略した場合の挙動です。コミットとステージを取り消し、変更内容はワーキングツリーに残します。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="コピー" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git reset HEAD~1
# git reset --mixed HEAD~1 と同じ</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color:#1E1E1E;color:#D4D4D4" tabindex="0"><code><span class="line"><span style="color:#DCDCAA">git</span><span style="color:#CE9178"> reset</span><span style="color:#CE9178"> HEAD~1</span></span>
<span class="line"><span style="color:#6A9955"># git reset --mixed HEAD~1 と同じ</span></span></code></pre></div>



<p class="wp-block-paragraph">ファイルの内容は消えないので、ステージするファイルを選び直してコミットし直したいときに使います。</p>



<p class="wp-block-paragraph">練習用のリポジトリで試すと、次のようになりました。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="コピー" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git reset HEAD~1
git log --oneline -1
git status --short</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">reset</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">HEAD~1</span></span>
<span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">log</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">--oneline</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">-1</span></span>
<span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">status</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">--short</span></span></code></pre></div>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Text</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="コピー" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>Unstaged changes after reset:
M	app.txt
a855476 3行目を追加
 M app.txt</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #D4D4D4">Unstaged changes after reset:</span></span>
<span class="line"><span style="color: #D4D4D4">M	app.txt</span></span>
<span class="line"><span style="color: #D4D4D4">a855476 3行目を追加</span></span>
<span class="line"><span style="color: #D4D4D4"> M app.txt</span></span></code></pre></div>



<p class="wp-block-paragraph">今度は M が右の列にあり、add も取り消されています。</p>



<h3 class="wp-block-heading">&#8211;hard：全部消す</h3>



<p class="wp-block-paragraph">コミット・ステージ・ワーキングツリーの変更内容まで、すべて指定した状態に巻き戻します。<strong>変更内容そのものが消える、唯一のオプション</strong>です。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="コピー" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git reset --hard HEAD~1</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color:#1E1E1E;color:#D4D4D4" tabindex="0"><code><span class="line"><span style="color:#DCDCAA">git</span><span style="color:#CE9178"> reset</span><span style="color:#569CD6"> --hard</span><span style="color:#CE9178"> HEAD~1</span></span></code></pre></div>



<p class="wp-block-paragraph">実行前に、消えて困る変更が残っていないか必ず確認しましょう。地味に見落としがちですが、<code>--hard</code>はワーキングツリーの未コミット分も巻き込んで消えます。</p>



<p class="wp-block-paragraph">練習用のリポジトリで試すと、次のようになりました。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="コピー" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git reset --hard HEAD~1
git log --oneline -1
git status --short
cat app.txt</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">reset</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">--hard</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">HEAD~1</span></span>
<span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">log</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">--oneline</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">-1</span></span>
<span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">status</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">--short</span></span>
<span class="line"><span style="color: #DCDCAA">cat</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">app.txt</span></span></code></pre></div>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Text</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="コピー" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>HEAD is now at a855476 3行目を追加
a855476 3行目を追加
hello
line2
line3</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #D4D4D4">HEAD is now at a855476 3行目を追加</span></span>
<span class="line"><span style="color: #D4D4D4">a855476 3行目を追加</span></span>
<span class="line"><span style="color: #D4D4D4">hello</span></span>
<span class="line"><span style="color: #D4D4D4">line2</span></span>
<span class="line"><span style="color: #D4D4D4">line3</span></span></code></pre></div>



<p class="wp-block-paragraph"><code>git status --short</code> は何も表示せず、<code>app.txt</code> から4行目が消えました。コミットも、作業ツリーの変更も、すべて1つ前の状態に戻ります。</p>



<h2 class="wp-block-heading">実務で役立つTips</h2>



<h3 class="wp-block-heading">共有ブランチではrevertを使う</h3>



<p class="wp-block-paragraph">resetは<strong>コミット履歴そのものを書き換える</strong>コマンドです。すでにリモートへpush済みで、他の人が同じブランチを見ている場合にresetすると、履歴の食い違いが起きます。</p>



<p class="wp-block-paragraph">そこで使うのがrevertです。取り消したいコミットを打ち消す、新しいコミットを追加する形で処理します。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="コピー" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git revert &lt;取り消したいコミットのハッシュ&gt;</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color:#1E1E1E;color:#D4D4D4" tabindex="0"><code><span class="line"><span style="color:#DCDCAA">git</span><span style="color:#CE9178"> revert</span><span style="color:#D4D4D4"> &lt;</span><span style="color:#CE9178">取り消したいコミットのハッシ</span><span style="color:#D4D4D4">ュ></span></span></code></pre></div>



<p class="wp-block-paragraph">履歴を書き換えないので、他の人の作業に影響を与えません。<strong>「まだ自分しか見ていないローカルの作業ならreset、すでに共有したらrevert」</strong>と覚えておけば判断に迷いません。</p>



<h3 class="wp-block-heading">&#8211;hardで消しすぎたときの復旧はreflog</h3>



<p class="wp-block-paragraph">もし<code>reset --hard</code>で必要なコミットまで消してしまっても、多くの場合は<code>reflog</code>から復旧できます。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="コピー" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git reflog
# 一覧からコミットハッシュを確認して
git reset --hard &lt;復旧したいコミットのハッシュ&gt;</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color:#1E1E1E;color:#D4D4D4" tabindex="0"><code><span class="line"><span style="color:#DCDCAA">git</span><span style="color:#CE9178"> reflog</span></span>
<span class="line"><span style="color:#6A9955"># 一覧からコミットハッシュを確認して</span></span>
<span class="line"><span style="color:#DCDCAA">git</span><span style="color:#CE9178"> reset</span><span style="color:#569CD6"> --hard</span><span style="color:#D4D4D4"> &lt;</span><span style="color:#CE9178">復旧したいコミットのハッシ</span><span style="color:#D4D4D4">ュ></span></span></code></pre></div>



<p class="wp-block-paragraph">reflogはHEADの移動履歴を記録しているので、resetやrebaseのやり直しにも使える保険です。ただし記録には期限があるので、過信は禁物です。</p>



<h3 class="wp-block-heading">複数のコミットをまとめてresetする</h3>



<p class="wp-block-paragraph"><code>HEAD~1</code>の数字を変えることで、さかのぼるコミット数を指定できます。例えば直近3件をまとめて取り消したい場合は、以下のように書きます。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="コピー" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly># 直近3件をまとめてsoftで取り消す
git reset --soft HEAD~3</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color:#1E1E1E;color:#D4D4D4" tabindex="0"><code><span class="line"><span style="color:#6A9955"># 直近3件をまとめてsoftで取り消す</span></span>
<span class="line"><span style="color:#DCDCAA">git</span><span style="color:#CE9178"> reset</span><span style="color:#569CD6"> --soft</span><span style="color:#CE9178"> HEAD~3</span></span></code></pre></div>



<h3 class="wp-block-heading">revertで複数コミットをまとめて取り消す</h3>



<p class="wp-block-paragraph">コミット範囲を指定すれば、複数コミットをまとめて打ち消すコミットを作成できます。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="コピー" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git revert &lt;古い方のハッシュ&gt;..&lt;新しい方のハッシュ&gt;</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color:#1E1E1E;color:#D4D4D4" tabindex="0"><code><span class="line"><span style="color:#DCDCAA">git</span><span style="color:#CE9178"> revert</span><span style="color:#D4D4D4"> &lt;</span><span style="color:#CE9178">古い方のハッシ</span><span style="color:#D4D4D4">ュ></span><span style="color:#CE9178">..</span><span style="color:#D4D4D4">&lt;</span><span style="color:#CE9178">新しい方のハッシ</span><span style="color:#D4D4D4">ュ></span></span></code></pre></div>



<p class="wp-block-paragraph">ただしコンフリクトが起きやすくなるため、範囲が広い場合は1件ずつ確認しながら実行するほうが安全です。</p>



<h2 class="wp-block-heading">消しすぎたら git reflog で戻す</h2>



<p class="wp-block-paragraph">上の「実務で役立つTips」で紹介した <code>git reflog</code> を、実際に動かして確かめます。</p>



<h3 class="wp-block-heading">reflog で前の位置を探す</h3>



<p class="wp-block-paragraph"><code>git reset --hard</code> でコミットを消してしまっても、<strong>一度コミットした内容</strong>なら <code>git reflog</code> で戻せます。reflog には、HEAD が動いた記録が新しい順に残っています。上の reset の節では試すたびに4行目のコミットを作り直したので、「4行目を追加」が3回（ハッシュはそれぞれ別）出ています。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="コピー" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git reflog -6</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">reflog</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">-6</span></span></code></pre></div>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Text</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="コピー" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>a855476 HEAD@{0}: reset: moving to HEAD~1
ce72862 HEAD@{1}: commit: 4行目を追加
a855476 HEAD@{2}: reset: moving to HEAD~1
5a0c2ab HEAD@{3}: commit: 4行目を追加
a855476 HEAD@{4}: reset: moving to HEAD~1
3e94b82 HEAD@{5}: commit: 4行目を追加</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #D4D4D4">a855476 HEAD@{0}: reset: moving to HEAD~1</span></span>
<span class="line"><span style="color: #D4D4D4">ce72862 HEAD@{1}: commit: 4行目を追加</span></span>
<span class="line"><span style="color: #D4D4D4">a855476 HEAD@{2}: reset: moving to HEAD~1</span></span>
<span class="line"><span style="color: #D4D4D4">5a0c2ab HEAD@{3}: commit: 4行目を追加</span></span>
<span class="line"><span style="color: #D4D4D4">a855476 HEAD@{4}: reset: moving to HEAD~1</span></span>
<span class="line"><span style="color: #D4D4D4">3e94b82 HEAD@{5}: commit: 4行目を追加</span></span></code></pre></div>



<p class="wp-block-paragraph">いちばん上が直前の <code>reset</code>、その下の <code>HEAD@{1}</code> が reset する前のコミットです。そこへ <code>git reset --hard</code> で戻します。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="コピー" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git reset --hard HEAD@{1}
git log --oneline -1
cat app.txt</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">reset</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">--hard</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">HEAD@{</span><span style="color: #B5CEA8">1</span><span style="color: #CE9178">}</span></span>
<span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">log</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">--oneline</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">-1</span></span>
<span class="line"><span style="color: #DCDCAA">cat</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">app.txt</span></span></code></pre></div>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Text</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="コピー" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>HEAD is now at ce72862 4行目を追加
ce72862 4行目を追加
hello
line2
line3
line4</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #D4D4D4">HEAD is now at ce72862 4行目を追加</span></span>
<span class="line"><span style="color: #D4D4D4">ce72862 4行目を追加</span></span>
<span class="line"><span style="color: #D4D4D4">hello</span></span>
<span class="line"><span style="color: #D4D4D4">line2</span></span>
<span class="line"><span style="color: #D4D4D4">line3</span></span>
<span class="line"><span style="color: #D4D4D4">line4</span></span></code></pre></div>



<p class="wp-block-paragraph">消えたはずの「4行目を追加」のコミットと、4行目の内容が戻りました。<code>HEAD@{1}</code> の代わりに、reflog に出ているハッシュ（<code>ce72862</code>）を指定してもかまいません。</p>



<h3 class="wp-block-heading">直前の reset は ORIG_HEAD で戻せる</h3>



<p class="wp-block-paragraph">reset した直後なら、Git が覚えている <code>ORIG_HEAD</code>（reset する前の位置）を使うと簡単です。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="コピー" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git reset --hard HEAD~1
git reset --hard ORIG_HEAD</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">reset</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">--hard</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">HEAD~1</span></span>
<span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">reset</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">--hard</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">ORIG_HEAD</span></span></code></pre></div>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Text</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="コピー" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>HEAD is now at a855476 3行目を追加
HEAD is now at ce72862 4行目を追加</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #D4D4D4">HEAD is now at a855476 3行目を追加</span></span>
<span class="line"><span style="color: #D4D4D4">HEAD is now at ce72862 4行目を追加</span></span></code></pre></div>



<h3 class="wp-block-heading">コミットしていない変更は戻せない</h3>



<p class="wp-block-paragraph">reflog はコミットの記録なので、<strong>コミットしていない変更</strong>は残りません。<code>app.txt</code> に1行書き足し、コミットしないまま <code>git reset --hard</code> を実行してみました。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="コピー" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git status --short
git reset --hard
cat app.txt</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">status</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">--short</span></span>
<span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">reset</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">--hard</span></span>
<span class="line"><span style="color: #DCDCAA">cat</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">app.txt</span></span></code></pre></div>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Text</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="コピー" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly> M app.txt
HEAD is now at ce72862 4行目を追加
hello
line2
line3
line4</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #D4D4D4"> M app.txt</span></span>
<span class="line"><span style="color: #D4D4D4">HEAD is now at ce72862 4行目を追加</span></span>
<span class="line"><span style="color: #D4D4D4">hello</span></span>
<span class="line"><span style="color: #D4D4D4">line2</span></span>
<span class="line"><span style="color: #D4D4D4">line3</span></span>
<span class="line"><span style="color: #D4D4D4">line4</span></span></code></pre></div>



<p class="wp-block-paragraph">書き足した行は消え、reflog を見ても戻す方法はありません。<code>--hard</code> の前に残したい変更があるなら、一度コミットするか <code>git stash</code> で退避しておきましょう。</p>



<h2 class="wp-block-heading">push 済みのコミットは git revert で打ち消す</h2>



<p class="wp-block-paragraph">上の「実務で役立つTips」の「共有ブランチではrevertを使う」で紹介した <code>git revert</code> を、実際に動かして確かめます。</p>



<h3 class="wp-block-heading">revert は打ち消すコミットを新しく作る</h3>



<p class="wp-block-paragraph"><code>git revert</code> は、指定したコミットと逆の変更を持つコミットを新しく作ります。履歴は消さずに、変更だけを打ち消します。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="コピー" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git log --oneline
git revert --no-edit HEAD
git log --oneline
cat app.txt</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">log</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">--oneline</span></span>
<span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">revert</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">--no-edit</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">HEAD</span></span>
<span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">log</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">--oneline</span></span>
<span class="line"><span style="color: #DCDCAA">cat</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">app.txt</span></span></code></pre></div>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Text</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="コピー" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>ce72862 4行目を追加
a855476 3行目を追加
2c02da1 2行目を追加
aaaed61 最初のコミット
[main 66edc13] Revert "4行目を追加"
 Date: Wed Nov 18 10:09:00 2026 +0900
 1 file changed, 1 deletion(-)
66edc13 Revert "4行目を追加"
ce72862 4行目を追加
a855476 3行目を追加
2c02da1 2行目を追加
aaaed61 最初のコミット
hello
line2
line3</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #D4D4D4">ce72862 4行目を追加</span></span>
<span class="line"><span style="color: #D4D4D4">a855476 3行目を追加</span></span>
<span class="line"><span style="color: #D4D4D4">2c02da1 2行目を追加</span></span>
<span class="line"><span style="color: #D4D4D4">aaaed61 最初のコミット</span></span>
<span class="line"><span style="color: #D4D4D4">[main 66edc13] Revert &quot;4行目を追加&quot;</span></span>
<span class="line"><span style="color: #D4D4D4"> Date: Wed Nov 18 10:09:00 2026 +0900</span></span>
<span class="line"><span style="color: #D4D4D4"> 1 file changed, 1 deletion(-)</span></span>
<span class="line"><span style="color: #D4D4D4">66edc13 Revert &quot;4行目を追加&quot;</span></span>
<span class="line"><span style="color: #D4D4D4">ce72862 4行目を追加</span></span>
<span class="line"><span style="color: #D4D4D4">a855476 3行目を追加</span></span>
<span class="line"><span style="color: #D4D4D4">2c02da1 2行目を追加</span></span>
<span class="line"><span style="color: #D4D4D4">aaaed61 最初のコミット</span></span>
<span class="line"><span style="color: #D4D4D4">hello</span></span>
<span class="line"><span style="color: #D4D4D4">line2</span></span>
<span class="line"><span style="color: #D4D4D4">line3</span></span></code></pre></div>



<p class="wp-block-paragraph">「4行目を追加」のコミットは履歴に残ったまま、その上に <code>Revert "4行目を追加"</code> というコミットができ、4行目が消えました。<code>--no-edit</code> を付けないと、コミットメッセージを編集するエディタが開きます。作られたコミットの本文には、どのコミットを打ち消したかが書かれています。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Text</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="コピー" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>66edc13 Revert "4行目を追加"

This reverts commit ce728622c3020c55292a12be861c8fd36e95b17b.</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #D4D4D4">66edc13 Revert &quot;4行目を追加&quot;</span></span>
<span class="line"><span style="color: #D4D4D4"></span></span>
<span class="line"><span style="color: #D4D4D4">This reverts commit ce728622c3020c55292a12be861c8fd36e95b17b.</span></span></code></pre></div>



<h3 class="wp-block-heading">push 済みなら reset ではなく revert を使う理由</h3>



<p class="wp-block-paragraph">手元に練習用のリモート（bare リポジトリ）を作って試しました。「バグのある変更」を push したあとで、<code>git reset</code> で消してから push してみます。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="コピー" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git log --oneline
git reset --hard HEAD~1
git push</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">log</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">--oneline</span></span>
<span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">reset</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">--hard</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">HEAD~1</span></span>
<span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">push</span></span></code></pre></div>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Text</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="コピー" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>1e5683f バグのある変更
ccef14f 最初のコミット
HEAD is now at ccef14f 最初のコミット
To /path/to/remote.git
 ! [rejected]        main -> main (non-fast-forward)
error: failed to push some refs to '/path/to/remote.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. If you want to integrate the remote changes,
hint: use 'git pull' before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #D4D4D4">1e5683f バグのある変更</span></span>
<span class="line"><span style="color: #D4D4D4">ccef14f 最初のコミット</span></span>
<span class="line"><span style="color: #D4D4D4">HEAD is now at ccef14f 最初のコミット</span></span>
<span class="line"><span style="color: #D4D4D4">To /path/to/remote.git</span></span>
<span class="line"><span style="color: #D4D4D4"> ! [rejected]        main -&gt; main (non-fast-forward)</span></span>
<span class="line"><span style="color: #D4D4D4">error: failed to push some refs to &apos;/path/to/remote.git&apos;</span></span>
<span class="line"><span style="color: #D4D4D4">hint: Updates were rejected because the tip of your current branch is behind</span></span>
<span class="line"><span style="color: #D4D4D4">hint: its remote counterpart. If you want to integrate the remote changes,</span></span>
<span class="line"><span style="color: #D4D4D4">hint: use &apos;git pull&apos; before pushing again.</span></span>
<span class="line"><span style="color: #D4D4D4">hint: See the &apos;Note about fast-forwards&apos; in &apos;git push --help&apos; for details.</span></span></code></pre></div>



<p class="wp-block-paragraph">手元の履歴がリモートより古い位置に戻ったため、push は <code>rejected</code>（<code>non-fast-forward</code>）で拒否されました。<code>git status -sb</code> を見ると、<code>## main...origin/main [behind 1]</code> と、リモートより1つ遅れている扱いになっています。これを通すには <code>--force</code> で上書きするしかなく、上書きすると、同じブランチを取り込んでいたほかの人の手元と食い違ってしまいます。</p>



<p class="wp-block-paragraph">リモートの状態に戻し（<code>git reset --hard origin/main</code>）、今度は <code>git revert</code> で打ち消してから push しました。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="コピー" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git revert --no-edit HEAD
git push
git log --oneline</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">revert</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">--no-edit</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">HEAD</span></span>
<span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">push</span></span>
<span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">log</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">--oneline</span></span></code></pre></div>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Text</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="コピー" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>[main a51b557] Revert "バグのある変更"
 Date: Wed Nov 18 11:03:00 2026 +0900
 1 file changed, 1 deletion(-)
To /path/to/remote.git
   1e5683f..a51b557  main -> main
a51b557 Revert "バグのある変更"
1e5683f バグのある変更
ccef14f 最初のコミット</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #D4D4D4">[main a51b557] Revert &quot;バグのある変更&quot;</span></span>
<span class="line"><span style="color: #D4D4D4"> Date: Wed Nov 18 11:03:00 2026 +0900</span></span>
<span class="line"><span style="color: #D4D4D4"> 1 file changed, 1 deletion(-)</span></span>
<span class="line"><span style="color: #D4D4D4">To /path/to/remote.git</span></span>
<span class="line"><span style="color: #D4D4D4">   1e5683f..a51b557  main -&gt; main</span></span>
<span class="line"><span style="color: #D4D4D4">a51b557 Revert &quot;バグのある変更&quot;</span></span>
<span class="line"><span style="color: #D4D4D4">1e5683f バグのある変更</span></span>
<span class="line"><span style="color: #D4D4D4">ccef14f 最初のコミット</span></span></code></pre></div>



<p class="wp-block-paragraph">打ち消すコミットを1つ足すだけなので、<code>--force</code> なしでそのまま push できました。</p>



<figure class="wp-block-table"><table><thead><tr><th></th><th><code>git reset</code></th><th><code>git revert</code></th></tr></thead><tbody><tr><td>やること</td><td>ブランチを前のコミットに戻す</td><td>打ち消すコミットを新しく作る</td></tr><tr><td>元のコミット</td><td>履歴から消える</td><td>履歴に残る</td></tr><tr><td>向いている場面</td><td>push する前のやり直し</td><td>push 済みのコミットの取り消し</td></tr><tr><td>push</td><td>push 済みなら <code>--force</code> が要る</td><td>そのまま push できる</td></tr></tbody></table></figure>



<h3 class="wp-block-heading">古いコミットを revert する・衝突したら &#8211;abort</h3>



<p class="wp-block-paragraph">直前ではない古いコミットも、ハッシュや <code>HEAD~n</code> で指定して revert できます。ただし、あとのコミットで同じ行を書き換えていると、衝突します。「2行目を追加」（<code>HEAD~3</code>）を revert してみました。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="コピー" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git revert --no-edit HEAD~3</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">revert</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">--no-edit</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">HEAD~3</span></span></code></pre></div>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Text</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="コピー" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>Auto-merging app.txt
CONFLICT (content): Merge conflict in app.txt
error: could not revert 2c02da1... 2行目を追加
hint: After resolving the conflicts, mark them with
hint: "git add/rm &lt;pathspec>", then run
hint: "git revert --continue".
hint: You can instead skip this commit with "git revert --skip".
hint: To abort and get back to the state before "git revert",
hint: run "git revert --abort".
（後略）</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #D4D4D4">Auto-merging app.txt</span></span>
<span class="line"><span style="color: #D4D4D4">CONFLICT (content): Merge conflict in app.txt</span></span>
<span class="line"><span style="color: #D4D4D4">error: could not revert 2c02da1... 2行目を追加</span></span>
<span class="line"><span style="color: #D4D4D4">hint: After resolving the conflicts, mark them with</span></span>
<span class="line"><span style="color: #D4D4D4">hint: &quot;git add/rm &lt;pathspec&gt;&quot;, then run</span></span>
<span class="line"><span style="color: #D4D4D4">hint: &quot;git revert --continue&quot;.</span></span>
<span class="line"><span style="color: #D4D4D4">hint: You can instead skip this commit with &quot;git revert --skip&quot;.</span></span>
<span class="line"><span style="color: #D4D4D4">hint: To abort and get back to the state before &quot;git revert&quot;,</span></span>
<span class="line"><span style="color: #D4D4D4">hint: run &quot;git revert --abort&quot;.</span></span>
<span class="line"><span style="color: #D4D4D4">（後略）</span></span></code></pre></div>



<p class="wp-block-paragraph">衝突を直して続けるなら、ファイルを編集して <code>git add</code> し、<code>git revert --continue</code> を実行します。やめるなら <code>git revert --abort</code> です。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="コピー" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git revert --abort
git status --short
git log --oneline -1</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">revert</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">--abort</span></span>
<span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">status</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">--short</span></span>
<span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">log</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">--oneline</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">-1</span></span></code></pre></div>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Text</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="コピー" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>66edc13 Revert "4行目を追加"</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #D4D4D4">66edc13 Revert &quot;4行目を追加&quot;</span></span></code></pre></div>



<p class="wp-block-paragraph">revert を始める前の状態に戻りました。ほかのコミットとぶつからない「3行目を追加」（<code>README.md</code> と3行目を足したコミット、<code>HEAD~2</code>）なら、そのまま打ち消せました。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="コピー" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git revert --no-edit HEAD~2</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">revert</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">--no-edit</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">HEAD~2</span></span></code></pre></div>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Text</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="コピー" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>[main 16cd686] Revert "3行目を追加"
 Date: Wed Nov 18 10:11:00 2026 +0900
 2 files changed, 2 deletions(-)
 delete mode 100644 README.md</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #D4D4D4">[main 16cd686] Revert &quot;3行目を追加&quot;</span></span>
<span class="line"><span style="color: #D4D4D4"> Date: Wed Nov 18 10:11:00 2026 +0900</span></span>
<span class="line"><span style="color: #D4D4D4"> 2 files changed, 2 deletions(-)</span></span>
<span class="line"><span style="color: #D4D4D4"> delete mode 100644 README.md</span></span></code></pre></div>



<h3 class="wp-block-heading">revert を取り消す</h3>



<p class="wp-block-paragraph">revert したコミットも、もう一度 revert すれば元に戻せます。手元の git 2.54 では、2回目のコミットは <code>Reapply</code> という名前になりました。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="コピー" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git revert --no-edit HEAD
ls</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">revert</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">--no-edit</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">HEAD</span></span>
<span class="line"><span style="color: #DCDCAA">ls</span></span></code></pre></div>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Text</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="コピー" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>[main a2eb725] Reapply "3行目を追加"
 Date: Wed Nov 18 10:12:00 2026 +0900
 2 files changed, 2 insertions(+)
 create mode 100644 README.md
README.md
app.txt</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #D4D4D4">[main a2eb725] Reapply &quot;3行目を追加&quot;</span></span>
<span class="line"><span style="color: #D4D4D4"> Date: Wed Nov 18 10:12:00 2026 +0900</span></span>
<span class="line"><span style="color: #D4D4D4"> 2 files changed, 2 insertions(+)</span></span>
<span class="line"><span style="color: #D4D4D4"> create mode 100644 README.md</span></span>
<span class="line"><span style="color: #D4D4D4">README.md</span></span>
<span class="line"><span style="color: #D4D4D4">app.txt</span></span></code></pre></div>



<p class="wp-block-paragraph">取り込んだコミットを打ち消す方法は、<a href="https://cfxlog.com/git-cherry-pick-undo-guide/">cherry-pick を取り消す方法の記事</a>でも紹介しています。</p>



<h2 class="wp-block-heading">作業中の変更を取り消す（git restore）</h2>



<p class="wp-block-paragraph">ここからの3つの節は、2行目までコミットした、別の時点の練習用リポジトリで試しています（上の reset の節に出てくる <code>a855476</code> は、このあと紹介する <code>--amend</code> で作ったコミットです）。</p>



<h3 class="wp-block-heading">ファイルの書き換えを元に戻す</h3>



<p class="wp-block-paragraph">まだコミットしていない書き換えを捨てるときは、<code>reset</code> ではなく <code>git restore</code> を使います。<code>app.txt</code> に、間違えて1行書き足してしまったとします。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="コピー" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git status --short
git diff</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">status</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">--short</span></span>
<span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">diff</span></span></code></pre></div>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Text</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="コピー" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly> M app.txt
diff --git a/app.txt b/app.txt
index 1b3eb56..a765ede 100644
--- a/app.txt
+++ b/app.txt
@@ -1,2 +1,3 @@
 hello
 line2
+まちがえて書いた行</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #D4D4D4"> M app.txt</span></span>
<span class="line"><span style="color: #D4D4D4">diff --git a/app.txt b/app.txt</span></span>
<span class="line"><span style="color: #D4D4D4">index 1b3eb56..a765ede 100644</span></span>
<span class="line"><span style="color: #D4D4D4">--- a/app.txt</span></span>
<span class="line"><span style="color: #D4D4D4">+++ b/app.txt</span></span>
<span class="line"><span style="color: #D4D4D4">@@ -1,2 +1,3 @@</span></span>
<span class="line"><span style="color: #D4D4D4"> hello</span></span>
<span class="line"><span style="color: #D4D4D4"> line2</span></span>
<span class="line"><span style="color: #D4D4D4">+まちがえて書いた行</span></span></code></pre></div>



<p class="wp-block-paragraph">最後にコミットした状態に戻すには、<code>git restore</code> にファイル名を渡します。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="コピー" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git restore app.txt
git status --short
cat app.txt</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">restore</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">app.txt</span></span>
<span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">status</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">--short</span></span>
<span class="line"><span style="color: #DCDCAA">cat</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">app.txt</span></span></code></pre></div>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Text</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="コピー" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>hello
line2</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #D4D4D4">hello</span></span>
<span class="line"><span style="color: #D4D4D4">line2</span></span></code></pre></div>



<p class="wp-block-paragraph"><code>git status --short</code> は何も表示しなくなり、書き足した行が消えました。複数のファイルをまとめて戻すときは <code>git restore .</code> とします。</p>



<div class="swell-block-capbox cap_box is-style-onborder_ttl"><div class="cap_box_ttl"><span>注意：restore で消した変更は戻せない</span></div><div class="cap_box_content">
<p class="wp-block-paragraph"><code>git restore</code> で捨てた変更は、コミットもステージもしていないので Git には残っていません。<code>git reflog</code> でも戻せないので、必要かもしれない変更は先に <code>git stash</code> で退避しておくと安心です。</p>
</div></div>



<h3 class="wp-block-heading">新しく作ったファイルは git clean で消す</h3>



<p class="wp-block-paragraph">まだ Git に追加していない新しいファイル（<code>memo.txt</code>）に <code>git restore</code> を使うと、エラーになります。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="コピー" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git restore memo.txt</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">restore</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">memo.txt</span></span></code></pre></div>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Text</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="コピー" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>error: pathspec 'memo.txt' did not match any file(s) known to git</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #D4D4D4">error: pathspec &apos;memo.txt&apos; did not match any file(s) known to git</span></span></code></pre></div>



<p class="wp-block-paragraph">Git が管理していないファイルは、<code>git clean</code> で消します。いきなり消さず、まず <code>-n</code> で何が消えるかを確かめます。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="コピー" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git clean -n</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">clean</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">-n</span></span></code></pre></div>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Text</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="コピー" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>Would remove memo.txt</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #D4D4D4">Would remove memo.txt</span></span></code></pre></div>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="コピー" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git clean -f</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">clean</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">-f</span></span></code></pre></div>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Text</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="コピー" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>Removing memo.txt</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #D4D4D4">Removing memo.txt</span></span></code></pre></div>



<p class="wp-block-paragraph"><code>git clean -f</code> で消したファイルはゴミ箱にも入りません。消す前に、必ず <code>-n</code> で一覧を確かめてください。</p>



<h3 class="wp-block-heading">昔の書き方 git checkout &#8212; でも戻せる</h3>



<p class="wp-block-paragraph">古い記事では、<code>git checkout -- app.txt</code> という書き方をよく見かけます。手元で試すと、こちらでも同じように元に戻りました。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="コピー" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git checkout -- app.txt</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">checkout</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">--</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">app.txt</span></span></code></pre></div>



<p class="wp-block-paragraph"><code>git checkout</code> はブランチの切り替えにも使うため、ファイルを戻す用途には、役割のはっきりした <code>git restore</code>（Git 2.23 で追加）を使うのがおすすめです。</p>



<h2 class="wp-block-heading">git add を取り消す（git restore &#8211;staged）</h2>



<h3 class="wp-block-heading">1つのファイルだけ取り消す</h3>



<p class="wp-block-paragraph"><code>git add .</code> で、コミットするつもりのなかった <code>config.local</code> までステージに入れてしまいました。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="コピー" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git add .
git status</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">add</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">.</span></span>
<span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">status</span></span></code></pre></div>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Text</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="コピー" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>On branch main
Changes to be committed:
  (use "git restore --staged &lt;file>..." to unstage)
	modified:   app.txt
	new file:   config.local</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #D4D4D4">On branch main</span></span>
<span class="line"><span style="color: #D4D4D4">Changes to be committed:</span></span>
<span class="line"><span style="color: #D4D4D4">  (use &quot;git restore --staged &lt;file&gt;...&quot; to unstage)</span></span>
<span class="line"><span style="color: #D4D4D4">	modified:   app.txt</span></span>
<span class="line"><span style="color: #D4D4D4">	new file:   config.local</span></span></code></pre></div>



<p class="wp-block-paragraph"><code>git status</code> の案内にも出ているとおり、<code>git restore --staged</code> で取り消せます。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="コピー" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git restore --staged config.local
git status --short</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">restore</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">--staged</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">config.local</span></span>
<span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">status</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">--short</span></span></code></pre></div>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Text</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="コピー" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>M  app.txt
?? config.local</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #D4D4D4">M  app.txt</span></span>
<span class="line"><span style="color: #D4D4D4">?? config.local</span></span></code></pre></div>



<p class="wp-block-paragraph"><code>config.local</code> はステージから外れて、追跡されていないファイル（<code>??</code>）に戻りました。<strong>ファイルの中身はそのまま残っています</strong>。取り消されるのは「add した」ことだけです。</p>



<h3 class="wp-block-heading">全部まとめて取り消す</h3>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="コピー" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git restore --staged .
git status --short</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">restore</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">--staged</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">.</span></span>
<span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">status</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">--short</span></span></code></pre></div>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Text</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="コピー" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly> M app.txt
?? config.local</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #D4D4D4"> M app.txt</span></span>
<span class="line"><span style="color: #D4D4D4">?? config.local</span></span></code></pre></div>



<p class="wp-block-paragraph"><code>app.txt</code> の M が右の列に移り、ステージから外れて作業ツリーの変更に戻ったことが分かります。</p>



<h3 class="wp-block-heading">git reset でも取り消せる</h3>



<p class="wp-block-paragraph"><code>git reset ファイル名</code>（または <code>git reset HEAD ファイル名</code>）でも、同じように add を取り消せます。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="コピー" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git add app.txt
git reset app.txt</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">add</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">app.txt</span></span>
<span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">reset</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">app.txt</span></span></code></pre></div>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Text</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="コピー" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>Unstaged changes after reset:
M	app.txt</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #D4D4D4">Unstaged changes after reset:</span></span>
<span class="line"><span style="color: #D4D4D4">M	app.txt</span></span></code></pre></div>



<p class="wp-block-paragraph">どちらを使っても結果は同じです。僕は、名前で用途が分かる <code>git restore --staged</code> を使っています。</p>



<h2 class="wp-block-heading">直前のコミットを直す（git commit &#8211;amend）</h2>



<h3 class="wp-block-heading">コミットメッセージを直す</h3>



<p class="wp-block-paragraph">コミットメッセージに余計な文字を入れたままコミットしてしまいました。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="コピー" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git commit -am "3行目を追加（タイポ）"
git log --oneline -1</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">commit</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">-am</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">&quot;3行目を追加（タイポ）&quot;</span></span>
<span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">log</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">--oneline</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">-1</span></span></code></pre></div>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Text</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="コピー" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>913456a 3行目を追加（タイポ）</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #D4D4D4">913456a 3行目を追加（タイポ）</span></span></code></pre></div>



<p class="wp-block-paragraph">直前のコミットなら、<code>--amend</code> でメッセージを付け直せます。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="コピー" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git commit --amend -m "3行目を追加"
git log --oneline</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">commit</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">--amend</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">-m</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">&quot;3行目を追加&quot;</span></span>
<span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">log</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">--oneline</span></span></code></pre></div>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Text</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="コピー" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>3813eab 3行目を追加
2c02da1 2行目を追加
aaaed61 最初のコミット</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #D4D4D4">3813eab 3行目を追加</span></span>
<span class="line"><span style="color: #D4D4D4">2c02da1 2行目を追加</span></span>
<span class="line"><span style="color: #D4D4D4">aaaed61 最初のコミット</span></span></code></pre></div>



<p class="wp-block-paragraph">履歴にコミットが増えるのではなく、直前のコミットが置き換わりました。ハッシュも <code>913456a</code> から <code>3813eab</code> に変わっています。</p>



<h3 class="wp-block-heading">入れ忘れたファイルを足す</h3>



<p class="wp-block-paragraph">コミットしたあとで、<code>README.md</code> を入れ忘れたことに気づいたときも <code>--amend</code> を使います。メッセージを変えないなら <code>--no-edit</code> を付けます。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="コピー" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git add README.md
git commit --amend --no-edit
git show --stat --format="%h %s" HEAD</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">add</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">README.md</span></span>
<span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">commit</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">--amend</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">--no-edit</span></span>
<span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">show</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">--stat</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">--format=</span><span style="color: #CE9178">&quot;%h %s&quot;</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">HEAD</span></span></code></pre></div>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Text</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="コピー" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>a855476 3行目を追加

 README.md | 1 +
 app.txt   | 1 +
 2 files changed, 2 insertions(+)</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #D4D4D4">a855476 3行目を追加</span></span>
<span class="line"><span style="color: #D4D4D4"></span></span>
<span class="line"><span style="color: #D4D4D4"> README.md | 1 +</span></span>
<span class="line"><span style="color: #D4D4D4"> app.txt   | 1 +</span></span>
<span class="line"><span style="color: #D4D4D4"> 2 files changed, 2 insertions(+)</span></span></code></pre></div>



<p class="wp-block-paragraph">1つのコミットに <code>README.md</code> も入りました。</p>



<div class="swell-block-capbox cap_box is-style-onborder_ttl"><div class="cap_box_ttl"><span>注意：&#8211;amend は push する前だけにする</span></div><div class="cap_box_content">
<p class="wp-block-paragraph"><code>--amend</code> はコミットを作り直すので、ハッシュが変わります。すでに push したコミットを <code>--amend</code> すると、リモートの履歴と食い違ってしまいます。push 済みのコミットを直したいときは、<code>git revert</code> か、直すための新しいコミットを作ります。</p>
</div></div>



<h2 class="wp-block-heading">作業をいったん退避する（git stash）</h2>



<h3 class="wp-block-heading">捨てずに片付けるなら stash</h3>



<p class="wp-block-paragraph">変更を捨てるのではなく、一時的に片付けておきたいときは <code>git stash</code> を使います。退避した変更は <code>git stash pop</code> で戻せます。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="コピー" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git stash
git stash list
git stash pop</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">stash</span></span>
<span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">stash</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">list</span></span>
<span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">stash</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">pop</span></span></code></pre></div>



<p class="wp-block-paragraph">何も付けない <code>git stash</code> は、Git が追跡しているファイルの変更だけを退避します。新しく作ったファイルも一緒に退避するときは <code>-u</code> を付けます。stash の使い方は<a href="https://cfxlog.com/git-stash-guide/">git stash の使い方の記事</a>で詳しく紹介しています。</p>



<h2 class="wp-block-heading">まとめ</h2>



<p class="wp-block-paragraph">取り消す範囲で選ぶならsoft・mixed・hardのどれか、共有済みかどうかで選ぶならreset・revertのどちらか。この2軸を意識するだけで、取り消し系コマンド選びに迷わなくなります。</p>



<p class="wp-block-paragraph">reset と revert 以外も含めると、次のように使い分けます。</p>



<div class="wp-block-group has-border -border01"><div class="wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained">
<ul class="wp-block-list">
<li>作業中の書き換えは <code>git restore</code>、新しいファイルは <code>git clean -n</code> で確かめてから <code>-f</code></li>

<li><code>git add</code> の取り消しは <code>git restore --staged</code></li>

<li>直前のコミットの手直しは <code>git commit --amend</code>（push 前だけ）</li>

<li>push 前のコミットの取り消しは <code>git reset</code>（<code>--soft</code>・<code>--mixed</code>・<code>--hard</code>）</li>

<li>push 済みのコミットは <code>git revert</code> で打ち消す</li>

<li>reset しすぎたら <code>git reflog</code> で戻す（コミットした内容だけ）</li>
</ul>
</div></div>



<p class="wp-block-paragraph">取り消しで一番怖いのは、コミットしていない変更を <code>--hard</code> や <code>restore</code> で消してしまうことです。迷ったら、まず <code>git stash</code> かコミットで変更を残してから操作すると安心です。ブランチの変更を取り込む方法は<a href="https://cfxlog.com/git-cherry-pick-guide/">cherry-pick の使い方</a>で紹介しています。</p>

]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>git stashの使い方完全ガイド｜作業を一時退避してブランチを切り替える</title>
		<link>https://cfxlog.com/git-stash-guide/</link>
		
		<dc:creator><![CDATA[CFXLOG]]></dc:creator>
		<pubDate>Mon, 27 Jul 2026 15:00:00 +0000</pubDate>
				<category><![CDATA[Git]]></category>
		<category><![CDATA[Git運用]]></category>
		<guid isPermaLink="false">https://cfxlog.com/git-stash-guide/</guid>

					<description><![CDATA[よくある話が、作業の途中で急に別のブランチに切り替えたくなる場面。 コミットするほどはまとまっていないが、そのままではブランチを切り替えられない。ところが、コミットせずに変更を一時的に退避できるのがgit stash。  [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">よくある話が、作業の途中で急に別のブランチに切り替えたくなる場面。</p>



<p class="wp-block-paragraph">コミットするほどはまとまっていないが、そのままではブランチを切り替えられない。ところが、コミットせずに変更を一時的に退避できるのがgit stash。</p>



<p class="wp-block-paragraph">この記事では、基本の使い方から、実務でつまずきやすい複数stashの管理方法までまとめた。</p>


<div class="swell-block-postLink">			<div class="p-blogCard -internal" data-type="type1" data-onclick="clickLink">
				<div class="p-blogCard__inner">
					<span class="p-blogCard__caption">あわせて読みたい</span>
					<div class="p-blogCard__thumb c-postThumb"><figure class="c-postThumb__figure"><img decoding="async" src="https://cfxlog.com/wp-content/uploads/2026/03/git-basics-cui-300x158.jpg" alt="" class="c-postThumb__img u-obf-cover" width="320" height="180"></figure></div>					<div class="p-blogCard__body">
						<a class="p-blogCard__title" href="https://cfxlog.com/git-basics-cui/">【基本】CUI を使った Git の基本操作</a>
						<span class="p-blogCard__excerpt">独学でプログラミングを始めると、Git はとても身につけにくい技術です。 Git はとても便利な反面、処理のフローが複雑で初学者にとって複雑怪奇。 さらに個人開発では &#8230;</span>					</div>
				</div>
			</div>
		</div>


<h2 class="wp-block-heading">stashの基本：退避して、戻す</h2>



<h3 class="wp-block-heading">退避する：stash</h3>



<p class="wp-block-paragraph">作業中の変更を退避するには、次のコマンドを使う。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git stash</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color:#1E1E1E;color:#D4D4D4" tabindex="0"><code><span class="line"><span style="color:#DCDCAA">git</span><span style="color:#CE9178"> stash</span></span></code></pre></div>



<p class="wp-block-paragraph">ワーキングツリーとステージの変更が退避され、直前のコミット状態に戻る。これで別のブランチへ自由に切り替えられる。</p>



<h3 class="wp-block-heading">戻す：stash pop</h3>



<p class="wp-block-paragraph">作業を再開するときは、退避した内容を元に戻す。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git stash pop</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color:#1E1E1E;color:#D4D4D4" tabindex="0"><code><span class="line"><span style="color:#DCDCAA">git</span><span style="color:#CE9178"> stash</span><span style="color:#CE9178"> pop</span></span></code></pre></div>



<p class="wp-block-paragraph">これだけで、コミットを汚さずに一時中断・再開ができる。</p>



<h2 class="wp-block-heading">popとapplyの違い</h2>



<p class="wp-block-paragraph">退避した内容を戻すコマンドには、<code>pop</code>のほかに<code>apply</code>もある。</p>



<figure class="wp-block-table"><table><thead><tr><th>コマンド</th><th>戻したあとのstash</th><th>使うタイミングの目安</th></tr></thead><tbody><tr><td>stash pop</td><td>一覧から削除される</td><td>普段の一時退避（そのまま使い切る）</td></tr><tr><td>stash apply</td><td>一覧に残る</td><td>同じ変更を複数のブランチに適用したいとき</td></tr></tbody></table></figure>



<p class="wp-block-paragraph">迷ったら<code>pop</code>で十分。<code>apply</code>は、同じ変更を別ブランチにも当てたい、といった特殊な場面だけ意識すればいい。</p>



<h2 class="wp-block-heading">複数のstashを管理する</h2>



<h3 class="wp-block-heading">一覧を確認する：stash list</h3>



<p class="wp-block-paragraph">stashは何度でも積み重ねられる。何を退避したか分からなくなったら、一覧で確認する。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git stash list</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color:#1E1E1E;color:#D4D4D4" tabindex="0"><code><span class="line"><span style="color:#DCDCAA">git</span><span style="color:#CE9178"> stash</span><span style="color:#CE9178"> list</span></span></code></pre></div>



<h3 class="wp-block-heading">特定のstashを指定する</h3>



<p class="wp-block-paragraph">複数ある場合、単に<code>pop</code>や<code>apply</code>だけを実行すると一番新しいものが対象になる。特定のstashを指定したいときは番号を渡す。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git stash pop stash@{2}</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color:#1E1E1E;color:#D4D4D4" tabindex="0"><code><span class="line"><span style="color:#DCDCAA">git</span><span style="color:#CE9178"> stash</span><span style="color:#CE9178"> pop</span><span style="color:#CE9178"> stash@{</span><span style="color:#B5CEA8">2</span><span style="color:#CE9178">}</span></span></code></pre></div>



<h3 class="wp-block-heading">メッセージを付けて退避する</h3>



<p class="wp-block-paragraph">意外と見落としがちだが、stashは積み重なるほど「どれが何だったか」が分かりにくくなる。退避するときにメッセージを付けておくと、あとで迷わない。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git stash push -m "検索機能の途中作業"</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color:#1E1E1E;color:#D4D4D4" tabindex="0"><code><span class="line"><span style="color:#DCDCAA">git</span><span style="color:#CE9178"> stash</span><span style="color:#CE9178"> push</span><span style="color:#569CD6"> -m</span><span style="color:#CE9178"> "検索機能の途中作業"</span></span></code></pre></div>



<h2 class="wp-block-heading">一部のファイルだけ退避する</h2>



<p class="wp-block-paragraph">変更したファイルのうち、一部だけを退避したい場合は<code>-p</code>（<code>--patch</code>）オプションを使う。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git stash push -p</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color:#1E1E1E;color:#D4D4D4" tabindex="0"><code><span class="line"><span style="color:#DCDCAA">git</span><span style="color:#CE9178"> stash</span><span style="color:#CE9178"> push</span><span style="color:#569CD6"> -p</span></span></code></pre></div>



<p class="wp-block-paragraph">変更のかたまり（hunk）ごとに、退避するかどうかを対話形式で選べる。1つのファイルに複数の作業が混ざってしまったときに使う。</p>



<h2 class="wp-block-heading">stashをそのまま新しいブランチにする</h2>



<p class="wp-block-paragraph">退避した内容が、実は別ブランチとして進めるべき作業だった、というケースもある。その場合は<code>stash branch</code>が使える。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git stash branch new-feature stash@{0}</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color:#1E1E1E;color:#D4D4D4" tabindex="0"><code><span class="line"><span style="color:#DCDCAA">git</span><span style="color:#CE9178"> stash</span><span style="color:#CE9178"> branch</span><span style="color:#CE9178"> new-feature</span><span style="color:#CE9178"> stash@{</span><span style="color:#B5CEA8">0</span><span style="color:#CE9178">}</span></span></code></pre></div>



<p class="wp-block-paragraph">新しいブランチを作成し、そこにstashの内容を復元してくれる。stashを適用する際にコンフリクトが起きそうな場合にも、退避元の状態を汚さず試せるので安心。</p>



<h2 class="wp-block-heading">よくある質問</h2>



<p class="wp-block-paragraph">本文で触れきれなかった細かい疑問をQ&#038;Aでまとめておきます。</p>



<div class="swell-block-faq is-style-faq-border" data-q="fill-custom" data-a="fill-main">
<div class="swell-block-faq__item"><h3 class="faq_q">stash popで戻すときにコンフリクトが起きたらどうすればいいですか？</h3><div class="faq_a">
<p class="wp-block-paragraph">通常のマージコンフリクトと同様に、該当ファイルを手動で解消して<code>git add</code>します。このケースでは<code>pop</code>が完了扱いにならずstash自体は一覧に残るため、解消後に不要であれば<code>git stash drop</code>で手動削除してください。</p>
</div></div>



<div class="swell-block-faq__item"><h3 class="faq_q">stashしたのに変更が退避されないファイルがあります。なぜですか？</h3><div class="faq_a">
<p class="wp-block-paragraph">新規追加した未追跡ファイル（Untracked files）は、デフォルトでは退避対象になりません。含めたい場合は<code>git stash -u</code>（<code>--include-untracked</code>）を使ってください。</p>
</div></div>



<div class="swell-block-faq__item"><h3 class="faq_q">不要になったstashを削除するには？</h3><div class="faq_a">
<p class="wp-block-paragraph">特定の1件を消すなら<code>git stash drop stash@{番号}</code>、全部まとめて消すなら<code>git stash clear</code>を使います。<code>clear</code>は復元できないため、実行前に本当に不要か確認してください。</p>
</div></div>
</div>



<h2 class="wp-block-heading">まとめ</h2>



<p class="wp-block-paragraph">基本は<code>stash</code>と<code>stash pop</code>の2つで十分。管理に困ったら<code>list</code>で確認し、複雑な場面ではメッセージ付き退避や<code>-p</code>、<code>stash branch</code>を使い分ければいい。</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>.gitignoreの書き方完全ガイド｜パターン記法から適用済みファイルの除外まで</title>
		<link>https://cfxlog.com/gitignore-guide/</link>
		
		<dc:creator><![CDATA[CFXLOG]]></dc:creator>
		<pubDate>Tue, 14 Jul 2026 15:00:00 +0000</pubDate>
				<category><![CDATA[Git]]></category>
		<category><![CDATA[Git運用]]></category>
		<guid isPermaLink="false">https://cfxlog.com/gitignore-guide/</guid>

					<description><![CDATA[よくある話が、環境依存のファイルやライブラリのキャッシュまで、うっかりGit管理下に入れてしまう場面。 ところが、.gitignoreの書き方はパターンの種類が意外と多く、思った通りに除外できないことがある。 この記事で [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">よくある話が、環境依存のファイルやライブラリのキャッシュまで、うっかりGit管理下に入れてしまう場面。</p>



<p class="wp-block-paragraph">ところが、<code>.gitignore</code>の書き方はパターンの種類が意外と多く、思った通りに除外できないことがある。</p>



<p class="wp-block-paragraph">この記事では、基本のパターン記法から、すでにコミットしてしまったファイルの除外方法までまとめた。</p>


<div class="swell-block-postLink">			<div class="p-blogCard -internal" data-type="type1" data-onclick="clickLink">
				<div class="p-blogCard__inner">
					<span class="p-blogCard__caption">あわせて読みたい</span>
					<div class="p-blogCard__thumb c-postThumb"><figure class="c-postThumb__figure"><img decoding="async" src="https://cfxlog.com/wp-content/uploads/2026/03/git-basics-cui-300x158.jpg" alt="" class="c-postThumb__img u-obf-cover" width="320" height="180"></figure></div>					<div class="p-blogCard__body">
						<a class="p-blogCard__title" href="https://cfxlog.com/git-basics-cui/">【基本】CUI を使った Git の基本操作</a>
						<span class="p-blogCard__excerpt">独学でプログラミングを始めると、Git はとても身につけにくい技術です。 Git はとても便利な反面、処理のフローが複雑で初学者にとって複雑怪奇。 さらに個人開発では &#8230;</span>					</div>
				</div>
			</div>
		</div>


<h2 class="wp-block-heading">.gitignoreの基本</h2>



<p class="wp-block-paragraph">リポジトリのルートに<code>.gitignore</code>という名前のファイルを置くと、そこに書いたパターンに一致するファイル・フォルダがGitの追跡対象から外れる。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly># 特定のファイル
.env

# 特定の拡張子すべて
*.log

# 特定のフォルダごと
node_modules/
__pycache__/</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color:#1E1E1E;color:#D4D4D4" tabindex="0"><code><span class="line"><span style="color:#6A9955"># 特定のファイル</span></span>
<span class="line"><span style="color:#DCDCAA">.env</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6A9955"># 特定の拡張子すべて</span></span>
<span class="line"><span style="color:#D4D4D4">*.log</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6A9955"># 特定のフォルダごと</span></span>
<span class="line"><span style="color:#DCDCAA">node_modules/</span></span>
<span class="line"><span style="color:#DCDCAA">__pycache__/</span></span></code></pre></div>



<div class="wp-block-group has-border -border01"><div class="wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained">
<ul class="wp-block-list">
<li>末尾に<code>/</code>を付けると、フォルダそのものを除外対象にする</li>

<li><code>*</code>は0文字以上の任意の文字列にマッチするワイルドカード</li>
</ul>
</div></div>



<h2 class="wp-block-heading">パターン記法で迷いやすいポイント</h2>



<h3 class="wp-block-heading">否定パターン（！）で一部だけ除外を解除する</h3>



<p class="wp-block-paragraph">フォルダごと除外しつつ、特定のファイルだけは残したい場合、<code>!</code>を先頭に付けて除外を打ち消す。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>logs/*
!logs/.gitkeep</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color:#1E1E1E;color:#D4D4D4" tabindex="0"><code><span class="line"><span style="color:#DCDCAA">logs/*</span></span>
<span class="line"><span style="color:#D4D4D4">!</span><span style="color:#DCDCAA">logs/.gitkeep</span></span></code></pre></div>



<p class="wp-block-paragraph">ただし、<strong>親フォルダ自体が先に除外されていると、否定パターンは効かない</strong>。フォルダ配下のファイルを指定して除外し、フォルダ自体は除外しない書き方にする必要がある。</p>



<h3 class="wp-block-heading">先頭の/でルート直下だけに限定する</h3>



<p class="wp-block-paragraph">パターンの先頭に<code>/</code>を付けると、リポジトリのルート直下のみが対象になる。付けなければ、同名のファイル・フォルダがどの階層にあっても対象になる。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>/build/     # ルート直下のbuildフォルダのみ除外
build/      # どの階層のbuildフォルダも除外</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color:#1E1E1E;color:#D4D4D4" tabindex="0"><code><span class="line"><span style="color:#DCDCAA">/build/</span><span style="color:#6A9955">     # ルート直下のbuildフォルダのみ除外</span></span>
<span class="line"><span style="color:#DCDCAA">build/</span><span style="color:#6A9955">      # どの階層のbuildフォルダも除外</span></span></code></pre></div>



<p class="wp-block-paragraph">意外と見落としがちだが、この違いを意識しないと、想定より広い範囲・あるいは狭い範囲を除外してしまうことがある。</p>



<h2 class="wp-block-heading">すでにコミット済みのファイルを除外する</h2>



<p class="wp-block-paragraph"><code>.gitignore</code>に書いても、<strong>すでにGit管理下に入っているファイルは自動では除外されない</strong>。これが一番つまずきやすいポイント。</p>



<p class="wp-block-paragraph">この場合は、Gitの追跡だけを解除する。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git rm --cached .env
git commit -m "Remove .env from tracking"</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color:#1E1E1E;color:#D4D4D4" tabindex="0"><code><span class="line"><span style="color:#DCDCAA">git</span><span style="color:#CE9178"> rm</span><span style="color:#569CD6"> --cached</span><span style="color:#CE9178"> .env</span></span>
<span class="line"><span style="color:#DCDCAA">git</span><span style="color:#CE9178"> commit</span><span style="color:#569CD6"> -m</span><span style="color:#CE9178"> "Remove .env from tracking"</span></span></code></pre></div>



<p class="wp-block-paragraph"><code>--cached</code>を付けることで、ローカルのファイル自体は残したまま、Gitの追跡対象からだけ外せる。この状態で<code>.gitignore</code>に書いておけば、以降は変更が追跡されなくなる。</p>



<p class="wp-block-paragraph">フォルダごと除外し直したい場合は<code>-r</code>を付ける。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git rm -r --cached node_modules</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color:#1E1E1E;color:#D4D4D4" tabindex="0"><code><span class="line"><span style="color:#DCDCAA">git</span><span style="color:#CE9178"> rm</span><span style="color:#569CD6"> -r</span><span style="color:#569CD6"> --cached</span><span style="color:#CE9178"> node_modules</span></span></code></pre></div>



<h2 class="wp-block-heading">プロジェクト共通ではなく自分の環境だけ除外したい場合</h2>



<p class="wp-block-paragraph">エディタの設定ファイルなど、チームの<code>.gitignore</code>には含めたくない、自分だけの除外ルールもある。その場合はグローバルな除外設定を使う。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git config --global core.excludesfile ~/.gitignore_global</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color:#1E1E1E;color:#D4D4D4" tabindex="0"><code><span class="line"><span style="color:#DCDCAA">git</span><span style="color:#CE9178"> config</span><span style="color:#569CD6"> --global</span><span style="color:#CE9178"> core.excludesfile</span><span style="color:#CE9178"> ~/.gitignore_global</span></span></code></pre></div>



<p class="wp-block-paragraph">プロジェクトの<code>.gitignore</code>にはリポジトリ全体で共有すべきルールだけを書き、個人の作業環境に依存するルールはグローバル設定側に分けておくと、他のメンバーの<code>.gitignore</code>を汚さずに済む。</p>



<h2 class="wp-block-heading">よくある質問</h2>



<p class="wp-block-paragraph">本文で触れきれなかった細かい疑問をQ&#038;Aでまとめておきます。</p>



<div class="swell-block-faq is-style-faq-border" data-q="fill-custom" data-a="fill-main">
<div class="swell-block-faq__item"><h3 class="faq_q">言語・フレームワークごとの.gitignoreテンプレートはありますか？</h3><div class="faq_a">
<p class="wp-block-paragraph">GitHub公式が公開している<code>gitignore</code>リポジトリに、言語・ツール別のテンプレートがまとまっています。新規プロジェクトを作る際は、まずここから該当するテンプレートをコピーしてから、プロジェクト固有のルールを追記するのがおすすめです。</p>
</div></div>



<div class="swell-block-faq__item"><h3 class="faq_q">.gitignoreを追記・修正したのに反映されません。なぜですか？</h3><div class="faq_a">
<p class="wp-block-paragraph">すでにコミット済みのファイルは、.gitignoreの新規ルールの影響を受けません。対象ファイルに対して<code>git rm --cached</code>を実行し、Gitの追跡から一度外す必要があります。</p>
</div></div>



<div class="swell-block-faq__item"><h3 class="faq_q">特定のファイルだけ、チーム全体の.gitignoreを無視して強制的に追跡させたいです。</h3><div class="faq_a">
<p class="wp-block-paragraph"><code>git add -f &lt;ファイル名&gt;</code>で、.gitignoreのルールを無視して強制的にステージできます。ただし恒常的に必要なら、.gitignore側に否定パターン（<code>!</code>）を追記するほうがチームで扱いやすくなります。</p>
</div></div>
</div>



<h2 class="wp-block-heading">まとめ</h2>



<p class="wp-block-paragraph">基本のパターンさえ押さえれば、あとは「すでに追跡済みなら<code>rm --cached</code>」「個人設定はグローバル側」という2つの応用を覚えておけば困らない。</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>git rebase・mergeの使い分け完全ガイド｜履歴の残し方で選ぶ判断軸</title>
		<link>https://cfxlog.com/git-rebase-merge-guide/</link>
		
		<dc:creator><![CDATA[CFXLOG]]></dc:creator>
		<pubDate>Tue, 30 Jun 2026 15:00:00 +0000</pubDate>
				<category><![CDATA[Git]]></category>
		<category><![CDATA[Git運用]]></category>
		<guid isPermaLink="false">https://cfxlog.com/git-rebase-merge-guide/</guid>

					<description><![CDATA[<p><img src="https://cfxlog.com/wp-content/uploads/2026/07/git-rebase-merge-guide-1024x538.jpg" class="webfeedsFeaturedVisual" /></p>ブランチを統合するときに、mergeとrebaseのどちらを使うか迷う場面はよくあります。 ところが、この2つはやりたいこと自体は同じでも、コミット履歴の残り方がまったく違います。 そこで、この記事では「履歴をどう残した [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><img src="https://cfxlog.com/wp-content/uploads/2026/07/git-rebase-merge-guide-1024x538.jpg" class="webfeedsFeaturedVisual" /></p>
<p class="wp-block-paragraph">ブランチを統合するときに、mergeとrebaseのどちらを使うか迷う場面はよくあります。</p>



<p class="wp-block-paragraph">ところが、この2つは<strong>やりたいこと自体は同じでも、コミット履歴の残り方がまったく違います</strong>。</p>



<p class="wp-block-paragraph">そこで、この記事では<strong>「履歴をどう残したいか」を軸にした使い分け</strong>を整理しました。</p>


<div class="swell-block-postLink">			<div class="p-blogCard -internal" data-type="type1" data-onclick="clickLink">
				<div class="p-blogCard__inner">
					<span class="p-blogCard__caption">あわせて読みたい</span>
					<div class="p-blogCard__thumb c-postThumb"><figure class="c-postThumb__figure"><img decoding="async" src="https://cfxlog.com/wp-content/uploads/2026/03/git-basics-cui-300x158.jpg" alt="" class="c-postThumb__img u-obf-cover" width="320" height="180"></figure></div>					<div class="p-blogCard__body">
						<a class="p-blogCard__title" href="https://cfxlog.com/git-basics-cui/">【基本】CUI を使った Git の基本操作</a>
						<span class="p-blogCard__excerpt">独学でプログラミングを始めると、Git はとても身につけにくい技術です。 Git はとても便利な反面、処理のフローが複雑で初学者にとって複雑怪奇。 さらに個人開発では &#8230;</span>					</div>
				</div>
			</div>
		</div>


<h2 class="wp-block-heading">mergeとrebase、何が違うのか</h2>



<figure class="wp-block-table"><table><thead><tr><th>項目</th><th>merge</th><th>rebase</th></tr></thead><tbody><tr><td>履歴の見た目</td><td>分岐・統合がそのまま残る</td><td>一直線に並び替えられる</td></tr><tr><td>コミット本体</td><td>変更されない</td><td>再作成される（ハッシュが変わる）</td></tr><tr><td>マージコミット</td><td>作られる</td><td>作られない（fast-forwardになる）</td></tr><tr><td>共有ブランチでの安全性</td><td>安全</td><td>他の人がpull済みだと事故りやすい</td></tr></tbody></table></figure>



<p class="wp-block-paragraph"><strong>mergeは事実をそのまま記録し、rebaseは記録を書き直してきれいに見せます。</strong>この違いが、そのまま使い分けの判断軸になります。</p>



<h3 class="wp-block-heading">mergeの基本</h3>



<p class="wp-block-paragraph">featureブランチの内容を、mainブランチに統合する基本の書き方です。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git checkout main
git merge feature</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">checkout</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">main</span></span>
<span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">merge</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">feature</span></span></code></pre></div>



<p class="wp-block-paragraph">分岐点からの変更をまとめる「マージコミット」が自動的に作られ、両方のブランチの履歴がそのまま残ります。</p>



<h3 class="wp-block-heading">rebaseの基本</h3>



<p class="wp-block-paragraph">featureブランチの分岐元を、mainブランチの最新コミットに付け替えます。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git checkout feature
git rebase main</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">checkout</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">feature</span></span>
<span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">rebase</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">main</span></span></code></pre></div>



<p class="wp-block-paragraph">featureブランチのコミットが1つずつ再作成され、まるで最初からmainの最新の上で作業していたかのような、一直線の履歴になります。</p>



<p class="wp-block-paragraph">コミットが再作成される以上、<strong>コミットハッシュはすべて変わります</strong>。ここがmergeとの決定的な違いです。</p>



<h3 class="wp-block-heading">使い分けの判断軸</h3>



<p class="wp-block-paragraph">判断軸はシンプルで、<strong>「そのブランチを他の人がすでに手元にpullしているかどうか」</strong>です。</p>



<div class="wp-block-group has-border -border01"><div class="wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained">
<ul class="wp-block-list">
<li>共有済みのブランチ（main等）に統合する → <strong>merge</strong>（履歴を書き換えないので安全）</li>



<li>自分専用のfeatureブランチを整理したい → <strong>rebase</strong>（誰にも影響しない）</li>
</ul>
</div></div>



<p class="wp-block-paragraph">他の人が同じブランチをpull済みの状態でrebaseすると、コミットハッシュの食い違いが起きて、次のpullで大量のコンフリクトを引き起こすことがあります。<strong>すでに公開したコミットはrebaseしない</strong>、というのがGitの基本ルールです。</p>



<h3 class="wp-block-heading">interactive rebaseでコミットを整理する</h3>



<p class="wp-block-paragraph">rebaseにはもう一つ、統合とは別の使い道があります。<strong>push前の自分のブランチのコミットを整理する</strong>用途です。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git rebase -i HEAD~3</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">rebase</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">-i</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">HEAD~3</span></span></code></pre></div>



<p class="wp-block-paragraph">エディタが開き、直近3件のコミットを一覧できます。<code>pick</code>を<code>squash</code>に書き換えると、そのコミットを1つ前にまとめられます。「動作確認用の細かいコミット」を、pushする前に意味のある単位にまとめ直すときに使います。</p>



<p class="wp-block-paragraph">この使い方は自分のブランチ内で完結するので、他の人に影響を与える心配はありません。</p>



<h2 class="wp-block-heading">実務で役立つTips</h2>



<h3 class="wp-block-heading">rebase中のコンフリクトを解消する</h3>



<p class="wp-block-paragraph">該当ファイルを開いて手動で解消し、<code>git add</code>でステージしたあと以下で再開します。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git rebase --continue</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">rebase</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">--continue</span></span></code></pre></div>



<p class="wp-block-paragraph">rebase自体をやめたい場合は、以下で開始前の状態に戻せます。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git rebase --abort</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">rebase</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">--abort</span></span></code></pre></div>



<h3 class="wp-block-heading">Squash and mergeはmerge・rebaseとは別物</h3>



<p class="wp-block-paragraph">GitHubのPull Requestでよく使う<strong>Squash and merge</strong>は、merge・rebaseのどちらとも異なる第3の統合方法です。featureブランチの全コミットを1つのコミットにまとめてmainへ追加します。</p>



<p class="wp-block-paragraph">CLIで同じことをする場合は、<code>--squash</code>オプションを使います。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git checkout main
git merge --squash feature
git commit -m "featureブランチの変更をまとめて統合"</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">checkout</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">main</span></span>
<span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">merge</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">--squash</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">feature</span></span>
<span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">commit</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">-m</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">&quot;featureブランチの変更をまとめて統合&quot;</span></span></code></pre></div>



<p class="wp-block-paragraph">通常の<code>git merge</code>と違い、マージコミットは自動生成されません。<code>--squash</code>はステージするだけなので、コミットは自分で行う必要があります。</p>



<p class="wp-block-paragraph">featureブランチ内の細かいコミットを気にせず、PR単位で1コミットとして履歴を残したい場合によく使われます。</p>



<h3 class="wp-block-heading">push済みのコミットを誤ってrebaseしてしまったときの対処</h3>



<p class="wp-block-paragraph">他の人がまだpullしていなければ、以下で上書きできます。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git push --force-with-lease</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">push</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">--force-with-lease</span></span></code></pre></div>



<p class="wp-block-paragraph">すでに誰かがpull済みの場合は、チームに連絡してrebase前の状態に揃えてもらう必要があります。</p>



<h2 class="wp-block-heading">まとめ</h2>



<p class="wp-block-paragraph">共有ブランチへの統合はmerge、自分だけのブランチの整理はrebase。「もう他の人が見ているかどうか」で選べば、事故らずに使い分けられます。</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>git cherry-pickの取り消し方法｜revert・reset・コンフリクト解消まで完全解説</title>
		<link>https://cfxlog.com/git-cherry-pick-undo-guide/</link>
		
		<dc:creator><![CDATA[CFXLOG]]></dc:creator>
		<pubDate>Fri, 27 Mar 2026 15:00:00 +0000</pubDate>
				<category><![CDATA[Git]]></category>
		<category><![CDATA[Git運用]]></category>
		<guid isPermaLink="false">https://cfxlog.com/?p=5076</guid>

					<description><![CDATA[<p><img src="https://cfxlog.com/wp-content/uploads/2026/03/git-cherry-pick-undo-guide-1024x538.jpg" class="webfeedsFeaturedVisual" /></p>cherry-pick の取り消し方法は「どのタイミングで取り消すか」によって使うコマンドが変わります。 この記事では、コンフリクト中・コミット後・push 後のそれぞれのケースで使うべきコマンドを、実際の手順と一緒に丁 [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><img src="https://cfxlog.com/wp-content/uploads/2026/03/git-cherry-pick-undo-guide-1024x538.jpg" class="webfeedsFeaturedVisual" /></p>
<div class="wp-block-group is-style-crease"><div class="wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained">
<ul class="wp-block-list">
<li>git cherry-pick を取り消したいけど、どのコマンドを使えばいいか分からない</li>

<li>コンフリクトが起きて cherry-pick が止まってしまった</li>

<li>push 後に cherry-pick したコミットを取り消したい</li>
</ul>
</div></div>



<p class="wp-block-paragraph">cherry-pick の取り消し方法は「どのタイミングで取り消すか」によって使うコマンドが変わります。</p>



<p class="wp-block-paragraph">この記事では、コンフリクト中・コミット後・push 後のそれぞれのケースで使うべきコマンドを、実際の手順と一緒に丁寧に解説します。</p>



<h2 class="wp-block-heading">取り消し方法は「タイミング」で変わる</h2>



<p class="wp-block-paragraph">cherry-pick の取り消しは、状況によって使うコマンドが3パターンに分かれます。</p>



<figure class="wp-block-table"><table><thead><tr><th>タイミング</th><th>使うコマンド</th></tr></thead><tbody><tr><td>cherry-pick 中にコンフリクトが起きた</td><td><code>git cherry-pick --abort</code></td></tr><tr><td>cherry-pick 完了後・push 前</td><td><code>git reset HEAD~1</code></td></tr><tr><td>push 後</td><td><code>git revert &lt;コミットID&gt;</code></td></tr></tbody></table></figure>



<p class="wp-block-paragraph">これを頭に入れておくと、焦らず対処できます。それぞれ詳しく見ていきましょう。</p>



<h2 class="wp-block-heading">コンフリクト中に取り消す：git cherry-pick &#8211;abort</h2>



<p class="wp-block-paragraph">cherry-pick を実行したときにコンフリクトが発生すると、Git は処理を一時停止して次のようなメッセージを表示します。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>CONFLICT (content): Merge conflict in src/main.py
error: could not apply a1b2c3d... fix: ログ出力を修正
hint: After resolving the conflicts, mark them with
hint: "git add/rm &lt;pathspec&gt;", then run
hint: "git cherry-pick --continue".
hint: You can instead skip this commit: run "git cherry-pick --skip".
hint: To abort and get back to the state before "cherry-pick",
hint: run "git cherry-pick --abort".</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color:#1E1E1E;color:#D4D4D4" tabindex="0"><code><span class="line"><span style="color:#DCDCAA">CONFLICT</span><span style="color:#D4D4D4"> (content): Merge conflict in src/main.py</span></span>
<span class="line"><span style="color:#DCDCAA">error:</span><span style="color:#CE9178"> could</span><span style="color:#CE9178"> not</span><span style="color:#CE9178"> apply</span><span style="color:#CE9178"> a1b2c3d...</span><span style="color:#CE9178"> fix:</span><span style="color:#CE9178"> ログ出力を修正</span></span>
<span class="line"><span style="color:#DCDCAA">hint:</span><span style="color:#CE9178"> After</span><span style="color:#CE9178"> resolving</span><span style="color:#CE9178"> the</span><span style="color:#CE9178"> conflicts,</span><span style="color:#CE9178"> mark</span><span style="color:#CE9178"> them</span><span style="color:#CE9178"> with</span></span>
<span class="line"><span style="color:#DCDCAA">hint:</span><span style="color:#CE9178"> "git add/rm &lt;pathspec>",</span><span style="color:#CE9178"> then</span><span style="color:#CE9178"> run</span></span>
<span class="line"><span style="color:#DCDCAA">hint:</span><span style="color:#CE9178"> "git cherry-pick --continue".</span></span>
<span class="line"><span style="color:#DCDCAA">hint:</span><span style="color:#CE9178"> You</span><span style="color:#CE9178"> can</span><span style="color:#CE9178"> instead</span><span style="color:#CE9178"> skip</span><span style="color:#CE9178"> this</span><span style="color:#CE9178"> commit:</span><span style="color:#CE9178"> run</span><span style="color:#CE9178"> "git cherry-pick --skip".</span></span>
<span class="line"><span style="color:#DCDCAA">hint:</span><span style="color:#CE9178"> To</span><span style="color:#CE9178"> abort</span><span style="color:#CE9178"> and</span><span style="color:#CE9178"> get</span><span style="color:#CE9178"> back</span><span style="color:#CE9178"> to</span><span style="color:#CE9178"> the</span><span style="color:#CE9178"> state</span><span style="color:#CE9178"> before</span><span style="color:#CE9178"> "cherry-pick",</span></span>
<span class="line"><span style="color:#DCDCAA">hint:</span><span style="color:#CE9178"> run</span><span style="color:#CE9178"> "git cherry-pick --abort".</span></span></code></pre></div>



<p class="wp-block-paragraph">この状態で「やっぱりこの cherry-pick 自体をなかったことにしたい」という場合は、次のコマンドを実行します。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git cherry-pick --abort</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color:#1E1E1E;color:#D4D4D4" tabindex="0"><code><span class="line"><span style="color:#DCDCAA">git</span><span style="color:#CE9178"> cherry-pick</span><span style="color:#569CD6"> --abort</span></span></code></pre></div>



<p class="wp-block-paragraph">このコマンドを実行すると、cherry-pick を始める前の状態にきれいに戻ります。作業中のファイルへの変更もすべて元に戻るので安心です。</p>



<h3 class="wp-block-heading">&#8211;abort が使えるのはコンフリクト中だけ</h3>



<p class="wp-block-paragraph">注意点として、<code>--abort</code> が有効なのは cherry-pick がコンフリクトで止まっている最中だけです。</p>



<p class="wp-block-paragraph">コンフリクト中のステータスは次のように確認できます。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>$ git status
On branch main
You are currently cherry-picking commit a1b2c3d.
  (fix conflicts and run "git cherry-pick --continue")
  (use "git cherry-pick --skip" to skip this patch)
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

Unmerged paths:
  (use "git add &lt;file&gt;..." to mark resolution)
	both modified:   src/main.py</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color:#1E1E1E;color:#D4D4D4" tabindex="0"><code><span class="line"><span style="color:#DCDCAA">$</span><span style="color:#CE9178"> git</span><span style="color:#CE9178"> status</span></span>
<span class="line"><span style="color:#DCDCAA">On</span><span style="color:#CE9178"> branch</span><span style="color:#CE9178"> main</span></span>
<span class="line"><span style="color:#DCDCAA">You</span><span style="color:#CE9178"> are</span><span style="color:#CE9178"> currently</span><span style="color:#CE9178"> cherry-picking</span><span style="color:#CE9178"> commit</span><span style="color:#CE9178"> a1b2c3d.</span></span>
<span class="line"><span style="color:#D4D4D4">  (</span><span style="color:#DCDCAA">fix</span><span style="color:#CE9178"> conflicts</span><span style="color:#CE9178"> and</span><span style="color:#CE9178"> run</span><span style="color:#CE9178"> "git cherry-pick --continue"</span><span style="color:#D4D4D4">)</span></span>
<span class="line"><span style="color:#D4D4D4">  (</span><span style="color:#DCDCAA">use</span><span style="color:#CE9178"> "git cherry-pick --skip"</span><span style="color:#CE9178"> to</span><span style="color:#CE9178"> skip</span><span style="color:#CE9178"> this</span><span style="color:#CE9178"> patch</span><span style="color:#D4D4D4">)</span></span>
<span class="line"><span style="color:#D4D4D4">  (</span><span style="color:#DCDCAA">use</span><span style="color:#CE9178"> "git cherry-pick --abort"</span><span style="color:#CE9178"> to</span><span style="color:#CE9178"> cancel</span><span style="color:#CE9178"> the</span><span style="color:#CE9178"> cherry-pick</span><span style="color:#CE9178"> operation</span><span style="color:#D4D4D4">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#DCDCAA">Unmerged</span><span style="color:#CE9178"> paths:</span></span>
<span class="line"><span style="color:#D4D4D4">  (</span><span style="color:#DCDCAA">use</span><span style="color:#CE9178"> "git add &lt;file>..."</span><span style="color:#CE9178"> to</span><span style="color:#CE9178"> mark</span><span style="color:#CE9178"> resolution</span><span style="color:#D4D4D4">)</span></span>
<span class="line"><span style="color:#DCDCAA">	both</span><span style="color:#CE9178"> modified:</span><span style="color:#CE9178">   src/main.py</span></span></code></pre></div>



<p class="wp-block-paragraph">「You are currently cherry-picking commit」と表示されていれば、<code>--abort</code> が使える状態です。</p>



<p class="wp-block-paragraph">cherry-pick が正常に完了してコミットが作られた後では、<code>--abort</code> は使えません。その場合は後述の git reset を使います。</p>



<h2 class="wp-block-heading">コンフリクトを解消してチェリーピックを完了させる</h2>



<p class="wp-block-paragraph">「取り消すのではなく、コンフリクトを直して cherry-pick を完了させたい」という場合の手順も押さえておきましょう。</p>



<h3 class="wp-block-heading">&#8211;continue でチェリーピックを再開する</h3>



<p class="wp-block-paragraph">コンフリクトを解消した後は、変更をステージに追加してから次のコマンドを実行します。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly># 1. コンフリクトしたファイルを修正する
vim src/main.py

# 2. 修正したファイルをステージに追加
git add src/main.py

# 3. cherry-pick を再開
git cherry-pick --continue</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color:#1E1E1E;color:#D4D4D4" tabindex="0"><code><span class="line"><span style="color:#6A9955"># 1. コンフリクトしたファイルを修正する</span></span>
<span class="line"><span style="color:#DCDCAA">vim</span><span style="color:#CE9178"> src/main.py</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6A9955"># 2. 修正したファイルをステージに追加</span></span>
<span class="line"><span style="color:#DCDCAA">git</span><span style="color:#CE9178"> add</span><span style="color:#CE9178"> src/main.py</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6A9955"># 3. cherry-pick を再開</span></span>
<span class="line"><span style="color:#DCDCAA">git</span><span style="color:#CE9178"> cherry-pick</span><span style="color:#569CD6"> --continue</span></span></code></pre></div>



<p class="wp-block-paragraph">実行するとコミットメッセージの入力画面が開きます。そのまま保存すれば cherry-pick が完了します。</p>



<h3 class="wp-block-heading">&#8211;skip でそのコミットだけ飛ばす</h3>



<p class="wp-block-paragraph">複数コミットを cherry-pick している途中で、特定のコミットだけをスキップしたい場合は次のコマンドを使います。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git cherry-pick --skip</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color:#1E1E1E;color:#D4D4D4" tabindex="0"><code><span class="line"><span style="color:#DCDCAA">git</span><span style="color:#CE9178"> cherry-pick</span><span style="color:#569CD6"> --skip</span></span></code></pre></div>



<p class="wp-block-paragraph">コンフリクトが起きたコミットを適用せずに次のコミットへ進みます。そのコミットの変更を捨てるという選択肢です。</p>



<h2 class="wp-block-heading">コミット後・push 前に取り消す：git reset</h2>



<p class="wp-block-paragraph">git cherry-pick 後のコミットを取り消す代表的な方法が <code>git reset</code> です。</p>



<p class="wp-block-paragraph">cherry-pick が正常に完了してコミットが作られたけど、push する前に「やっぱりやめたい」という場合に使います。</p>



<p class="wp-block-paragraph"><strong>ポイント：</strong><code>git reset</code> は「コミット履歴を巻き戻すコマンド」です。そのため、すでに共有された履歴に対して使うと他の人に影響を与える可能性があります。</p>



<h3 class="wp-block-heading">コミットだけ消してファイルの変更は残す（&#8211;soft / &#8211;mixed）</h3>



<p class="wp-block-paragraph">cherry-pick で作ったコミットを取り消しつつ、ファイルの変更内容は手元に残したい場合は次のコマンドを使います。</p>



<p class="wp-block-paragraph">コマンド中の <code>HEAD~1</code> は「1つ前のコミット」を意味します。cherry-pick で作ったコミットが直前の1件であれば、このまま使えます。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly># --soft: コミットを取り消す。変更はステージに残る
git reset --soft HEAD~1

# --mixed（デフォルト）: コミットを取り消す。変更はワーキングツリーに残る
git reset HEAD~1</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color:#1E1E1E;color:#D4D4D4" tabindex="0"><code><span class="line"><span style="color:#6A9955"># --soft: コミットを取り消す。変更はステージに残る</span></span>
<span class="line"><span style="color:#DCDCAA">git</span><span style="color:#CE9178"> reset</span><span style="color:#569CD6"> --soft</span><span style="color:#CE9178"> HEAD~1</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6A9955"># --mixed（デフォルト）: コミットを取り消す。変更はワーキングツリーに残る</span></span>
<span class="line"><span style="color:#DCDCAA">git</span><span style="color:#CE9178"> reset</span><span style="color:#CE9178"> HEAD~1</span></span></code></pre></div>



<h3 class="wp-block-heading">コミットもファイルの変更も完全に消す（&#8211;hard）</h3>



<p class="wp-block-paragraph">cherry-pick で取り込んだ変更そのものが不要で、完全に元に戻したい場合は次のコマンドを使います。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git reset --hard HEAD~1</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color:#1E1E1E;color:#D4D4D4" tabindex="0"><code><span class="line"><span style="color:#DCDCAA">git</span><span style="color:#CE9178"> reset</span><span style="color:#569CD6"> --hard</span><span style="color:#CE9178"> HEAD~1</span></span></code></pre></div>



<p class="wp-block-paragraph"><strong>注意：</strong>このオプションはファイルの変更も含めて完全に削除します。<strong>取り消しは非常に困難</strong>なので、本当に消していいか必ず確認してから実行してください。</p>



<h3 class="wp-block-heading">&#8211;soft / &#8211;mixed / &#8211;hard の違い一覧</h3>



<figure class="wp-block-table"><table><thead><tr><th>オプション</th><th>コミット</th><th>ステージ（index）</th><th>ワーキングツリー</th></tr></thead><tbody><tr><td><code>--soft</code></td><td>取り消す</td><td>変更が残る</td><td>変更が残る</td></tr><tr><td><code>--mixed</code>（デフォルト）</td><td>取り消す</td><td>変更が消える</td><td>変更が残る</td></tr><tr><td><code>--hard</code></td><td>取り消す</td><td>変更が消える</td><td>変更が消える</td></tr></tbody></table></figure>



<p class="wp-block-paragraph"><strong>補足：</strong>もし <code>reset</code> 後にすでに push 済みの履歴を上書きしたい場合は <code>git push --force</code> が必要になります。ただし、他のメンバーの履歴を壊す可能性があるため、チーム開発では基本的に避けましょう。</p>



<h2 class="wp-block-heading">push 後に取り消す：git revert</h2>



<p class="wp-block-paragraph">リモートリポジトリへ push してしまった後でも <code>git reset</code> 自体は使えますが、履歴を書き換えるため <code>git push --force</code> が必要になります。</p>



<p class="wp-block-paragraph">これは他のメンバーの履歴を壊す可能性があるため、チーム開発では基本的に避け、<code>git revert</code> を使うのが安全です。</p>



<h3 class="wp-block-heading">git revert の使い方</h3>



<p class="wp-block-paragraph">まず、取り消したい cherry-pick のコミット ID を確認します。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git log --oneline</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color:#1E1E1E;color:#D4D4D4" tabindex="0"><code><span class="line"><span style="color:#DCDCAA">git</span><span style="color:#CE9178"> log</span><span style="color:#569CD6"> --oneline</span></span></code></pre></div>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>a1b2c3d (HEAD -&gt; main) fix: ログ出力を修正  ← cherry-pick したコミット
e4f5g6h feat: 新機能を追加
7i8j9k0 docs: READMEを更新</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color:#1E1E1E;color:#D4D4D4" tabindex="0"><code><span class="line"><span style="color:#DCDCAA">a1b2c3d</span><span style="color:#D4D4D4"> (HEAD -> </span><span style="color:#CE9178">main</span><span style="color:#D4D4D4">) fix: ログ出力を修正  ← cherry-pick したコミット</span></span>
<span class="line"><span style="color:#DCDCAA">e4f5g6h</span><span style="color:#CE9178"> feat:</span><span style="color:#CE9178"> 新機能を追加</span></span>
<span class="line"><span style="color:#DCDCAA">7i8j9k0</span><span style="color:#CE9178"> docs:</span><span style="color:#CE9178"> READMEを更新</span></span></code></pre></div>



<p class="wp-block-paragraph">コミット ID が確認できたら次のコマンドを実行します。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git revert a1b2c3d</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color:#1E1E1E;color:#D4D4D4" tabindex="0"><code><span class="line"><span style="color:#DCDCAA">git</span><span style="color:#CE9178"> revert</span><span style="color:#CE9178"> a1b2c3d</span></span></code></pre></div>



<p class="wp-block-paragraph">コミットメッセージの入力画面が開きます。そのまま保存すると「Revert &#8220;fix: ログ出力を修正&#8221;」というコミットが新たに作られ、cherry-pick の変更が打ち消されます。</p>



<p class="wp-block-paragraph">あとは通常どおり push すれば完了です。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git push origin main</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color:#1E1E1E;color:#D4D4D4" tabindex="0"><code><span class="line"><span style="color:#DCDCAA">git</span><span style="color:#CE9178"> push</span><span style="color:#CE9178"> origin</span><span style="color:#CE9178"> main</span></span></code></pre></div>



<h3 class="wp-block-heading">git revert の特徴：履歴が残る</h3>



<p class="wp-block-paragraph">このコマンドは既存のコミットを書き換えるのではなく、「変更を打ち消す新しいコミット」を追加します。</p>



<p class="wp-block-paragraph">そのためコミット履歴がきれいに残り、チームの他のメンバーに影響を与えません。push 後の取り消しにはこちらが安全です。</p>



<p class="wp-block-paragraph"><strong>注意：</strong><code>git revert</code> でもコンフリクトが発生する場合があります。その場合は通常のマージと同様にコンフリクトを解消してからコミットします。</p>



<h3 class="wp-block-heading">コミットメッセージ入力をスキップしたい場合</h3>



<p class="wp-block-paragraph">エディタを開かずにデフォルトのメッセージでコミットしたい場合は、次のように実行します。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git revert a1b2c3d --no-edit</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color:#1E1E1E;color:#D4D4D4" tabindex="0"><code><span class="line"><span style="color:#DCDCAA">git</span><span style="color:#CE9178"> revert</span><span style="color:#CE9178"> a1b2c3d</span><span style="color:#569CD6"> --no-edit</span></span></code></pre></div>



<h2 class="wp-block-heading">まとめ：シーン別コマンド早見表</h2>



<p class="wp-block-paragraph">cherry-pick の取り消し方法を状況別にまとめます。</p>



<figure class="wp-block-table"><table><thead><tr><th>状況</th><th>コマンド</th><th>備考</th></tr></thead><tbody><tr><td>コンフリクト中・取り消したい</td><td><code>git cherry-pick --abort</code></td><td>cherry-pick 前の状態に戻る</td></tr><tr><td>コンフリクト中・解消して続けたい</td><td><code>git add</code> → <code>git cherry-pick --continue</code></td><td>ファイル修正後にステージへ追加</td></tr><tr><td>コンフリクト中・そのコミットだけ飛ばしたい</td><td><code>git cherry-pick --skip</code></td><td>変更は適用されない</td></tr><tr><td>コミット後・push 前（変更を残す）</td><td><code>git reset --soft HEAD~1</code></td><td>変更はステージに残る</td></tr><tr><td>コミット後・push 前（完全に消す）</td><td><code>git reset --hard HEAD~1</code></td><td>変更も完全削除（注意）</td></tr><tr><td>push 後</td><td><code>git revert &lt;コミットID&gt;</code></td><td>打ち消しコミットを追加</td></tr></tbody></table></figure>



<h3 class="wp-block-heading">どれを使うべきか迷ったら</h3>



<p class="wp-block-paragraph">判断基準はシンプルです。</p>



<div class="wp-block-group has-border -border01"><div class="wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained">
<ul class="wp-block-list">
<li>履歴を書き換えてもいい（push 前）→ <code>git reset</code></li>

<li>履歴を残したい（push 済み）→ <code>git revert</code></li>
</ul>
</div></div>



<p class="wp-block-paragraph">チーム開発では基本的に <code>revert</code> を使うと覚えておくと安全です。</p>



<p class="wp-block-paragraph">「取り消したいのはどのタイミングか」を最初に確認して、適切なコマンドを選ぶようにしましょう。</p>



<p class="wp-block-paragraph">「そもそも cherry-pick の使い方が曖昧」という方は、先にこちらの記事を読むのがおすすめです。</p>


<div class="swell-block-postLink">			<div class="p-blogCard -internal" data-type="type1" data-onclick="clickLink">
				<div class="p-blogCard__inner">
					<span class="p-blogCard__caption">あわせて読みたい</span>
					<div class="p-blogCard__thumb c-postThumb"><figure class="c-postThumb__figure"><img decoding="async" src="https://cfxlog.com/wp-content/uploads/2026/03/git-cherry-pick-guide-300x158.jpg" alt="" class="c-postThumb__img u-obf-cover" width="320" height="180"></figure></div>					<div class="p-blogCard__body">
						<a class="p-blogCard__title" href="https://cfxlog.com/git-cherry-pick-guide/">チェリーピックとは？git cherry-pickの意味と使い方</a>
						<span class="p-blogCard__excerpt">職場で「プッシュしたから、チェリーピックしておいて」と言われた。…ところで、チェリーピックってなに？ このような方に向けて書きました。 Gitで開発をしていると、&#8230;</span>					</div>
				</div>
			</div>
		</div>]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>【簡単】macOSにGitをセットアップする方法を基礎から解説</title>
		<link>https://cfxlog.com/mac-git-setup/</link>
		
		<dc:creator><![CDATA[CFXLOG]]></dc:creator>
		<pubDate>Mon, 26 Jan 2026 15:00:00 +0000</pubDate>
				<category><![CDATA[Mac]]></category>
		<category><![CDATA[Git運用]]></category>
		<category><![CDATA[環境構築]]></category>
		<guid isPermaLink="false">https://cfxlog.com/?p=4910</guid>

					<description><![CDATA[<p><img src="https://cfxlog.com/wp-content/uploads/2026/03/mac-git-setup.jpg" class="webfeedsFeaturedVisual" /></p>このような方に向けて書きました。 プログラミングを独学でやっていると、「どうやら実務ではGitというものがあるらしい」ということは風の噂で知ることになります。 就職には必須と知りながらも、自宅学習ではなかなかGitHub [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><img src="https://cfxlog.com/wp-content/uploads/2026/03/mac-git-setup.jpg" class="webfeedsFeaturedVisual" /></p>
<div class="wp-block-group is-style-crease"><div class="wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained">
<ul class="wp-block-list">
<li>GitとGitHubって何が違うの？</li>



<li>macOSにGitHubを導入したい！</li>
</ul>
</div></div>



<p class="wp-block-paragraph">このような方に向けて書きました。</p>



<p class="wp-block-paragraph">プログラミングを独学でやっていると、「<strong>どうやら実務ではGitというものがあるらしい</strong>」ということは風の噂で知ることになります。</p>



<p class="wp-block-paragraph">就職には必須と知りながらも、自宅学習ではなかなかGitHubを使うきっかけもないかと思います。（なくても何とかなってしまう）</p>



<p class="wp-block-paragraph">そこで本記事では、以下の内容を解説します。</p>



<div class="wp-block-group has-border -border04"><div class="wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained">
<ul class="wp-block-list">
<li>GitとGitHubを丁寧に解説</li>



<li>Gitのインストール方法</li>



<li>Gitに必要な初期設定方法</li>



<li>GitHubへの登録方法</li>
</ul>
</div></div>



<p class="wp-block-paragraph">Gitは奥が深いので、最初から全部理解しようとしないでください。</p>



<p class="wp-block-paragraph">まずは本記事で解説することを理解していただければ、「全く知らない」というレベルからは脱出できるはずです。</p>



<h2 class="wp-block-heading">GitとGitHubを解説</h2>



<p class="wp-block-paragraph">初学の頃は、GitとGitHubの違いがよく理解できないかもしれません。</p>



<p class="wp-block-paragraph">ここでは両者の内容と違いを解説します。</p>



<p class="is-style-icon_announce wp-block-paragraph">すでにご存知の方は、<strong><a href="#how-to-install">こちら</a></strong>からインストール方法に進んでください。</p>



<h3 class="wp-block-heading">Gitは変更履歴の保存ツール</h3>



<p class="wp-block-paragraph">Gitはソースコードの変更履歴を保存しておくツールです。</p>



<p class="wp-block-paragraph">何度もソースコードを変更して、その度にバックアップを取るのはとても大変な作業。</p>



<p class="wp-block-paragraph">たとえば毎日作業をして、バックアップを取るならこうなります。</p>



<figure class="wp-block-image size-full"><img decoding="async" width="753" height="179" src="https://cfxlog.com/wp-content/uploads/2026/03/mac-git-setup-1.png" alt="" class="wp-image-4914" srcset="https://cfxlog.com/wp-content/uploads/2026/03/mac-git-setup-1.png 753w, https://cfxlog.com/wp-content/uploads/2026/03/mac-git-setup-1-300x71.png 300w" sizes="(max-width: 753px) 100vw, 753px" /></figure>



<p class="wp-block-paragraph">これでは日を追うごとにフォルダがどんどん増えて煩雑になりますし、何をどうやって変更したのかが全くわかりません。</p>



<p class="wp-block-paragraph">Gitを使うと、以下のことができます。</p>



<div class="wp-block-group has-border -border01"><div class="wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained">
<ul class="wp-block-list">
<li>誰が、いつ、どこを変更したか一目でわかる</li>



<li>変更箇所の差分がわかりやすい</li>



<li>共同作業ができる</li>



<li>数クリックで過去の状態に戻せる</li>
</ul>
</div></div>



<p class="wp-block-paragraph">特にチームでGitを使わないバージョン管理をしようとすると、地獄を見ます。</p>



<p class="wp-block-paragraph">メリットが多すぎるツールなので、個人開発であっても積極的に使いたいものです。</p>



<h3 class="wp-block-heading">Gitをオンラインで使えるようにするGitHub</h3>



<p class="wp-block-paragraph">GitHubは、Gitをオンラインでも使えるようにできるサービスです。</p>



<p class="wp-block-paragraph">最初のうちは単純化して「Gitを使ってプログラミングコードをアップロードできるスペース」程度に考えておくと理解が早いかもしれません。</p>



<p class="wp-block-paragraph">GitHubを使うとコードがオンライン上に保存されるので、これを起点としてチーム開発もできるようになります。</p>


<div class="swell-block-balloon"><div class="c-balloon -bln-left" data-col="green"><div class="c-balloon__icon -square"><img decoding="async" src="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" data-src="https://cfxlog.com/wp-content/uploads/2026/07/01-150x150-2.jpg" alt="" class="lazyload c-balloon__iconImg" width="80px" height="80px"><noscript><img decoding="async" src="https://cfxlog.com/wp-content/uploads/2026/07/01-150x150-2.jpg" alt="" class="c-balloon__iconImg" width="80px" height="80px"></noscript><span class="c-balloon__iconName">CFXLOG</span></div><div class="c-balloon__body -speaking -border-none"><div class="c-balloon__text">
<p>他にもプルリクエストなど便利な機能もありますが、おいおい覚えていけば大丈夫です！</p>
<span class="c-balloon__shapes"><span class="c-balloon__before"></span><span class="c-balloon__after"></span></span></div></div></div></div>


<h3 class="wp-block-heading">GitとGitHubの関係性</h3>



<p class="wp-block-paragraph">Gitはソースコードの変更履歴を管理するツールにすぎません。</p>



<p class="wp-block-paragraph">このGitという仕組みを使って、インターネット上にコードを保管したり共有できるようにしたのがGitHubということになります。</p>



<p class="wp-block-paragraph">Gitはインターネット上で管理できることで、100%の力を出すことができます。</p>



<p class="wp-block-paragraph">そのため、Git単独ではなくGitHubと併用して使われることがほとんどです。</p>


<div class="swell-block-balloon"><div class="c-balloon -bln-left" data-col="green"><div class="c-balloon__icon -square"><img decoding="async" src="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" data-src="https://cfxlog.com/wp-content/uploads/2026/07/01-150x150-2.jpg" alt="" class="lazyload c-balloon__iconImg" width="80px" height="80px"><noscript><img decoding="async" src="https://cfxlog.com/wp-content/uploads/2026/07/01-150x150-2.jpg" alt="" class="c-balloon__iconImg" width="80px" height="80px"></noscript><span class="c-balloon__iconName">CFXLOG</span></div><div class="c-balloon__body -speaking -border-none"><div class="c-balloon__text">
<p>本記事では、この二つの環境を構築する方法をお伝えします！</p>
<span class="c-balloon__shapes"><span class="c-balloon__before"></span><span class="c-balloon__after"></span></span></div></div></div></div>


<h2 class="wp-block-heading" id="how-to-install">Gitをインストールする方法</h2>



<p class="wp-block-paragraph">GitHubを使うためには、Gitのインストールが必須です。</p>



<p class="wp-block-paragraph">ここではmacOS向けのインストール方法を解説します。</p>



<h3 class="wp-block-heading">Gitがインストール済みか確認</h3>



<p class="wp-block-paragraph">すでにGitがインストールされている場合もあります。</p>



<p class="wp-block-paragraph">以下のコマンドでGitが入っているか確認しましょう。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>% git --version
# git version 2.39.5 (Apple Git-154) ← インストール済み
# git: command not found ← まだインストールされていない</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">%</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">git</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">--version</span></span>
<span class="line"><span style="color: #6A9955"># git version 2.39.5 (Apple Git-154) ← インストール済み</span></span>
<span class="line"><span style="color: #6A9955"># git: command not found ← まだインストールされていない</span></span></code></pre></div>



<p class="wp-block-paragraph">上記のようにバージョンが返ってくれば、インストール済みです。</p>



<p class="wp-block-paragraph">バージョンが返ってこなければ、Gitが入っていないので次のステップに進みましょう。</p>



<p class="is-style-icon_announce wp-block-paragraph">インストール済みの方は、<strong><a href="#git-initial-settings">こちら</a></strong>からGitの初期設定に進んでください。</p>



<h3 class="wp-block-heading">GitをHomebrewでインストールする</h3>



<p class="wp-block-paragraph">Gitをインストールするには、Homebrewというパッケージ管理ソフトが必要です。</p>



<p class="wp-block-paragraph">以下のコマンドでHomebrewをインストールしましょう。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">/bin/bash</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">-c</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">&quot;$(</span><span style="color: #DCDCAA">curl</span><span style="color: #CE9178"> </span><span style="color: #569CD6">-fsSL</span><span style="color: #CE9178"> https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)&quot;</span></span></code></pre></div>



<p class="wp-block-paragraph">Homebrewのインストールが終わったら、Gitをインストールします。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>brew install git</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">brew</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">install</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">git</span></span></code></pre></div>



<p class="wp-block-paragraph">最後にGitがインストールされたかを<code>--version</code>コマンドで確認します。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>% git --version</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">%</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">git</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">--version</span></span></code></pre></div>



<p class="wp-block-paragraph">バージョンが返ってくれば、Gitのインストールは成功です。</p>



<h2 class="wp-block-heading" id="git-initial-settings">Gitの初期設定</h2>



<p class="wp-block-paragraph">Gitの初期設定を進めましょう。</p>



<p class="wp-block-paragraph">Gitでは「<strong>誰がその操作を行なったか</strong>」が重要なので、初期設定として個人に紐づく情報を登録します。</p>



<h3 class="wp-block-heading">ユーザー名を設定</h3>



<p class="wp-block-paragraph">まずはユーザー名を設定しましょう。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git config --global user.name "Your Name"</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">config</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">--global</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">user.name</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">&quot;Your Name&quot;</span></span></code></pre></div>



<p class="wp-block-paragraph">ここで設定されるユーザー名は、誰によるコミットかを識別するためのものです。</p>



<p class="wp-block-paragraph">そのため本名ではなくニックネームでの登録も可能です。</p>



<p class="wp-block-paragraph">ただしこのユーザー名は誰でも見られます。</p>



<p class="wp-block-paragraph">そのため、見られて恥ずかしかったり困る名前はつけないようにしてください。</p>



<h3 class="wp-block-heading">メールアドレスの設定</h3>



<p class="wp-block-paragraph">GitHubではユーザーとメールアドレスが紐づけられているので、必ず設定します。</p>



<p class="wp-block-paragraph">以下のコマンドでメールアドレスを登録しましょう。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git config --global user.email "youremail@example.com"</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">config</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">--global</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">user.email</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">&quot;youremail@example.com&quot;</span></span></code></pre></div>



<p class="wp-block-paragraph">ユーザー名と同様に、メールアドレスも他人の目に触れることもあります。</p>



<p class="wp-block-paragraph">それが嫌な場合には、GitHubが用意しているプライバシー保護用のメールアドレスを使うこともできます。</p>



<p class="wp-block-paragraph">詳しい設定方法などは、後日別記事でご紹介予定です。</p>



<p class="wp-block-paragraph"><span style="text-decoration: underline;">» 参考：GitHubでプライバシー保護用のメールアドレスを設定する方法（準備中）</span></p>



<h3 class="wp-block-heading">登録した内容を確認する方法</h3>



<p class="wp-block-paragraph">登録内容を確認するには、以下のコマンドを実行します。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git config --global --list
# user.name=Your Name
# user.email=john.doe@example.com</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">config</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">--global</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">--list</span></span>
<span class="line"><span style="color: #6A9955"># user.name=Your Name</span></span>
<span class="line"><span style="color: #6A9955"># user.email=john.doe@example.com</span></span></code></pre></div>



<p class="wp-block-paragraph">設定が成功していれば、先ほど入力した情報が返ってくるはずです。</p>



<h2 class="wp-block-heading">GitHubの設定</h2>



<p class="wp-block-paragraph">最後にGitHubの設定を行いましょう。</p>



<p class="wp-block-paragraph">ここではアカウントの登録と、SSH接続の設定ができればOKです。</p>



<h3 class="wp-block-heading">GitHubでユーザー登録する</h3>



<p class="wp-block-paragraph"><a href="https://github.com/"><strong>GitHubのトップページ</strong></a><span data-icon="LsExternalLink" data-id="13" style="--the-icon-svg: url(data:image/svg+xml;base64,PHN2ZyBoZWlnaHQ9IjFlbSIgd2lkdGg9IjFlbSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiBhcmlhLWhpZGRlbj0idHJ1ZSIgdmlld0JveD0iMCAwIDQ4IDQ4Ij48cGF0aCBkPSJNNDQgMkgzMGMtMS4xIDAtMiAuOS0yIDJzLjkgMiAyIDJoOS4yTDI0IDIxLjJjLS44LjgtLjggMiAwIDIuOHMyIC44IDIuOCAwTDQyIDguOFYxOGMwIDEuMS45IDIgMiAyczItLjkgMi0yVjRjMC0xLjEtLjktMi0yLTJ6Ij48L3BhdGg+PHBhdGggZD0iTTQxIDI3Yy0xLjEgMC0yIC45LTIgMnYxMGMwIDEuMS0uOSAyLTIgMkg5Yy0xLjEgMC0yLS45LTItMlYxMWMwLTEuMS45LTIgMi0yaDEwYzEuMSAwIDItLjkgMi0ycy0uOS0yLTItMkg5Yy0zLjMgMC02IDIuNy02IDZ2MjhjMCAzLjMgMi43IDYgNiA2aDI4YzMuMyAwIDYtMi43IDYtNlYyOWMwLTEuMS0uOS0yLTItMnoiPjwvcGF0aD48L3N2Zz4=)" aria-hidden="true" class="swl-inline-icon"> </span>の右上にあるSign Upボタンをクリックします。</p>



<figure class="wp-block-image size-full"><img decoding="async" width="1014" height="439" src="https://cfxlog.com/wp-content/uploads/2026/03/mac-git-setup-2.png" alt="" class="wp-image-4915" srcset="https://cfxlog.com/wp-content/uploads/2026/03/mac-git-setup-2.png 1014w, https://cfxlog.com/wp-content/uploads/2026/03/mac-git-setup-2-300x130.png 300w, https://cfxlog.com/wp-content/uploads/2026/03/mac-git-setup-2-768x332.png 768w" sizes="(max-width: 1014px) 100vw, 1014px" /></figure>



<p class="wp-block-paragraph">すると、以下のような入力フォームが表示されます。</p>



<figure class="wp-block-image size-full is-resized"><img decoding="async" width="720" height="261" src="https://cfxlog.com/wp-content/uploads/2026/03/mac-git-setup-3.png" alt="" class="wp-image-4916" style="width:750px;height:auto" srcset="https://cfxlog.com/wp-content/uploads/2026/03/mac-git-setup-3.png 720w, https://cfxlog.com/wp-content/uploads/2026/03/mac-git-setup-3-300x109.png 300w" sizes="(max-width: 720px) 100vw, 720px" /></figure>



<p class="wp-block-paragraph">メールアドレスやパスワード、ユーザー名などをどうするか聞かれるので、それぞれ入力して進めていきます。</p>



<p class="wp-block-paragraph">そこまで難しいことは聞かれないので、アカウントの作成は簡単にできるかと思います。</p>



<h3 class="wp-block-heading">SSH接続でサクサク開発する</h3>



<p class="wp-block-paragraph">GitHubの操作時には、GitHubの認証を通過する必要があります。</p>



<p class="wp-block-paragraph">認証方式は次の2種類です。</p>



<div class="wp-block-group has-border -border04"><div class="wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained">
<ul class="wp-block-list is-style-num_circle">
<li>SSH接続</li>



<li>パーソナルアクセストークン</li>
</ul>
</div></div>



<p class="wp-block-paragraph">どちらでも問題なく認証はできますが、<strong>おすすめはSSH接続</strong>です。</p>



<p class="wp-block-paragraph">なぜならパーソナルアクセストークンでは有効期限のたびにトークンの再発行が必要なので、結構手間がかかるからです。</p>



<p class="wp-block-paragraph">一方のSSH接続は初回は少し手間がかかるものの、最初に設定すれば再設定の必要がありません。</p>



<h2 class="wp-block-heading">まとめ</h2>



<p class="wp-block-paragraph">本記事ではGit / GitHubの基本と、セットアップ方法をお伝えしてきました。</p>



<p class="wp-block-paragraph">GitとGitHubを組み合わせて使うことで、ソースコードをオンライン上に保存できるようになり大変便利です。</p>



<p class="wp-block-paragraph">GitHubのセットアップができたら、次はGitによる操作を学んでいきましょう。</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>【基本】CUI を使った Git の基本操作</title>
		<link>https://cfxlog.com/git-basics-cui/</link>
		
		<dc:creator><![CDATA[CFXLOG]]></dc:creator>
		<pubDate>Wed, 24 Dec 2025 15:00:00 +0000</pubDate>
				<category><![CDATA[Git]]></category>
		<category><![CDATA[CLIツール]]></category>
		<category><![CDATA[Git運用]]></category>
		<guid isPermaLink="false">https://cfxlog.com/?p=4941</guid>

					<description><![CDATA[<p><img src="https://cfxlog.com/wp-content/uploads/2026/03/git-basics-cui.jpg" class="webfeedsFeaturedVisual" /></p>独学でプログラミングを始めると、Git はとても身につけにくい技術です。 Git はとても便利な反面、処理のフローが複雑で初学者にとって複雑怪奇。 さらに個人開発では Git なしでもどうにかなってしまうので、学習が後回 [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><img src="https://cfxlog.com/wp-content/uploads/2026/03/git-basics-cui.jpg" class="webfeedsFeaturedVisual" /></p>
<p class="wp-block-paragraph">独学でプログラミングを始めると、Git はとても身につけにくい技術です。</p>



<p class="wp-block-paragraph">Git はとても便利な反面、処理のフローが複雑で初学者にとって複雑怪奇。</p>



<p class="wp-block-paragraph">さらに個人開発では Git なしでもどうにかなってしまうので、学習が後回しになってしまいがちです。</p>



<p class="wp-block-paragraph">ところが、<strong>プログラムが大規模になると Git なしでの開発はほぼ不可能</strong>になります。</p>



<p class="wp-block-paragraph">つまり、会社勤めをするなど複数人で開発するなら Git を扱えることは必須になります。</p>



<p class="wp-block-paragraph">本記事では CUI を用いた Git 操作を紹介していきます。</p>



<p class="is-style-icon_announce wp-block-paragraph">なお、CUI操作はなかなかイメージがつかないデメリットもあるので、<a href="https://www.sourcetreeapp.com/">SourceTree</a>や<a href="https://git-fork.com/">Fork</a>などと併用するのもおすすめです。僕は併用してます。</p>



<p class="is-style-icon_announce wp-block-paragraph">Gitコマンドは<a href="https://tracpath.com/docs/">公式リファレンス</a>が参考になります。<br>コマンドの数はそこまで多くないので、見出しだけでもざっと目を通しておくと全体感が捉えやすいです。</p>



<h2 class="wp-block-heading">【基本】CUI を使った Git の基本操作</h2>



<p class="wp-block-paragraph">まずは日常的に使うコマンドからご紹介します。</p>



<h3 class="wp-block-heading">init</h3>



<p class="wp-block-paragraph">現在のディレクトリを新たに Git 管理下に置きたい場合のコマンドです。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>mkdir my_project &amp;&amp; cd my_project
git init</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">mkdir</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">my_project</span><span style="color: #D4D4D4"> &amp;&amp; </span><span style="color: #DCDCAA">cd</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">my_project</span></span>
<span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">init</span></span></code></pre></div>



<p class="wp-block-paragraph">このコマンドを実行すると、<code>.git</code>ディレクトリが作成されます。</p>



<p class="wp-block-paragraph">これにより、現在のディレクトリのファイルやディレクトリが Git によるバージョン管理の対象になるというわけです。</p>



<h3 class="wp-block-heading">clone</h3>



<p class="wp-block-paragraph">リモートリポジトリをローカルマシンにクローン（コピー）します。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git clone https://github.com/username/repository.git</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">clone</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">https://github.com/username/repository.git</span></span></code></pre></div>



<p class="wp-block-paragraph">コピーしたローカルリポジトリには、リポジトリのコミット履歴やブランチの情報も含まれることになります。</p>



<p class="wp-block-paragraph">リポジトリーのURLは、リポジトリページの「Code」ボタンから確認できます。</p>



<figure class="wp-block-image size-full is-style-border"><img decoding="async" width="1024" height="574" src="https://cfxlog.com/wp-content/uploads/2025/12/git-basics-cui-1.png" alt="" class="wp-image-4947"/></figure>



<p class="wp-block-paragraph">HTTPSまたはSSHのタブをクリックして、URLを取得しましょう。</p>



<h3 class="wp-block-heading">status</h3>



<p class="wp-block-paragraph">リポジトリの現在の状態（変更内容、コミット状況など）を確認します。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git status

# On branch main
# Your branch is up to date with 'origin/main'.
# nothing to commit, working tree clean</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">status</span></span>
<span class="line"></span>
<span class="line"><span style="color: #6A9955"># On branch main</span></span>
<span class="line"><span style="color: #6A9955"># Your branch is up to date with &apos;origin/main&apos;.</span></span>
<span class="line"><span style="color: #6A9955"># nothing to commit, working tree clean</span></span></code></pre></div>



<p class="is-style-icon_announce wp-block-paragraph">これは頻繁にお世話になるコマンドです。</p>



<h3 class="wp-block-heading">add</h3>



<p class="wp-block-paragraph">作業ディレクトリでの変更をステージング（次のコミットに追加する準備）します。</p>



<p class="wp-block-paragraph">ファイル全体をステージングするときは以下のようにします。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly># ファイル全体を追加する場合
git add .</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #6A9955"># ファイル全体を追加する場合</span></span>
<span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">add</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">.</span></span></code></pre></div>



<p class="wp-block-paragraph"><code>.</code> は「カレントディレクトリ下のすべて」という意味です。</p>



<p class="wp-block-paragraph">また、特定のファイルだけをステージングする場合には具体的なファイル名を指定します。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly># 特定のファイルを追加する場合
git add README.md</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #6A9955"># 特定のファイルを追加する場合</span></span>
<span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">add</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">README.md</span></span></code></pre></div>



<p class="wp-block-paragraph">つまり、上記の例では<code>README.md</code>だけがステージングされることになります。</p>



<h3 class="wp-block-heading">commit</h3>



<p class="wp-block-paragraph">ステージングされた変更をリポジトリにコミットします。</p>



<p class="wp-block-paragraph"><code>-m</code>オプションをつけると、一行コメントを入れることができます。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly># 一行コメントを入れる
git commit -m "Add initial project files"</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #6A9955"># 一行コメントを入れる</span></span>
<span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">commit</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">-m</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">&quot;Add initial project files&quot;</span></span></code></pre></div>



<p class="wp-block-paragraph">ただし、一般的には「1行目 = タイトル、2行目 = 空白、3行目 = 詳細」といった感じで書きます。そうすると<code>-m</code>だと物足りないので、次のコマンドを実行します。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git commit</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">commit</span></span></code></pre></div>



<p class="wp-block-paragraph">実行後に vim が立ち上がるので、以下の要領でコメントを入力していきましょう。</p>



<div class="wp-block-group has-border -border01"><div class="wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained">
<ol class="wp-block-list is-style-num_circle">
<li><code>i</code>キーを押す</li>



<li><code>Enter</code>キーを押す（Insertモードになる）</li>



<li>コミットメッセージを入力する</li>



<li><code>ESC</code>キーを押す</li>



<li><code>:wq</code>をタイプする</li>



<li><code>Enter</code>キーを押す</li>
</ol>
</div></div>



<p class="wp-block-paragraph">コメントの記載例をいくつか挙げておきます。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Plaintext</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>修正: ログイン時のエラーチェックが不十分だった問題を解決

ログインフォームでの入力値チェックが不足しており、特定の入力がエラーを引き起こしていました。
- ユーザ名の空文字を許可しないように修正
- パスワードの最小文字数をチェック</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #D4D4D4">修正: ログイン時のエラーチェックが不十分だった問題を解決</span></span>
<span class="line"><span style="color: #D4D4D4"></span></span>
<span class="line"><span style="color: #D4D4D4">ログインフォームでの入力値チェックが不足しており、特定の入力がエラーを引き起こしていました。</span></span>
<span class="line"><span style="color: #D4D4D4">- ユーザ名の空文字を許可しないように修正</span></span>
<span class="line"><span style="color: #D4D4D4">- パスワードの最小文字数をチェック</span></span></code></pre></div>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Plaintext</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>機能追加: ユーザープロファイルページに画像アップロード機能を実装

ユーザーが自分のプロフィール画像をアップロードできるようにするため、以下の変更を加えました。
- 画像アップロードのための新しいフォームを追加
- サーバーサイドでの画像処理ロジックを実装
- アップロードされた画像のサイズ制限を設定</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #D4D4D4">機能追加: ユーザープロファイルページに画像アップロード機能を実装</span></span>
<span class="line"><span style="color: #D4D4D4"></span></span>
<span class="line"><span style="color: #D4D4D4">ユーザーが自分のプロフィール画像をアップロードできるようにするため、以下の変更を加えました。</span></span>
<span class="line"><span style="color: #D4D4D4">- 画像アップロードのための新しいフォームを追加</span></span>
<span class="line"><span style="color: #D4D4D4">- サーバーサイドでの画像処理ロジックを実装</span></span>
<span class="line"><span style="color: #D4D4D4">- アップロードされた画像のサイズ制限を設定</span></span></code></pre></div>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Plaintext</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>更新: READMEファイルにインストール手順を追記

新しい依存関係ライブラリに対する説明を追加し、ユーザーがプロジェクトをスムーズにセットアップできるようにしました。
- Pythonのバージョン要件を明記
- 実行に必要な外部ツールのリンクを追加</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #D4D4D4">更新: READMEファイルにインストール手順を追記</span></span>
<span class="line"><span style="color: #D4D4D4"></span></span>
<span class="line"><span style="color: #D4D4D4">新しい依存関係ライブラリに対する説明を追加し、ユーザーがプロジェクトをスムーズにセットアップできるようにしました。</span></span>
<span class="line"><span style="color: #D4D4D4">- Pythonのバージョン要件を明記</span></span>
<span class="line"><span style="color: #D4D4D4">- 実行に必要な外部ツールのリンクを追加</span></span></code></pre></div>



<p class="is-style-icon_info wp-block-paragraph">プロジェクトごとにコメントのルールが決められていることもあるので、基本的にそれに従ってください。</p>



<h3 class="wp-block-heading">push</h3>



<p class="wp-block-paragraph">ローカルのコミットをリモートリポジトリに送信します。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git push origin main</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">push</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">origin</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">main</span></span></code></pre></div>



<h3 class="wp-block-heading">pull</h3>



<p class="wp-block-paragraph">リモートリポジトリの最新の変更をローカルに取り込みます。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git pull origin main</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">pull</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">origin</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">main</span></span></code></pre></div>



<p class="wp-block-paragraph">origin と main の意味は以下の通りです。</p>



<h4 class="wp-block-heading">origin</h4>



<p class="wp-block-paragraph">リモートリポジトリのデフォルトの名前が<code>origin</code>です。</p>



<p class="wp-block-paragraph">Git でリポジトリをクローンすると、リモートリポジトリのデフォルトのショートカットとして<code>origin</code>が設定されます。</p>



<p class="wp-block-paragraph">これにより URL を繰り返し入力する代わりに<code>origin</code>とするだけでリモートリポジトリにアクセスできるようになります。</p>



<p class="is-style-icon_announce wp-block-paragraph">ここが変更されることは滅多にないので、基本的には<code>origin</code>で固定と思っていて間違いありません。</p>



<h4 class="wp-block-heading">main</h4>



<p class="wp-block-paragraph">リポジトリのブランチ名を指します。</p>



<p class="wp-block-paragraph">Git では複数のブランチが切られることが多いので、<code>pull</code>したいブランチ名を正確に指定することが必要になります。</p>



<h3 class="wp-block-heading">branch</h3>



<p class="wp-block-paragraph">ブランチの確認や作成を行います。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly># 既存のブランチ一覧を表示する場合
git branch</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #6A9955"># 既存のブランチ一覧を表示する場合</span></span>
<span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">branch</span></span></code></pre></div>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly># 新しいブランチを作成する場合
git branch feature-branch</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #6A9955"># 新しいブランチを作成する場合</span></span>
<span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">branch</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">feature-branch</span></span></code></pre></div>



<h3 class="wp-block-heading">checkout</h3>



<p class="wp-block-paragraph">ブランチの切り替えや、特定のコミットに移動します。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly># ブランチを切り替える場合
git checkout feature-branch</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #6A9955"># ブランチを切り替える場合</span></span>
<span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">checkout</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">feature-branch</span></span></code></pre></div>



<p class="wp-block-paragraph">ブランチを新規作成して、新しくできたブランチに切り替える場合は以下のコマンドを使います。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git checkout -b &#91;新しいブランチ名&#93;</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">checkout</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">-b</span><span style="color: #D4D4D4"> &#91;新しいブランチ名&#93;</span></span></code></pre></div>



<h3 class="wp-block-heading">merge</h3>



<p class="wp-block-paragraph">別のブランチを現在のブランチにマージ（統合）します。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git checkout main
git merge feature-branch</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">checkout</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">main</span></span>
<span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">merge</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">feature-branch</span></span></code></pre></div>



<p class="wp-block-paragraph">rebaseとの使い分けや、実務でのマージ運用ルールは別記事で詳しく解説しました。</p>


<div class="swell-block-postLink">			<div class="p-blogCard -internal" data-type="type1" data-onclick="clickLink">
				<div class="p-blogCard__inner">
					<span class="p-blogCard__caption">あわせて読みたい</span>
					<div class="p-blogCard__thumb c-postThumb"><figure class="c-postThumb__figure"><img decoding="async" src="https://cfxlog.com/wp-content/uploads/2026/07/git-rebase-merge-guide-300x158.jpg" alt="" class="c-postThumb__img u-obf-cover" width="320" height="180"></figure></div>					<div class="p-blogCard__body">
						<a class="p-blogCard__title" href="https://cfxlog.com/git-rebase-merge-guide/">git rebase・mergeの使い分け完全ガイド｜履歴の残し方で選ぶ判断軸</a>
						<span class="p-blogCard__excerpt">ブランチを統合するときに、mergeとrebaseのどちらを使うか迷う場面はよくあります。 ところが、この2つはやりたいこと自体は同じでも、コミット履歴の残り方がまったく&#8230;</span>					</div>
				</div>
			</div>
		</div>


<h3 class="wp-block-heading">cherry-pick</h3>



<p class="wp-block-paragraph">他のブランチにある特定のコミットだけを取り込みます。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git cherry-pick &#91;commitID&#93;</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">cherry-pick</span><span style="color: #D4D4D4"> &#91;commitID&#93;</span></span></code></pre></div>



<p class="wp-block-paragraph">複数のコミットの取り込みや、連続したコミットの範囲指定による取り込みもできます。</p>



<p class="wp-block-paragraph">別記事で詳しく解説しました。</p>


<div class="swell-block-postLink">			<div class="p-blogCard -internal" data-type="type1" data-onclick="clickLink">
				<div class="p-blogCard__inner">
					<span class="p-blogCard__caption">あわせて読みたい</span>
					<div class="p-blogCard__thumb c-postThumb"><figure class="c-postThumb__figure"><img decoding="async" src="https://cfxlog.com/wp-content/uploads/2026/03/git-cherry-pick-guide-300x158.jpg" alt="" class="c-postThumb__img u-obf-cover" width="320" height="180"></figure></div>					<div class="p-blogCard__body">
						<a class="p-blogCard__title" href="https://cfxlog.com/git-cherry-pick-guide/">チェリーピックとは？git cherry-pickの意味と使い方</a>
						<span class="p-blogCard__excerpt">職場で「プッシュしたから、チェリーピックしておいて」と言われた。…ところで、チェリーピックってなに？ このような方に向けて書きました。 Gitで開発をしていると、&#8230;</span>					</div>
				</div>
			</div>
		</div>


<h3 class="wp-block-heading">revertとreset</h3>



<p class="wp-block-paragraph">Git を使っていると、「あのコミットを取り消したい」という場面が必ず出てきます。<br>ここでよく混乱を招くのが、<strong>revert と reset の違い</strong>です。結論から言うと：</p>



<div class="wp-block-group has-border -border01"><div class="wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained">
<ul class="wp-block-list">
<li><strong>reset は歴史を書き換える（危険／共有リポジトリでは原則禁止）</strong></li>



<li><strong>revert は取り消し用の新しいコミットを作る（安全／実務向け）</strong></li>
</ul>
</div></div>



<p class="wp-block-paragraph">という関係です。</p>



<h4 class="wp-block-heading">revert は “巻き戻す” ではなく “打ち消す”</h4>



<p class="wp-block-paragraph"><code>git revert</code> の動きは直感と少し違い、「過去のコミットを削除する」のではなく、</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p class="wp-block-paragraph"><strong>指定したコミットの変更内容を、反対の変更で打ち消すコミットを作る</strong></p>
</blockquote>



<p class="wp-block-paragraph">というものです。</p>



<p class="wp-block-paragraph">つまり、<strong>履歴を壊さない</strong>ため、チーム開発や本番運用リポジトリでは revert が圧倒的に安全です。</p>



<h4 class="wp-block-heading">revert の基本コマンド</h4>



<p class="wp-block-paragraph">単一コミットを取り消す場合：</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#2e3440"><span style="background:#c8d0e0;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#2e3440">Bash</span></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git revert &lt;コミットID></textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #88C0D0">git</span><span style="color: #D8DEE9FF"> </span><span style="color: #A3BE8C">revert</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">&lt;</span><span style="color: #A3BE8C">コミットI</span><span style="color: #D8DEE9FF">D</span><span style="color: #81A1C1">&gt;</span></span></code></pre></div>



<p class="wp-block-paragraph">複数コミットを取り消す場合：</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#2e3440"><span style="background:#c8d0e0;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#2e3440">Bash</span></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git revert &lt;開始コミットID>..&lt;終了コミットID></textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #88C0D0">git</span><span style="color: #D8DEE9FF"> </span><span style="color: #A3BE8C">revert</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">&lt;</span><span style="color: #A3BE8C">開始コミットI</span><span style="color: #D8DEE9FF">D</span><span style="color: #81A1C1">&gt;</span><span style="color: #A3BE8C">..</span><span style="color: #81A1C1">&lt;</span><span style="color: #A3BE8C">終了コミットI</span><span style="color: #D8DEE9FF">D</span><span style="color: #81A1C1">&gt;</span></span></code></pre></div>



<p class="wp-block-paragraph">コミットメッセージ入力をスキップして自動生成させる場合：</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#2e3440"><span style="background:#c8d0e0;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#2e3440">Bash</span></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git revert --no-edit &lt;コミットID></textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #88C0D0">git</span><span style="color: #D8DEE9FF"> </span><span style="color: #A3BE8C">revert</span><span style="color: #D8DEE9FF"> </span><span style="color: #A3BE8C">--no-edit</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">&lt;</span><span style="color: #A3BE8C">コミットI</span><span style="color: #D8DEE9FF">D</span><span style="color: #81A1C1">&gt;</span></span></code></pre></div>



<h4 class="wp-block-heading">実務で revert を使うシーン</h4>



<div class="wp-block-group has-border -border01"><div class="wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained">
<ul class="wp-block-list">
<li><strong>誤って本番にバグを出してしまったとき</strong><br>→ 該当コミットを revert すれば “安全に元に戻せる”</li>



<li><strong>PR をマージしたら挙動がおかしくなったとき</strong><br>→ reset は絶対NG（履歴破壊）。revert 一択。</li>



<li><strong>Aさんのコミットを B さんが取り消したいケース</strong><br>→ reset は他人の作業を壊すためご法度。revert が正攻法。</li>
</ul>
</div></div>



<p class="wp-block-paragraph">つまり、<strong>共有リポジトリでは reset より revert を使う場面の方が圧倒的に多い</strong>。</p>



<h4 class="wp-block-heading">revert と reset の使い分け</h4>



<p class="wp-block-paragraph">まとめると以下です。</p>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th>コマンド</th><th>履歴</th><th>安全性</th><th>使う場所</th></tr></thead><tbody><tr><td>reset</td><td>履歴を書き換える</td><td>危険</td><td>個人作業ブランチのみ</td></tr><tr><td>revert</td><td>履歴を残す（取り消しコミットを追加）</td><td>安全</td><td>共有ブランチ・本番系</td></tr></tbody></table></figure>



<p class="wp-block-paragraph">本番ブランチは <strong>reset 禁止</strong>、迷ったら <strong>revert</strong> と覚えておくと安全です。</p>



<p class="wp-block-paragraph">&#8211;soft・&#8211;mixed・&#8211;hardの違いなど、resetの詳細は別記事にまとめました。</p>


<div class="swell-block-postLink">			<div class="p-blogCard -internal" data-type="type1" data-onclick="clickLink">
				<div class="p-blogCard__inner">
					<span class="p-blogCard__caption">あわせて読みたい</span>
					<div class="p-blogCard__thumb c-postThumb"><figure class="c-postThumb__figure"><img decoding="async" src="https://cfxlog.com/wp-content/uploads/2026/08/git-reset-revert-guide-300x158.jpg" alt="" class="c-postThumb__img u-obf-cover" width="320" height="180"></figure></div>					<div class="p-blogCard__body">
						<a class="p-blogCard__title" href="https://cfxlog.com/git-reset-revert-guide/">git resetの使い分け完全ガイド｜&#8211;soft・&#8211;mixed・&#8211;hardとrevertの違い</a>
						<span class="p-blogCard__excerpt">コミットを取り消したいだけなのに、コマンドが複数あって迷う場面はよくあります。 ところが、git resetには&#8211;soft・&#8211;mixed・&#8211;hardという3つのオプションがあり、git&#8230;</span>					</div>
				</div>
			</div>
		</div>


<h3 class="wp-block-heading">log</h3>



<p class="wp-block-paragraph">リポジトリのコミット履歴を表示します。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git log</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">log</span></span></code></pre></div>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly># ログを1行で表示する場合
git log --oneline</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #6A9955"># ログを1行で表示する場合</span></span>
<span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">log</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">--oneline</span></span></code></pre></div>



<h3 class="wp-block-heading">diff</h3>



<p class="wp-block-paragraph">コミット間やステージングエリアと作業ディレクトリの変更点を比較します。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly># ステージングされていない変更を確認する場合
git diff</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #6A9955"># ステージングされていない変更を確認する場合</span></span>
<span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">diff</span></span></code></pre></div>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly># ステージングされた変更を確認する場合
git diff --staged</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #6A9955"># ステージングされた変更を確認する場合</span></span>
<span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">diff</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">--staged</span></span></code></pre></div>



<h2 class="wp-block-heading">ケース別コマンドの組み合わせ</h2>



<h3 class="wp-block-heading">ファイルを追加したり更新した場合</h3>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git add -A  # 全てステージングする
git commit -m "メッセージ"  #コミットする
git push origin main  # プッシュする</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">add</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">-A</span><span style="color: #D4D4D4">  </span><span style="color: #6A9955"># 全てステージングする</span></span>
<span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">commit</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">-m</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">&quot;メッセージ&quot;</span><span style="color: #D4D4D4">  </span><span style="color: #6A9955">#コミットする</span></span>
<span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">push</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">origin</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">main</span><span style="color: #D4D4D4">  </span><span style="color: #6A9955"># プッシュする</span></span></code></pre></div>



<h3 class="wp-block-heading">最新のリモートリポジトリを取り込む</h3>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git pull origin main</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">pull</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">origin</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">main</span></span></code></pre></div>



<h3 class="wp-block-heading">HTTPS から SSH に変更したいとき</h3>



<p class="wp-block-paragraph">認証方法を HTTPS から SSH に変更する場合には、以下のコマンドを実行します。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git remote set-url origin &lt;新しいリモートリポジトリのURL></textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">git</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">remote</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">set-url</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">origin</span><span style="color: #D4D4D4"> &lt;</span><span style="color: #CE9178">新しいリモートリポジトリのUR</span><span style="color: #D4D4D4">L&gt;</span></span></code></pre></div>



<p class="wp-block-paragraph">このコマンドが必要な理由は、HTTPS と SSH でリモートリポジトリの URL が変わるためです。</p>



<h2 class="wp-block-heading">作業が快適になる Git の便利設定</h2>



<p class="wp-block-paragraph">Git はそのままでも使えますが、設定を少し加えるだけでかなり使いやすくなります。</p>



<h3 class="wp-block-heading">よく使う alias を設定する</h3>



<p class="wp-block-paragraph"><code>git st</code> で status が出るので、作業がだいぶ快適になります。</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#2e3440"><span style="background:#c8d0e0;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#2e3440">Bash</span></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>git config --global alias.st status
git config --global alias.br branch
git config --global alias.co checkout</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #88C0D0">git</span><span style="color: #D8DEE9FF"> </span><span style="color: #A3BE8C">config</span><span style="color: #D8DEE9FF"> </span><span style="color: #A3BE8C">--global</span><span style="color: #D8DEE9FF"> </span><span style="color: #A3BE8C">alias.st</span><span style="color: #D8DEE9FF"> </span><span style="color: #A3BE8C">status</span></span>
<span class="line"><span style="color: #88C0D0">git</span><span style="color: #D8DEE9FF"> </span><span style="color: #A3BE8C">config</span><span style="color: #D8DEE9FF"> </span><span style="color: #A3BE8C">--global</span><span style="color: #D8DEE9FF"> </span><span style="color: #A3BE8C">alias.br</span><span style="color: #D8DEE9FF"> </span><span style="color: #A3BE8C">branch</span></span>
<span class="line"><span style="color: #88C0D0">git</span><span style="color: #D8DEE9FF"> </span><span style="color: #A3BE8C">config</span><span style="color: #D8DEE9FF"> </span><span style="color: #A3BE8C">--global</span><span style="color: #D8DEE9FF"> </span><span style="color: #A3BE8C">alias.co</span><span style="color: #D8DEE9FF"> </span><span style="color: #A3BE8C">checkout</span></span></code></pre></div>



<h3 class="wp-block-heading">● mac の .DS_Store を無視したい場合</h3>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#2e3440"><span style="background:#c8d0e0;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#2e3440">Bash</span></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>echo .DS_Store >> ~/.gitignore_global
git config --global core.excludesfile ~/.gitignore_global</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #88C0D0">echo</span><span style="color: #D8DEE9FF"> </span><span style="color: #A3BE8C">.DS_Store</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">&gt;&gt;</span><span style="color: #D8DEE9FF"> </span><span style="color: #A3BE8C">~/.gitignore_global</span></span>
<span class="line"><span style="color: #88C0D0">git</span><span style="color: #D8DEE9FF"> </span><span style="color: #A3BE8C">config</span><span style="color: #D8DEE9FF"> </span><span style="color: #A3BE8C">--global</span><span style="color: #D8DEE9FF"> </span><span style="color: #A3BE8C">core.excludesfile</span><span style="color: #D8DEE9FF"> </span><span style="color: #A3BE8C">~/.gitignore_global</span></span></code></pre></div>



<p class="wp-block-paragraph">特定のパターンの除外方法や、すでに追跡中のファイルを後から除外する方法は別記事にまとめました。</p>


<div class="swell-block-postLink">			<div class="p-blogCard -internal" data-type="type1" data-onclick="clickLink">
				<div class="p-blogCard__inner">
					<span class="p-blogCard__caption">あわせて読みたい</span>
					<div class="p-blogCard__thumb c-postThumb"><figure class="c-postThumb__figure"><img decoding="async" src="https://cfxlog.com/wp-content/uploads/2026/07/swell-eyecatch-noimg-300x158.jpg" alt="" class="c-postThumb__img u-obf-cover" width="320" height="180"></figure></div>					<div class="p-blogCard__body">
						<a class="p-blogCard__title" href="https://cfxlog.com/gitignore-guide/">.gitignoreの書き方完全ガイド｜パターン記法から適用済みファイルの除外まで</a>
						<span class="p-blogCard__excerpt">よくある話が、環境依存のファイルやライブラリのキャッシュまで、うっかりGit管理下に入れてしまう場面。 ところが、.gitignoreの書き方はパターンの種類が意外と多く&#8230;</span>					</div>
				</div>
			</div>
		</div>


<h2 class="wp-block-heading">Git トラブルシューティング（よくあるエラーまとめ）</h2>



<p class="wp-block-paragraph">よくあるつまずきポイントをまとめておきます。</p>



<h3 class="wp-block-heading">push rejected（non-fast-forward）</h3>



<p class="wp-block-paragraph">リモートが進んでいるので、一度 <code>git pull</code> してから push します。</p>



<h3 class="wp-block-heading">.gitignore が効かない</h3>



<p class="wp-block-paragraph">すでに履歴に入っているパターンです。</p>



<p class="wp-block-paragraph"><code>git rm --cached</code> が必要です。</p>



<h3 class="wp-block-heading">Detached HEAD になった</h3>



<p class="wp-block-paragraph"><code>git switch &lt;branch&gt;</code> で戻れます。</p>



<h3 class="wp-block-heading">変更したのに反映されない</h3>



<p class="wp-block-paragraph">add / commit / push のどこかが抜けているパターンです。</p>



<h2 class="wp-block-heading">Git学習ロードマップ</h2>



<p class="wp-block-paragraph">ここまでの基本操作を一通り触ったら、次は以下の順番で深掘りしていくと実務でつまずきにくくなります。</p>



<h3 class="wp-block-heading">Step 1: 作業を安全に一時退避する</h3>


<div class="swell-block-postLink">			<div class="p-blogCard -internal" data-type="type1" data-onclick="clickLink">
				<div class="p-blogCard__inner">
					<span class="p-blogCard__caption">あわせて読みたい</span>
					<div class="p-blogCard__thumb c-postThumb"><figure class="c-postThumb__figure"><img decoding="async" src="https://cfxlog.com/wp-content/uploads/2026/07/swell-eyecatch-noimg-300x158.jpg" alt="" class="c-postThumb__img u-obf-cover" width="320" height="180"></figure></div>					<div class="p-blogCard__body">
						<a class="p-blogCard__title" href="https://cfxlog.com/git-stash-guide/">git stashの使い方完全ガイド｜作業を一時退避してブランチを切り替える</a>
						<span class="p-blogCard__excerpt">よくある話が、作業の途中で急に別のブランチに切り替えたくなる場面。 コミットするほどはまとまっていないが、そのままではブランチを切り替えられない。ところが、コ&#8230;</span>					</div>
				</div>
			</div>
		</div>


<h3 class="wp-block-heading">Step 2: 統合方法を使い分ける</h3>


<div class="swell-block-postLink">			<div class="p-blogCard -internal" data-type="type1" data-onclick="clickLink">
				<div class="p-blogCard__inner">
					<span class="p-blogCard__caption">あわせて読みたい</span>
					<div class="p-blogCard__thumb c-postThumb"><figure class="c-postThumb__figure"><img decoding="async" src="https://cfxlog.com/wp-content/uploads/2026/07/git-rebase-merge-guide-300x158.jpg" alt="" class="c-postThumb__img u-obf-cover" width="320" height="180"></figure></div>					<div class="p-blogCard__body">
						<a class="p-blogCard__title" href="https://cfxlog.com/git-rebase-merge-guide/">git rebase・mergeの使い分け完全ガイド｜履歴の残し方で選ぶ判断軸</a>
						<span class="p-blogCard__excerpt">ブランチを統合するときに、mergeとrebaseのどちらを使うか迷う場面はよくあります。 ところが、この2つはやりたいこと自体は同じでも、コミット履歴の残り方がまったく&#8230;</span>					</div>
				</div>
			</div>
		</div>


<h3 class="wp-block-heading">Step 3: コミットを安全に取り消す</h3>


<div class="swell-block-postLink">			<div class="p-blogCard -internal" data-type="type1" data-onclick="clickLink">
				<div class="p-blogCard__inner">
					<span class="p-blogCard__caption">あわせて読みたい</span>
					<div class="p-blogCard__thumb c-postThumb"><figure class="c-postThumb__figure"><img decoding="async" src="https://cfxlog.com/wp-content/uploads/2026/08/git-reset-revert-guide-300x158.jpg" alt="" class="c-postThumb__img u-obf-cover" width="320" height="180"></figure></div>					<div class="p-blogCard__body">
						<a class="p-blogCard__title" href="https://cfxlog.com/git-reset-revert-guide/">git resetの使い分け完全ガイド｜&#8211;soft・&#8211;mixed・&#8211;hardとrevertの違い</a>
						<span class="p-blogCard__excerpt">コミットを取り消したいだけなのに、コマンドが複数あって迷う場面はよくあります。 ところが、git resetには&#8211;soft・&#8211;mixed・&#8211;hardという3つのオプションがあり、git&#8230;</span>					</div>
				</div>
			</div>
		</div>


<h3 class="wp-block-heading">Step 4: 特定のコミットだけを取り込む・取り消す</h3>


<div class="swell-block-postLink">			<div class="p-blogCard -internal" data-type="type1" data-onclick="clickLink">
				<div class="p-blogCard__inner">
					<span class="p-blogCard__caption">あわせて読みたい</span>
					<div class="p-blogCard__thumb c-postThumb"><figure class="c-postThumb__figure"><img decoding="async" src="https://cfxlog.com/wp-content/uploads/2026/03/git-cherry-pick-guide-300x158.jpg" alt="" class="c-postThumb__img u-obf-cover" width="320" height="180"></figure></div>					<div class="p-blogCard__body">
						<a class="p-blogCard__title" href="https://cfxlog.com/git-cherry-pick-guide/">チェリーピックとは？git cherry-pickの意味と使い方</a>
						<span class="p-blogCard__excerpt">職場で「プッシュしたから、チェリーピックしておいて」と言われた。…ところで、チェリーピックってなに？ このような方に向けて書きました。 Gitで開発をしていると、&#8230;</span>					</div>
				</div>
			</div>
		</div>

<div class="swell-block-postLink">			<div class="p-blogCard -internal" data-type="type1" data-onclick="clickLink">
				<div class="p-blogCard__inner">
					<span class="p-blogCard__caption">あわせて読みたい</span>
					<div class="p-blogCard__thumb c-postThumb"><figure class="c-postThumb__figure"><img decoding="async" src="https://cfxlog.com/wp-content/uploads/2026/03/git-cherry-pick-undo-guide-300x158.jpg" alt="" class="c-postThumb__img u-obf-cover" width="320" height="180"></figure></div>					<div class="p-blogCard__body">
						<a class="p-blogCard__title" href="https://cfxlog.com/git-cherry-pick-undo-guide/">git cherry-pickの取り消し方法｜revert・reset・コンフリクト解消まで完全解説</a>
						<span class="p-blogCard__excerpt">git cherry-pick を取り消したいけど、どのコマンドを使えばいいか分からない コンフリクトが起きて cherry-pick が止まってしまった push 後に cherry-pick したコミッ&#8230;</span>					</div>
				</div>
			</div>
		</div>


<h3 class="wp-block-heading">Step 5: 管理対象から除外する</h3>


<div class="swell-block-postLink">			<div class="p-blogCard -internal" data-type="type1" data-onclick="clickLink">
				<div class="p-blogCard__inner">
					<span class="p-blogCard__caption">あわせて読みたい</span>
					<div class="p-blogCard__thumb c-postThumb"><figure class="c-postThumb__figure"><img decoding="async" src="https://cfxlog.com/wp-content/uploads/2026/07/swell-eyecatch-noimg-300x158.jpg" alt="" class="c-postThumb__img u-obf-cover" width="320" height="180"></figure></div>					<div class="p-blogCard__body">
						<a class="p-blogCard__title" href="https://cfxlog.com/gitignore-guide/">.gitignoreの書き方完全ガイド｜パターン記法から適用済みファイルの除外まで</a>
						<span class="p-blogCard__excerpt">よくある話が、環境依存のファイルやライブラリのキャッシュまで、うっかりGit管理下に入れてしまう場面。 ところが、.gitignoreの書き方はパターンの種類が意外と多く&#8230;</span>					</div>
				</div>
			</div>
		</div>


<h2 class="wp-block-heading">逆引きナビ：目的から探す</h2>



<p class="wp-block-paragraph">「〜したい」から該当の記事を探したい場合は、こちらもご利用ください。</p>



<ul class="wp-block-list">

<li><strong>作業を中断して別のブランチに切り替えたい</strong> → <a href="https://cfxlog.com/git-stash-guide/">git stashの使い方完全ガイド</a></li>



<li><strong>ブランチの履歴をきれいに保ちたい・チームでの運用ルールを知りたい</strong> → <a href="https://cfxlog.com/git-rebase-merge-guide/">git rebase・mergeの使い分け完全ガイド</a></li>



<li><strong>コミットを取り消したい・安全な取り消し方を知りたい</strong> → <a href="https://cfxlog.com/git-reset-revert-guide/">git resetの使い分け完全ガイド</a></li>



<li><strong>特定のコミットだけを別ブランチに持っていきたい</strong> → <a href="https://cfxlog.com/git-cherry-pick-guide/">cherry-pickの使い方</a></li>



<li><strong>cherry-pickを間違えた・取り消したい</strong> → <a href="https://cfxlog.com/git-cherry-pick-undo-guide/">cherry-pickの取り消し方法</a></li>



<li><strong>特定のファイルをGit管理から除外したい</strong> → <a href="https://cfxlog.com/gitignore-guide/">.gitignoreの書き方完全ガイド</a></li>

</ul>



<h2 class="wp-block-heading">まとめ</h2>



<p class="wp-block-paragraph">Gitは独学だと後回しにしがちですが、覚えるコマンドの数自体はそう多くありません。まずはこの記事の基本操作を手を動かして覚え、つまずいたら該当の詳細記事に進む、という流れで十分です。</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>ローカルのSSHキーを使ってリモートサーバーからGitHub操作をする方法</title>
		<link>https://cfxlog.com/linux-ssh-agent-forwarding-github/</link>
		
		<dc:creator><![CDATA[CFXLOG]]></dc:creator>
		<pubDate>Mon, 10 Nov 2025 15:00:00 +0000</pubDate>
				<category><![CDATA[VPS・サーバー運用]]></category>
		<category><![CDATA[Git運用]]></category>
		<category><![CDATA[SSH]]></category>
		<category><![CDATA[セキュリティ]]></category>
		<guid isPermaLink="false">https://cfxlog.com/?p=4928</guid>

					<description><![CDATA[<p><img src="https://cfxlog.com/wp-content/uploads/2026/03/linux-ssh-agent-forwarding-github.jpg" class="webfeedsFeaturedVisual" /></p>そんな時に便利なのがSSHエージェントフォワーディングです。 これを利用すれば、リモートサーバーにいながらローカルマシンにある秘密鍵を使って Git 操作ができるようになります。 つまり、リモートサーバー常に秘密鍵がなく [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><img src="https://cfxlog.com/wp-content/uploads/2026/03/linux-ssh-agent-forwarding-github.jpg" class="webfeedsFeaturedVisual" /></p>
<div class="wp-block-group is-style-crease"><div class="wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained">
<ul class="wp-block-list">
<li>リモートサーバーでGitHub操作を行うとき、秘密鍵を置くのはセキュリティ的に不安…</li>
</ul>
</div></div>



<p class="wp-block-paragraph">そんな時に便利なのがSSHエージェントフォワーディングです。</p>



<p class="wp-block-paragraph">これを利用すれば、リモートサーバーにいながら<strong>ローカルマシンにある秘密鍵</strong>を使って Git 操作ができるようになります。</p>



<p class="wp-block-paragraph">つまり、リモートサーバー常に秘密鍵がなくとも、あたかもリモートサーバー上に秘密鍵があるように振る舞うことができるというわけです。</p>



<h2 class="wp-block-heading">SSHエージェントフォワーディングの仕組み</h2>



<figure class="wp-block-image size-full is-style-border"><img decoding="async" width="960" height="540" src="https://cfxlog.com/wp-content/uploads/2026/03/linux-ssh-agent-forwarding-github-1.png" alt="" class="wp-image-4932" srcset="https://cfxlog.com/wp-content/uploads/2026/03/linux-ssh-agent-forwarding-github-1.png 960w, https://cfxlog.com/wp-content/uploads/2026/03/linux-ssh-agent-forwarding-github-1-300x169.png 300w, https://cfxlog.com/wp-content/uploads/2026/03/linux-ssh-agent-forwarding-github-1-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></figure>



<p class="wp-block-paragraph">サーバーに入った状態で、ローカルマシンにある秘密鍵が使えるようになります。</p>



<p class="wp-block-paragraph">上の図でもわかるとおりサーバーには秘密鍵を置きません。</p>



<p class="wp-block-paragraph">ここでリモートサーバーに接続する時に<code>-A</code>オプションをつけると、SSHエージェントフォワーディングとして接続されます。</p>



<p class="wp-block-paragraph">これにより、<strong>リモートサーバーからローカルマシンのSSHエージェントにアクセスできる</strong>ようになるのです。</p>



<p class="wp-block-paragraph">つまり、秘密鍵の実態はローカルマシンにありながら、リモートサーバーからもローカルマシンの秘密鍵が使える状態になるわけです。</p>



<p class="wp-block-paragraph">特徴としては以下の通り。</p>



<div class="wp-block-group has-border -border01"><div class="wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained">
<ul class="wp-block-list">
<li><strong>秘密鍵をリモートサーバーに置かない</strong><br>秘密鍵はローカルマシンに保持されたまま</li>



<li><strong>安全なアクセス権限の管理</strong><br>リモートサーバーではSSHエージェントを経由して認証が行われるので、直接秘密鍵を扱わない</li>
</ul>
</div></div>



<p class="wp-block-paragraph">これにより、リモートサーバーに秘密鍵をおくことなく、GitHubへの認証も通すことができます。</p>



<h2 class="wp-block-heading">SSHエージェントフォワーディングの手順</h2>



<p class="wp-block-paragraph">SSHエージェントフォワーディングの具体的な手順をご紹介します。</p>



<div class="wp-block-group has-border -border01"><div class="wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained">
<ul class="wp-block-list">
<li>ローカルマシンのSSHエージェントに鍵を登録</li>



<li>エージェントフォワーディングで接続</li>



<li>動作確認</li>
</ul>
</div></div>



<p class="wp-block-paragraph">順番に解説します。</p>



<h3 class="wp-block-heading">ローカルマシンでSSHエージェントを起動し、鍵を追加</h3>



<p class="wp-block-paragraph">まず、ローカルマシンでSSHエージェントを起動します。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly># SSHエージェントを起動
eval "$(ssh-agent -s)"</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #6A9955"># SSHエージェントを起動</span></span>
<span class="line"><span style="color: #DCDCAA">eval</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">&quot;$(</span><span style="color: #DCDCAA">ssh-agent</span><span style="color: #CE9178"> </span><span style="color: #569CD6">-s</span><span style="color: #CE9178">)&quot;</span></span></code></pre></div>



<p class="wp-block-paragraph">SSHエージェントに秘密鍵を登録しましょう。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly># 秘密鍵をエージェントに追加
ssh-add ~/.ssh/id_rsa</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #6A9955"># 秘密鍵をエージェントに追加</span></span>
<span class="line"><span style="color: #DCDCAA">ssh-add</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">~/.ssh/id_rsa</span></span></code></pre></div>



<p class="wp-block-paragraph">これでローカルマシンのSSHエージェントに秘密鍵が登録されました。</p>



<h3 class="wp-block-heading">エージェントフォワーディングを使ってサーバーに接続</h3>



<p class="wp-block-paragraph"><code>-A</code>オプションをつけて、エージェントフォワーディングでサーバーに接続します。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>ssh -A username@remote-server</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">ssh</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">-A</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">username@remote-server</span></span></code></pre></div>



<p class="wp-block-paragraph">これで<strong>リモートサーバーからローカルマシンのSSHエージェントに通信できる</strong>ようになります。</p>



<h3 class="wp-block-heading">リモートサーバー上でGit操作を行う</h3>



<p class="wp-block-paragraph">あとはサーバー上で通常通りのGit操作を行えます。</p>



<p class="wp-block-paragraph">GitHubとの疎通確認をしてみましょう。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>ssh -T git@github.com
# Hi ********! You've successfully authenticated, but GitHub does not provide shell access.</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">ssh</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">-T</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">git@github.com</span></span>
<span class="line"><span style="color: #6A9955"># Hi ********! You&apos;ve successfully authenticated, but GitHub does not provide shell access.</span></span></code></pre></div>



<p class="wp-block-paragraph">接続にあたってはローカルのSSH秘密鍵が利用されるので、リモートサーバーに秘密鍵を保存しなくても正常に動きます。</p>



<h2 class="wp-block-heading">よくある疑問とその答え</h2>



<h3 class="wp-block-heading">エージェント転送の確認</h3>



<p class="wp-block-paragraph">次のコマンドでSSHエージェントが正常に転送されているか確認します。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>echo $SSH_AUTH_SOCK
# 何も返ってこない</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">echo</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">$SSH_AUTH_SOCK</span></span>
<span class="line"><span style="color: #6A9955"># 何も返ってこない</span></span></code></pre></div>



<p class="wp-block-paragraph">転送されていなければ何も返ってきませんので、SSH接続の時に<code>-A</code>オプションを忘れていないかを確認してください。</p>



<p class="wp-block-paragraph">もしパスが返ってくればエージェント転送が有効です。</p>



<p class="wp-block-paragraph">続いて、<strong>ローカル側</strong>で次のコマンドを実行してみましょう。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>ssh-add -l
# The agent has no identities.</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">ssh-add</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">-l</span></span>
<span class="line"><span style="color: #6A9955"># The agent has no identities.</span></span></code></pre></div>



<p class="wp-block-paragraph"><code>The agent has no identities.</code>と返ってきた場合には、SSHエージェントに鍵が登録されていません。</p>



<p class="wp-block-paragraph">次のコマンドでSSHエージェントに鍵を登録しましょう。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>ssh-add ~/.ssh/id_rsa</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">ssh-add</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">~/.ssh/id_rsa</span></span></code></pre></div>



<p class="wp-block-paragraph">再度ssh-addコマンドを実行します。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>ssh-add -l
# 256 SHA256:************* ******@gmail.com (ED25519)</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">ssh-add</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">-l</span></span>
<span class="line"><span style="color: #6A9955"># 256 SHA256:************* ******@gmail.com (ED25519)</span></span></code></pre></div>



<p class="wp-block-paragraph">これで鍵が登録されたことがわかりました。</p>



<p class="wp-block-paragraph"><code>ssh -A</code>としてログインすれば、GitHubへのアクセスができるようになっているはずです。</p>



<h3 class="wp-block-heading">エージェントフォワーディングの永続化方法</h3>



<p class="wp-block-paragraph">毎回、自動的に<code>-A</code>オプションをつける方法があります。</p>



<p class="wp-block-paragraph">SSH設定ファイル（<code>~/.ssh/config</code>）に<code>ForwardAgent yes</code>を設定しましょう。</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">Bash</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>Host remote-server
    HostName example.com
    User username
    ForwardAgent yes</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">Host</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">remote-server</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #DCDCAA">HostName</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">example.com</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #DCDCAA">User</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">username</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #DCDCAA">ForwardAgent</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">yes</span></span></code></pre></div>



<p class="wp-block-paragraph">このように<code>ForwardAgent yes</code>とするとエージェントフォワーディングを自動化できますが、エージェント転送を常に有効にすることはセキュリティリスクがあります。</p>



<p class="wp-block-paragraph">例えば、中間者攻撃により悪意のあるサーバーがSSHエージェントを悪用する可能性があります。</p>



<p class="wp-block-paragraph">セキュリティを十分に考慮して利用してください。</p>



<p class="wp-block-paragraph">基本的には、必要なタイミングで<code>-A</code>オプションをつけましょう。</p>



<h2 class="wp-block-heading">まとめ</h2>



<p class="wp-block-paragraph">ここまでSSHエージェントフォワーディングを使った秘密鍵管理の方法をお伝えしてきました。</p>



<p class="wp-block-paragraph">手順は以下の通りです。</p>



<div class="wp-block-group has-border -border01"><div class="wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained">
<ul class="wp-block-list is-style-num_circle">
<li>SSHエージェントに鍵を登録</li>



<li><code>-A</code>オプションをつけてSSH接続</li>
</ul>
</div></div>



<p class="wp-block-paragraph">便利な方法ですが、中間者攻撃を受けてしまった場合の問題などもあったりするそうです。</p>



<p class="wp-block-paragraph">セキュリティ対策はしつつの使用が望ましいかと思います。</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
