|
2 | 2 | layout: default |
3 | 3 | title: 4 - Advanced Git |
4 | 4 | nav_order: 6 |
5 | | -last_modified_date: "2025-05-06 10:46AM" |
| 5 | +last_modified_date: "2025-09-16 10:46AM" |
6 | 6 | --- |
7 | 7 |
|
8 | 8 | # Advanced `git` |
@@ -75,17 +75,38 @@ $ git stash list |
75 | 75 | stash@{0}: WIP on main: 26393a3 Another commit |
76 | 76 | stash@{1}: WIP on main: 4d931c4 Testing rebase |
77 | 77 | ``` |
78 | | -If you have only one stash listed, or you want to use the most recent stash (with index `0`), you can pull those files/changes back into your branch with |
| 78 | +If you have one or more stashes listed, you have a choice to either `apply` or `pop` |
| 79 | +the stash, which are two forms of pulling the changed file(s) back into your working |
| 80 | +directory. |
| 81 | + |
| 82 | +**`git stash apply 0`** - applies the changed file(s) of the stash into your project but |
| 83 | +leaves the stash. The `0` in this command indicates the index number of the stash. |
| 84 | + |
| 85 | +**`git stash pop 2``** - applies the changed file(s) of the stash into your project but |
| 86 | +deletes the stash. The `2` in this command indicates the index number of your stash. |
| 87 | + |
| 88 | +``` |
| 89 | +$ git stash apply <INDEX> |
| 90 | +$ git stash pop <INDEX> |
| 91 | +``` |
| 92 | + |
| 93 | +However, it is not always obvious what a stash contains, or what changes were incorporated |
| 94 | +into it. You can find out more about stashes with these commands: |
| 95 | + |
| 96 | +To show a summary of changes in a stash: |
| 97 | + |
79 | 98 | ``` |
80 | | -$ git stash pop |
| 99 | +$ git stash show stash@{<index>} |
81 | 100 | ``` |
82 | 101 |
|
83 | | -If you have more than one stash listed and want to specify which you want to re-incorporate, you need to `apply` the stash by index (`0`, `1`, `2`, ...) |
| 102 | +To show a detailed patch view within a stash: |
| 103 | + |
84 | 104 | ``` |
85 | | -$ git stash pop <INDEX> |
| 105 | +$ git stash show -p stash@{<index>} |
86 | 106 | ``` |
87 | 107 |
|
88 | 108 | ### Create a Branch from a Stash |
| 109 | + |
89 | 110 | There are other powerful stash options, such as creating a new branch based on the stash. To do this, list your stashes first to identify the index of the stash you want to work with: |
90 | 111 |
|
91 | 112 | ``` |
@@ -333,4 +354,4 @@ Practice the above skills by doing the following: |
333 | 354 | 8. In a repository with multiple commits, reset your commit history back 3 commits. |
334 | 355 | 9. In a repository with multiple commits, revert your commit history back 2 commits. |
335 | 356 | 10. Create a useful `git alias`. |
336 | | -11. Install the GitHub CLI and use it to list your repositories. |
| 357 | +11. Install the GitHub CLI and use it to list your repositories. |
0 commit comments