Use case - You have some changes, and you do not want to commit to upstream/origin, but you want to share the changes to your friend/colleague, so that they can work on top of your changes, then you can follow below steps
Step 1 - You can use git diff
to get the current changes in your repo
For example - Let’s say you have below file
➜ repo1 git:(master) cat test.txt
test 1
test 2
test 3
test 4
test 5
You made some change, to replace test 2
to sample 2
➜ repo1 git:(master) sed -i "" 's/test 2/sample 2/' test.txt
You can see what changes are done in this repo by using git diff
(usually git diff will open results in less
editor,
you can pipe the output to bat or cat
command)
➜ repo1 git:(master) ✗ git diff | bat
───────┬───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
│ STDIN
───────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
1 │ diff --git a/test.txt b/test.txt
2 │ index cd9fa46..556e1aa 100644
3 │ --- a/test.txt
4 │ +++ b/test.txt
5 │ @@ -1,5 +1,5 @@
6 │ test 1
7 │ -test 2
8 │ +sample 2
9 │ test 3
10 │ test 4
11 │ test 5
You can capture the git diff
output in a file, like below.
➜ repo1 git:(master) ✗ git diff > /tmp/repo-changes
Step 2 - Now, you can share this file (/tmp/repo-changes) with your friend/colleague.
And they can apply the changes using git apply
(which will make changes as per the given file)
For example -
➜ repo2 git:(master) git apply /tmp/repo-changes
And verify whether changes are applied or not using git diff
➜ repo2 git:(master) ✗ git diff | bat
───────┬───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
│ STDIN
───────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
1 │ diff --git a/test.txt b/test.txt
2 │ index cd9fa46..556e1aa 100644
3 │ --- a/test.txt
4 │ +++ b/test.txt
5 │ @@ -1,5 +1,5 @@
6 │ test 1
7 │ -test 2
8 │ +sample 2
9 │ test 3
10 │ test 4
11 │ test 5
Similarly, let’s say you have your changes in git stash
. You can share the changes like below.
For example - you saved your current changes using git stash
.(this will keep your changes in stash snapshot and revert
your local repo to its head version, git stash apply
will put back the changes, git stash apply stash@{12}
to apply
a specific stash snapshot, git stash drop stash@{0}
to remove stash snapshot)
➜ repo1 git:(master) ✗ git stash
Saved working directory and index state WIP on master: 2bd3c35 my first commit
You can list your stashes using the below command
➜ repo1 git:(master) git stash list | cat
stash@{0}: WIP on master: 2bd3c35 my first commit
You can view the latest stash changes like below
➜ repo1 git:(master) git stash show -p | cat
diff --git a/test.txt b/test.txt
index cd9fa46..556e1aa 100644
--- a/test.txt
+++ b/test.txt
@@ -1,5 +1,5 @@
test 1
-test 2
+sample 2
test 3
test 4
test 5
You can now capture the latest stash changes in a file like below
➜ repo1 git:(master) git stash show -p > /tmp/repo-changes-1
➜ repo1 git:(master) more /tmp/repo-changes-1
diff --git a/test.txt b/test.txt
index cd9fa46..556e1aa 100644
--- a/test.txt
+++ b/test.txt
@@ -1,5 +1,5 @@
test 1
-test 2
+sample 2
test 3
test 4
test 5
the output of git stash show -p
can be shared with anyone, and they can apply using git apply
as mentioned earlier.
Note: You can use git stash show -p stash@{3}
to get changes of a particular stash if you have multiple stashes.
I hope this helps.
– RC
Comments