DEV Community

loading...
Cover image for Top 5 Git Tips & Tricks
GitLive

Top 5 Git Tips & Tricks

Agnieszka Stec
Technology | Sailing
Originally published at blog.git.live ・4 min read

Becoming a Git power-user is on the bucket list of every developer. Today we prepared 5 Git tips that will help you level up your workflow and bring you one step closer to Git mastery.

Tip #1

Modify previous commit without changing the commit message

You’ve just committed your changes on your local copy with a detailed and thought-through message, but the moment you hit RETURN you realised you forgot to add that one change that really belongs there. If only there was a way to update the previous commit instead of creating a new one...

$ git commit --amend --no-edit
Enter fullscreen mode Exit fullscreen mode

This command lets you modify the last commit without changing the commit message. The hash value will be updated, but there will be only one commit record leaving your local Git history nice and clean.

Tip #2

Keep your commits organised

You just wanted to fix that one feature, but in the meantime got into the flow, took care of a tricky bug and spotted a very annoying typo. One thing led to another, and suddenly you realised that you’ve been coding for hours without actually committing anything. Now your changes are too vast to squeeze in one commit...

$ git add -p <filename>
Enter fullscreen mode Exit fullscreen mode

With git add -p (or git add --patch) you can choose which parts of code from a file you want to include in your commit. After running the command you will get the list of the options you can add to git add -p

By choosing s you can split the hunk into smaller pieces. From there you can simply choose the chunks you want to stage for commit (and omit those you don’t) by navigating with y and n (or go for any other option from the list).

Tip #3

Take me back to better times (when everything worked!)

Not your best day. You made changes you shouldn’t have and now everything is broken… Is there a way to undo those commits?

$ git reflog
Enter fullscreen mode Exit fullscreen mode

With this handy command, you can get the record of all the commits done in Git.

termianl view

Now you just need to find the commit prior to the one that caused all the hassle. HEAD@{index} represents the specified commit, so just replace index with the correct number and run:

$ git reset HEAD@{index}
Enter fullscreen mode Exit fullscreen mode

The default action for git reset is git reset --mixed. It means that the changes in your working directory will be preserved but not staged (since the index was modified to match the chosen commit, the changes are not in the index anymore).

Other options are:

$ git reset --soft
# Doesn’t modify the index or the working tree, leaving your changes staged for commit.
Enter fullscreen mode Exit fullscreen mode
$ git reset --hard
# Use with caution, as it resets both the index and working tree. Uncommitted changes and all commits after will be removed.
Enter fullscreen mode Exit fullscreen mode

And voilà, you can start over from the point when everything in your repository worked like a charm. Remember to use it only locally, as modifying a shared repository is considered a serious crime.

Tip #4

Let’s face those merge conflicts

You're on to a hairy merge conflict, but after comparing two conflicting versions you still have no idea which one is correct.

$ git checkout --conflict=diff3 <filename>
Enter fullscreen mode Exit fullscreen mode

Resolving merge conflicts is not all fun and games, but this command can make your life a little bit easier. Often you need more context to decide which branch is correct. By default, Git shows you a version of the markers that contains versions of the two files that have a conflict. By choosing the option above you will be able to see the base version as well, which can hopefully save you some trouble. You can also set it as default with:

$ git config --global merge.conflictstyle diff3
Enter fullscreen mode Exit fullscreen mode

💡 "Resolving merge conflicts is fun!" - said no one ever. The good news is that with GitLive you can get notified of conflicts before they occur. Gutter indicators show your teammates changes in your editor in real-time. Check out this blog post to find out more.


Tip #5

Let autocorrect take care of it

You’re pretty proud of your breakneck typing speed, but at the same time you can’t even remember how many times you typed “git stauts” instead of “git status” and it leaves you mildly annoyed.

$ git config --global help.autocorrect <integer>
Enter fullscreen mode Exit fullscreen mode

Git Autocorrect is a convenient option for all the impatient devs out there. The integer value represents a tenth of a second. Choosing 30 will give you 3 seconds to change your mind and stop the operation - otherwise Git will assume you meant the instruction most similar to the one you wrote. Don’t worry though, if you type something that is not even close to a Git command, Git will give up on guessing and print an error message instead.

Thanks for reading! Hope you found our Git-pick useful. Feel free to share your favorite hacks in the comments! Happy Git-ing!

Discussion (10)

Collapse
michaelcurrin profile image
Michael

I don't bother with the -p chunks in the terminal. VS Code and PyCharm do a great job and giving a neat UI for selecting a range of lines and staging those.

Also a time saver, if don't like git add, you can do this, which applies to named paths. If the file is untracked because it is new, you will have to use git add though.

# add and commit directory foo, bar and file.txt etc. Everything in current directory and below.
git commit .

# add and commit everything in foo/ and below
git commit foo/

# Target a file
git commit file.txt
git commit foo/file.txt

# Target files
git commit foo/ bar/*.js package*.json README.md about.md
Enter fullscreen mode Exit fullscreen mode
Collapse
michaelcurrin profile image
Michael

I like reflog for undoing a deleted branch or a bad rebase decision.

You can also use git checkout against a commit reference from git reflog, if you don't want to use reset.

I will check out the diff conflict thing.

I didn't know about the typo auto correction.
Oh and I don't bother typing git status, I aliased it to git st, which runs git status --short.

Collapse
stecagnieszka profile image
Agnieszka Stec Author

Thanks for the feedback and more insightful Git hacks :)

Collapse
yaireo profile image
Yair Even Or

Or just install TortoiseGit for windows which has a GUI - a thing invented decades ago so programmers won't have to memorize commands

Collapse
endershadow8 profile image
EnderShadow8

Lol the autocorrect doesn't help me all the times I type "giit"

Collapse
lukecp5 profile image
lukecp5

I'm just starting out in Git, and this is all very helpful information. Thank you so much for the tips!

Collapse
stecagnieszka profile image
Agnieszka Stec Author

Thanks! Good luck on your Git journey :)

Collapse
linhfishcr7 profile image
Ha Van Linh

Thanks so much!

Collapse
barbara66871045 profile image
Barbara O'Hara

Thanks for the article, very useful!

Collapse
steakeye profile image
Andrew Keats

Some good tips!