Git Stash を元に戻す

John Wachira 2024年2月15日 Git Git Stash
  1. Git でディレクトリ内の変更を収集するには git add コマンドを使用する
  2. Git でディレクトリにステージした変更を保存するには git commit コマンドを使用する
  3. Git でステージ済みの変更と未ステージの変更をディレクトリに保存するには git stash コマンドを使用する
  4. Git で git stash を元に戻すには git stash pop コマンドを使用する
Git Stash を元に戻す

この記事では、リポジトリに変更を加えて保存する方法を示します。Git を使用すると、変更をローカルに保存し、必要に応じてサーバーにプッシュできます。

Git では、save という用語を使用する代わりに、commit を使用します。変更を保存するには、git addgit commit、および git stash コマンドを使用します。

また、git stash pop コマンドを使用して git stash を元に戻す方法も示しています。

Git でディレクトリ内の変更を収集するには git add コマンドを使用する

git add コマンドは、プロジェクトのディレクトリ内のすべての変更を収集し、それらをステージング領域に移動します。実際には、次のコミットでファイルを更新するように Git に指示します。

更新を保存するには、git commit を実行する必要があります。

これは、git add コマンドを使用して、次のコミットのためにファイルまたはフォルダーをステージングする方法です。

git add Tutorial.txt 

ディレクトリの場合:

git add -Delft 

この場合、作業ディレクトリに Tutorial.txt ファイルがあります。ファイルに変更を加えた場合は、git add Tutorial.txt コマンドを使用してコミットの変更をステージングします。

Git でディレクトリにステージした変更を保存するには git commit コマンドを使用する

git commit コマンドを使用して、段階的な変更を作業ディレクトリに保存します。このコマンドは、git add と連携して使用します。

これらのコミットは、プロジェクトの現在の状態のスナップショットのようなものです。

Git を使用すると、収集されたと感じる変更を中央サーバーにプッシュする前に収集できます。このようにして、機能して合意されたものだけが中央サーバーに移動できます。

以前、このコマンドを使用してファイルをステージングしました。

git add Tutorial.txt 

git status コマンドを使用して、以下の出力を確認します。

pc@JOHN MINGW64 ~/Git (main) $ git status On branch main Changes to be committed:  (use "git restore --staged <file>..." to unstage)  new file: Tutorial.txt 

変更を保存するには、git commit を実行します。

pc@JOHN MINGW64 ~/Git (main) $ git status On branch main Changes to be committed:  (use "git restore --staged <file>..." to unstage)  new file: Tutorial.txt $git commit 

出力:

git commit

最初の行にコミットを要約します。空白のままにすると、コミットが終了します。

Git でステージ済みの変更と未ステージの変更をディレクトリに保存するには git stash コマンドを使用する

git stash コマンドを使用して、ステージングされた変更とステージングされていない変更を保存します。git status コマンドを使用して、ダーティワークスペースを確認します。

$ git status On branch main Changes to be committed:  (use "git restore --staged <file>..." to unstage)  modified: Tutorial.txt  Changes not staged for commit:  (use "git add/rm <file>..." to update what will be committed)  (use "git restore <file>..." to discard changes in working directory)  deleted: text.txt.txt  modified: text.txt.txt.bak 

git stash コマンドを使用して、他の何かに作用する上記の変更を保存できます。

pc@JOHN MINGW64 ~/Git (main) $ git stash Saved working directory and index state WIP on main: 195e5c3 $git status 

git status の実行:

pc@JOHN MINGW64 ~/Git (main) $ git status On branch main nothing to commit, working tree clean 

Git で git stash を元に戻すには git stash pop コマンドを使用する

git stash を元に戻すには、git stash pop コマンドを使用します。それはあなたの隠し場所をあなたの作業コピーに再適用します。

pc@JOHN MINGW64 ~/Git (main) $ git stash pop Removing text.txt.txt On branch main Changes not staged for commit:  (use "git add/rm <file>..." to update what will be committed)  (use "git restore <file>..." to discard changes in working directory)  modified: Tutorial.txt  deleted: text.txt.txt  modified: text.txt.txt.bak  no changes added to commit (use "git add" and/or "git commit -a") Dropped refs/stash@{0} (e1fdba2aaecc32e7ad546de1586a2381f812a5dd) 
チュートリアルを楽しんでいますか? <a href="https://www.youtube.com/@delftstack/?sub_confirmation=1" style="color: #a94442; font-weight: bold; text-decoration: underline;">DelftStackをチャンネル登録</a> して、高品質な動画ガイドをさらに制作するためのサポートをお願いします。 Subscribe
著者: John Wachira
John Wachira avatar John Wachira avatar

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.

LinkedIn

関連記事 - Git Stash