If you’re a developer, you know that Git is a powerful tool for managing your projects. But what if you want to make some changes to a project, but don’t want to lose all the work that’s been done in the past? There are a few ways to save changes in Git, and we’ll show you how to do it in this article.
- Save Changes as a File The first way to save changes is to create a file called “git-commit-msg” in your project’s root directory. This file will contain the text of your commit message, and any other important information about your commit. To save changes into this file:
- Open up your editor of choice (Git Bash or Emacs), and create a new file called “git-commit-msg” inside of the “git” directory. This will be the text of your commit message, as well as any other important information about your commit.
- Paste the contents of “git-commit-msg” into the new file. You can use any text editor you like, but we recommend using Git Bash because it’s easy to use and has many features available from its built-in tools.
- Save the file by pressing Ctrl+S (Win) or Cmd+S (Mac). You can also save the file as an image using the following command: # git commit -am ‘added some new code’ # git push origin master
Need to switch to a different branch, but you’re not ready to commit the changes you’ve made in your current branch? You can stash your changes and come back to them later. It’s possible in Git, whether you use GitHub or another hosting service.
Why Stash Your Changes?
Stashing your changes is a great way to keep up with your current work without committing them to the working branch. This allows you to work between several branches without pushing any changes.
There are several cases in which you may need to stash your changes. Let’s say, for example, that you’re working on branch A. However, there’s a serious bug in the code of branch B that needs your immediate attention. You need to switch over to branch B to fix the bug, but you’re not ready to commit the work you’ve been doing in branch A.
Thanks to git stash, you can stash your changes in branch A without pushing them, switch over and fix the bug in branch B, and then switch back to branch A and pick up where you left off.
How to Stash Changes
You can stash your changes by running a simple command. Before doing that, though, you can run a different command to see exactly what you’ll be stashing. In your working branch, run this command:
This will show you both the staged and unstaged changes you’ve made in your branch. In our case, we’ve modified the “test.md” file. Keep in mind that git stash will stash both staged and unstaged changes.
Now that you’ve reviewed what will be stashed, run this command to stash the changes:
Once executed, you’ll then receive a message stating that your changes have been stashed on
View Stashed Changes
If you’ve saved several stashes, you may want to see a list of the stashes before you try to retrieve one. When you view a list of your stashes, take note of the name of the stash you want to retrieve and continue working on.
In the terminal, run this command:
A list of stashes will then be returned. In the example above, our stash name is stash@{0}. The number inside the curly brackets is the index. If you have several stashes on the same branch, the number will be different.
If you want to view the details of a stash, run:
You can also run git stash show -p to view the results in diff format.
Retrieve Stashed Changes
Once you’re ready to pick up where you left off, you’ll need to retrieve your stashed changes. There are two different ways you can do this. One command will keep a copy of your changes in the stash while also copying it over to your working branch. The other will copy everything over to your working branch, but will remove everything from the stash.
To keep a copy of your changes in the stash and also bring them over to your working branch, run:
To bring the changes to your working branch but delete the copy of the stash, run:
If there are multiple stashes on a single branch, simply add the name of the stash to the end of the command.
You can now continue working with your previous changes. Once you’ve made all the necessary changes to the branch and have merged it to the main branch, don’t forget to delete the branch to keep your repository clean!
RELATED: How to Delete a Branch on GitHub