DEV Community

Sergiu Mureşan
Sergiu Mureşan

Posted on

3 GIT productivity features explained

I know there are some people in the community that use GIT on a day-to-day basis but only commit/pull/push while omitting some of the more useful features of it. I present to you 3 useful features, all related to committing, that will help your productivity and ease of change.

1) git stash

This is the simplest way to put what you have been working on away for a second in favor of an urgent bug fix. All you have to do is:

git stash
... do whatever it is needed to fix an urgent bug fix or whatever else
git stash pop

After you git stash all your uncommitted changes go away in a temporary place which you can re-apply using git stash pop.

2) Using multiple local branches

I know it has been said time and time again that anyone with a serious enough project should be using multiple branches for their project. That is not always true for all projects, but even so, if you only have one branch on the upstream it doesn't mean you can't use multiple branches on you own local repository. Here's how you do it:

git branch new-branch-name
git checkout new-branch-name
... commit whatever you want on this branch while keeping the main branch intact for more urgent modifications
git checkout master (or your main branch)
git merge new-branch-name

And voila! You have another way to quickly switch to another task if needed. Isn't that great?

3) Selective commits

Instead of committing all changes you can selectively choose which files go where using the git add command to add them and git commit -m ... to commit whatever you have added.

Really mate? This is the "productivity tip" you're teaching? I have been using that since I was 5!

I know I know, but bare with, I have made this mistake where I didn't know what the stage area was for and was always committing all my changes using git commit -am .... I never knew there was an alternative for quite some time

With that said, here are the steps:

git add file_you_want_to_commit
git commit -m "Message properly explaining your changes"
git add another_file_you_want_to_commit
git commit -m "Another message properly explaining your changes"

That has been it for this tutorial. Hope you got something out of it. I have published a video explaining these features a bit more in-depth. You can check it out here:

What other features have you wished you knew when starting to use git?

Top comments (6)

Collapse
 
chrispook profile image
Chris Pook • Edited

Thanks Sergiu,

When using git stash be aware that git stash pop will literally pop the stashed changes out of the storage, so you won't be able to pop it again later.

You may prefer to git stash list to see all your previously stashed states and then git stash apply stash@{X}(where X is the stash you want to pop back up).

This way the stashed code stays in the stash store as well as being applied, allowing you to later git stash apply again or git stash drop stash@{X} when you're definitely done.

Collapse
 
codevault profile image
Sergiu Mureşan

Thanks for the more in-depth explanation! I covered git stash apply a bit in the video but didn't explain about git stash drop.

One more useful feature I'd like to add, since we are expanding on this, is naming your stashes using git stash save "Name or message of the feature" since it can really get confusing with just the name of the commit you were working on.

Collapse
 
alexmacniven profile image
Alex Macniven

Thanks,

When merging I like to apply a couple of options;--no-commit and --no-ff allow you to review the merge before committing. I find this very useful when merging a development branch into a production branch

Collapse
 
codevault profile image
Sergiu Mureşan

Didn't knew this feature exists. I will have to tell my team as we might have found the solution to automatic merges that go wrong. Thank you for that very much!

Collapse
 
jeklah profile image
Jeklah

I was also in the habit of add and then commit -am until I realised the value of having a concise comment about what was done. Can't really have a long comment explaining everything that was done over lots of files, so best git practice is commit small and concise commits with comments often. :)

Collapse
 
codevault profile image
Sergiu Mureşan

Definitely, committing often is much easier to revert afterwards. I hate it when I have to revert a very large commit for just a simple feature x.x