Handling Unstaged Files
Now that you understand how to view your unstaged changes, let's explore the different ways to handle those changes in your Git workflow.
Staging Unstaged Changes
To add your unstaged changes to the staging area, you can use the git add
command. This will move the changes from the working directory to the staging area, preparing them for the next commit.
$ git add file1.txt file2.js
After running this command, the modified files file1.txt
and file2.js
will be staged and ready to be committed.
Discarding Unstaged Changes
If you've made changes to a file in your working directory but don't want to keep those changes, you can discard them using the git restore
command.
$ git restore file1.txt
This will revert the changes made to file1.txt
and restore the file to its previous state.
Selectively Staging Changes
Sometimes, you may have made changes to multiple files, but you only want to stage some of them. Git allows you to selectively stage changes using the git add -p
command.
$ git add -p
diff --git a/file1.txt b/file1.txt
index 5716ca5..e25d8fc 100644
--- a/file1.txt
+++ b/file1.txt
@@ -1 +1 @@
-hello
+hello world
Stage this hunk [y,n,q,a,d,/,j,J,g,e,?]? y
diff --git a/file2.js b/file2.js
index 4c6d7c2..a9bdc3f 100644
--- a/file2.js
+++ b/file2.js
@@ -1,3 +1,3 @@
var x = 1
-var y = 2
+var y = 3
Stage this hunk [y,n,q,a,d,/,j,J,g,e,?]? n
This command will walk you through the changes in your working directory, allowing you to selectively stage the changes you want to include in the next commit.
By understanding how to handle unstaged files, you can effectively manage your Git workflow and ensure that your commits are organized and meaningful.