DEV Community

Cover image for Handy Git Commands and practices to improve your workflow

Handy Git Commands and practices to improve your workflow

Ted Ngeene on January 16, 2021

I have been in active software development for the better part of the last year and a half. For the majority of that period, I've come to appreciat...
Collapse
 
michaelcurrin profile image
Michael Currin

Thanks for sharing. I rarely use git rm - I usually use rm and commit. And I rename the path to something else if I need to keep it.
I added to my notes regarding cached and -r flags though.

I don't need to use git branch for a new branch. Your usage of git checkout -b is much more common and practical.

And pro tip. I rarely use add if a file already exists...

If i make file changes, then i do this for the directory (or use a dir name).

git commit .
Enter fullscreen mode Exit fullscreen mode

You can also check the comments in your commit message editor window if you need to see which paths are going to be committed. Or use git status before you commit.

Here I commit changes to package.json and package-lock.json without using add but preventing changes in other files getting commited.

git commit package*
Enter fullscreen mode Exit fullscreen mode
Collapse
 
tngeene profile image
Ted Ngeene

this is a new way of looking at things. Really helpful. Thanks Mike.

Collapse
 
neurovelho profile image
Teemu Hirvonen

That's a very nice list! The bit about descriptive commit messages and pull requests is especially important. It's not very nice navigating a codebase where half the commits are "updated stuff" and "implemented thingamajig".

Here's a couple of more commands if you want to show off a bit to other developers in your team (or maybe make your and your teams lives a bit easier 😄):

$ git config --global alias.co checkout
Enter fullscreen mode Exit fullscreen mode

Comes in pretty handy if you're tired of having to write git checkout [branch] every time you want to change branches. Can just do git co [branch]. You can of course create aliases for other stuff like commit, etc.

Another cool command is rebase. Many people use it mainly for rebasing master onto a feature branch but you can also use it for cleaning up commits on your feature branch. Here's a pretty cool article showing how to do it.

Collapse
 
tngeene profile image
Ted Ngeene

Thanks! I didn't know this. I tried it out and works like a charm!

Collapse
 
singhrahul31 profile image
singhrahul31

Just a query here. So we take out a branch from the 'main' as 'code fix'. Once done with the changes we would commit & push the branch to the repo. Checkout the 'main' branch and then merge the 'code fix' to the main
Am i saying this right or missing out on something?

Collapse
 
tngeene profile image
Ted Ngeene

Yeah, you're saying it right. But the appropriate way is to push it to the repo and wait for it to be merged. The only reason you'd merge locally is when you're working alone. But it's advisable that you push to the remote, once it's been merged then you can pull from the master.

Collapse
 
watiriroyalty profile image
Watiri Kambo

Nice read.....you made it so easy to understand 😀

Collapse
 
realtoughcandy profile image
RealToughCandy.io

Useful list here for Git newbies and experienced users alike.

Collapse
 
willteksoftwares profile image
willteksoftwares

Very well explained Teddy. Nice one.

Collapse
 
bmuskalla profile image
Benjamin Muskalla

Nice one! You may be interested in this one as well as explaining some of the cool features behind git log/blame: dev.to/bmuskalla/git-archeology-1nl3

Collapse
 
tngeene profile image
Ted Ngeene

nice article...