You can use git stash.
Source -
Often, when you’ve been working on part of your project, things are in a messy state and you want to switch branches for a bit to work on something else. The problem is, you don’t want to do a commit of half-done work just so you can get back to this point later. The answer to this issue is the git stash command.
Stashing takes the dirty state of your working directory — that is, your modified tracked files and staged changes — and saves it on a stack of unfinished changes that you can reapply at any time.
What you can is stash, go on your branch and stash the changes you want , then for every branch you want those changes on, simply use git stash apply
. If you use pop
instead it removes the state from the stash.
To list the entries or the part you want to keep etc, use
git stash list
- List the stash entries that you currently have. Each stash entry is listed with its name (e.g. stash@{0} is the latest entry, stash@{1} is the one before, etc.)
git stash drop
- Remove a single stash entry from the list of stash entries. When no is given, it removes the latest one. i.e. stash@{0}
With those commands coupled you can definitely manipulate the changes you want at a finer level. You can use the man pages or the official documentation for specific commands options. Also,Recovering stash entries that were cleared/dropped erroneously.
- If you mistakenly drop or clear stash entries, they cannot be recovered through the normal safety mechanisms
A stash entry is represented as a commit whose tree records the state of the working directory, and its first parent is the commit at HEAD when the entry was created. The tree of the second parent records the state of the index when the entry is made, and it is made a child of the HEAD commit. The ancestry graph looks like this:
.----W
/ /
-----H----I
where H is the HEAD commit, I is a commit that records the state of the index, and W is a commit that records the state of the working tree.