Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to git stash specific lines of a file?

Tags:

git

This is kind of a hard question to explain, mainly because I still don't know a whole bunch about git.

I just know the basics to be able to get by.

Currently I've been working on a bigger project and have changed a bunch of lines in a specific file. The update that I'm working on is still a ways away from being committed and pushed to git.

Lone behold there ended up being a smaller update that I had to make within that same file and now need to push that code instead of the semi working file currently.

Is there any way that I could stash certain lines of that file so that the bigger changes I made to that file don't get lost when I decide to push the smaller changes?

Or do I have to stash what I currently have now, and then go back into the file and remake the smaller changes again and push that?

Sorry if this is hard to understand, it's kind of difficult to explain, plus I don't know if there is a way of currently doing this with git anyways.

EDIT

I'm looking into using separate branches just like the second answer suggests. This seems like a more efficient solution, and will allow me to keep track of my fixes.

like image 636
Michael Avatar asked Oct 04 '19 19:10

Michael


People also ask

How do I apply a specific file in stash?

Apply Git stashes Now that you have saved your Git stashes on the side, you might want to “take them out from the stack” and apply them to your current working directory. In order to apply your Git stash to your current working directory, use the “git stash apply” command and specify the stash you want to apply.

Can you git stash multiple branches?

Multiple stash entries The most recent stash will have the reference stash@{0} . The stash list can contain stash entries from different branches, which could each be applied to other branches in your project.

How do I stash only staged files?

Stage all your files that you need to stash. Run git stash --keep-index . This command will create a stash with ALL of your changes (staged and unstaged), but will leave the staged changes in your working directory (still in state staged). Now your "good stash" has ONLY staged files.


2 Answers

You can stash specific lines from files by using the --patch option:

git stash --patch

git stash --patch  <filename>

Git will ask you interactively what you want to do with each file. You can edit the files or choose which lines get stashed

Note that you can also do this when adding files to your staging area with git add:

git add --patch  <filenames>

If what you want to do is commit part of a file, you can stage the part you want to commit with git add --patch, and there is no need to stash.

like image 58
David Sugar Avatar answered Sep 21 '22 18:09

David Sugar


Read about git branches, which allow you to work on fixes to multiple aspects of a project separately.

You should create one branch for the big ongoing changes, and a separate branch for the smaller update.

Example with more details: https://git-scm.com/book/en/v2/Git-Branching-Basic-Branching-and-Merging

like image 42
Burrito Avatar answered Sep 17 '22 18:09

Burrito