...and not get lost on the way.
Problem
For almost a year I've been a part of a big and mature JavaScript project. No frameworks. Just Node, JS and MVC. Often times when I am fixing a bug I have to jump into multiple files and classes for investigation. My open files tab gets full really quickly. My main problem is jumping between different solutions for a particular fix.
I want to perform some change in code, test it and leave it for later to go find a different approach. I repeat these steps a few times. Then, when I have a fix that is the best fit, in my opinion, I can make a PR for code review or discuss it with my team.
So ideally I would like to quickly switch between possible fixes.
For this, I have two approaches.
Save a diff file
git diff > fix1.diff
Git will generate a patch file with all the changes made to the repository. I can send this file to someone, open it in its own window to compare with the current state, etc.
Very easy for quick verification.
To apply this file:
git apply fix1.diff
This is the simplest workflow for progressively save your work between commits.
I have just a file with all of the changes.
This is nice and simple, but there are better solutions.
Git Stash
Stashing is saving work for later.
There are many great tutorials and documentation on this topic.
atlassian
git-scm
I found this 2 commands helpful in my case:
git stash save <message>
git stash apply
git stash save
will save the changes and clean my working directory, soo to continue working I must apply them back. (git stash pop
will also apply changes but they will be deleted from stash).
Now I have a saved point in working "timeline" that I can easily evaluate or revert back to.
This can also be done inside VScode(if you use it) with Gitlens plugin(Webstorm also has this functionality)
** Update
As highlighted by
git stash save
is deprecated. Please use git stash push
Micro-tip: personalized comments
I put a comment in this manner:
// @mch <what I think is happening here>
mch > my initials
Within editor, I've set up a rule to highlight @mch
string.
For VScode there is nice plugin: TODO
I've customized it with:
"todohighlight.keywords": [
{
"text": "TODO",
"color": "#000000",
"backgroundColor": "gold",
"borderRadius": "2px",
},
{
"text": "@mch",
"color": "#66ffdd",
"backgroundColor": "#116644",
"borderRadius": "2px",
},
],
This is helpful to quickly lookup all the places that cought my eye.
Ctrl + Shift + F for them with @mch
or use TODO plugin lookup.
These 3 tips help me with day to day work.
What are your hacks for productive work??
Disclaimer:
This post is my first blog'ish publication like ever ever :D
Thank you DEV team for making it possible for me to share :D
Top comments (9)
Liked personalized comments. I discourage people from using git stash. It's equally easy to create local branches. Cleaner approach and provides more flexibility in terms of rebasing on top of latest changes after a pull. With that, we can get rid of diff patches as well.
Nice tip, thanks :) I will test it with new bug.
Wow, yes or yes I'm going to apply these tips, specially the diff file. I just would like to remember that
git stash save
is deprecated and now you have to usegit stash push
instead.github.com/magit/magit/issues/3349
Thank you for the code review :)
How do you remember that you have
git stash
ed something if it wasn't immediately used?Inside VSCode, in Gitlens I can see all saved stashes. Inside a single stash entry, I have all modified files. Here I can just click a file to see the diff.
imgur.com/a/apoWEnl
Just by browsing the entries in the stash I can remember what it was about.
Can this be helpful?
I do the same thing with
# @abhnv:
. I also add a colon after my nickname. Otherwise it could mean that it was addressed to me.Nice recommendations, I will try using got stash and good plugin, thanks for sharing!
I'm glad you found it helpful :)