Posted on

Top Git Commands You Should Know


Top Git Commands

Have you been using Git for a while, but only via IDE’s interface and feel like by this time you should know some more commands or just interested about what’s happening under the hood? Or you just switch platforms often and don’t want to learn new interface to do the same things you’ve been doing in other IDE’s? Then welcome, we’re going to talk about the basic Git commands you should know

Why Git Command Line

You might be totally fine with using IDE’s interface for all of your Git interactions though. But when switching, you have this little first time discomfort using different interface. Or you just feel like typing commands is way cooler. It is kinda. Plus typing can be faster then going through the interface details with a mouse.

I will split Git commands into two categories: Single use and Collaborative. If you work in a team, you’ll be using both of them, but if you have just a personal project, then you won’t need to use any of the Collaborative commands most likely. And it makes it easier to learn. Basically commands aren’t difficult to remember, it’s more to understand use cases.

Single use

Those are the basic commands that you use on a regular basis. First would be

Commit

which creates a new commit in your current brunch, but I prefer using a beautiful Android Studio interface, by the way the AS shortcut for commit is  Ctrl + K.  Because it displays the changed files since the last commit and you can choose which ones to add. Saving you from using

New Branch

The second would be to create a new branch

With this command you create a new branch named develop. Forget it, use

always, you check out from new branch as well. Saves you from typing two commands.

Stash

But what if you were making some changes and realized that it’s not the branch that you want to create a commit in? No problem, use

It stores all the changes that you made in a list and removes them from current branch. Now you can checkout from the branch that you want or create it. And then use

This command moves all those stored changes into your new branch and deletes it from the stash list.. And now you have your changes in a new branch.

I’m not going to talk about push command, it’s quite obvious, the only thing that I say about this is that Ctrl + Shift + K is an Android Studio shortcut for it. Useful when you get an error during pushing commit and need to retry.

Reset

That’s one of my favorite!  Let’s imagine this situation: you made one or few commits that you think are useless and want to go back completely to you previous commit. Then you need to get that commit’s sha1 by

and then

But if you try to push, then it will give you a message that there’s a commit found at remote branch and you need to merge it first, the commit that you removed from your local branch. So using this command you’ll remove those commits from remote

Or instead of –hard you can use –soft, resets you to that commit, but keeps all the changes till that commit as staged.

For example you need to return to the previous version in your commits, but don’t want to loose changes after that commit.

First Approach

you can use this command

after it your code won’t change, but the commits in between will convert into staged files and unstaged files will remain as well. Now just stash current changes

And you have reverted to that commit, all the changes after this commit will be in stash list which you can get back by

But if you want completely to return to the version that you had before – you’ll have to make new commits.

Second Approach

You can create a new branch

and then reset hard to the previous commit that you want

All changes after that commit removed and you can run your app at that commit version.  After you tested it you can just checkout from you previous branch and delete this reset_demo which we used just for reseting by

I think second approach is cleaner because you don’t have to make a new commit.

Undo changes in single file

Undo uncommited changes to file is really useful, but it’s not convenient to use it in command line, just look at this for example

Don’t know about you, but I wouldn’t want to type full file path manually, yes, you can drag ‘n’ drop a file into terminal. That’s ok if you’re in File Explorer, but if you’re already in your IDE, then I bet there’s a much faster way to undo changes to a file in its context menu.

Collaborative commands

You won’t use those if you have changes happening only from your side, because you’ll always have the latest code and no merge conflicts are going to happen. Well, they could appear if you make changes in multiple branches on the same modules, but this is very unlikely.

Pull

For collaborative I would put git pull, it does fetching commits from remote branch. Yes, it does it for current branch and merges it. If there’s any conflicts – you’ll have to resolve them and create a commit.

Fetch

Downloads branches from remote repository, you can run this command to fetch all branches at once

Merge/Rebase

I prefer merge command over rebase. Because it’s more straightforward and easier and nobody will complain if you use it.

This is one line approach instead of two: checkout, merge. Use any which is more convenient for you after all it’s all about writing software, not making beautiful Version Control history. Well, unless you work in a big corporation where there’s some manager checking every of your commits.

 

Alright, that’s the basic most useful Git commands that I can think of. I didn’t include init, log, status because they are too obvious. Thanks for reading, leave a comment about what command do you use more  – merge or rebase, I’m really interested about that. And subscribe if you want to receive weekly tutorials from me!