10 most used git commands!

Rabindra Joshi
PROSHORE
8 min readAug 21, 2019

--

In modern-day software development, git is synonymous to the version control system. It facilitates smooth development mechanism in a team of developers working in parallel.

If you use git everyday for collaboration and version control, take it to the next level. Don’t get stuck with the basic usage!

Those same commands you use every day can improve developer experience with tiny changes to them.

1. git status

git status

Do you have everything committed and pushed?

On branch [branch-name]
Your branch is up to date with 'origin/[branch-name]'.
nothing to commit, working tree clean

Have you modified the existing files and added new files (untracked)?

On branch [branch-name]
Your branch is up to date with 'origin/[branch-name]'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: path/to/modified/file.ext
modified: path/to/another/modified/file.ext
Untracked files:
(use "git add <file>..." to include in what will be committed)
path/to/untracked/file.extno changes added to commit (use "git add" and/or "git commit -a")

If you've got a lot of files and status becomes too long for your screen, try with a sb flag:

git status -sb

s flag shows the short output while b shows the branch name (branch name is shown in long output only, by default)!

Output:

## [local-branch-name]...origin/[remote-branch-name]
M file1.md
D file2.js
?? file3.md
?? relative/path/to/file4.txt
?? relative/path/again/file4.txt

??: new (untracked) file, D: deleted file, M: Modified

The default git status is same as git status --long. Well, it shows a long output.

Don’t want to see the untracked file in the list?

git status --untracked-files=no 
# Or git status --u=no

Lists the modified files only.

Want the command even shorter? Why not?

git status -uno

2. git branch

No, you don’t create a new branch with git branch. Refer git checkout instead.

List available local branches.

git branch

Output:

  master
development
* test
another-branch

You’re currently in the test branch, denoted by preceding asterisk (*) symbol.

Would you like to see all remote branches as well?

git branch -a

Remote branches will be listed as remotes/origin/[branch-name]

Delete a branch (Locally)?

git branch -d [branch-name]

You can’t delete the branch you’re working on. You’d fall down the tree, right? Git wants you safe!

error: Cannot delete branch 'test' checked out at '/path/to/local/git/repo'

If you’re on a different branch and haven’t merged everything on the test branch, it’ll still throw some unmerged errors

warning: not deleting branch '[branch-name]' that is not yet merged to
'refs/remotes/origin/[branch-name]', even though it is merged to HEAD.
error: The branch '[branch-name]' is not fully merged.
If you are sure you want to delete it, run 'git branch -D [branch-name]'.

If you’re sure to delete it, pass the D flag.

git branch -D [branch-name]

Now, you can delete it successfully. Be careful!

3. git remote

Show remote:

git remote -v

Output:

origin [repo-url] (fetch)
origin [repo-url] (push)

GitHub, GitLab and BitBucket use same alias (origin) by default, you can change it with:

git remote rename [old-alias] [new-alias]

Example: git remote origin test will change remote alias to test.

Verify with git remote -v

test [repo-url] (fetch)
test [repo-url] (push)

NOTE: The origin above, and in many places in the post, refers to a particular remote repository. The same repo could have multiple remotes. If origin refers to GitHub remote and heroku refers to Heroku’s remote, git push heroku master will push the master branch to Heroku while git push origin master will push it to GitHub.

4. git Log

git log

You’ll see output like this:

commit [commit-hash] (HEAD -> master, origin/master)
Author: [author-name] <[author-email]>
Date: [date-time]
[commit-message-1]commit [commit-hash]
Author: [author-name] <[author-email]>
Date: [date-time]
[commit-message-2]commit [commit-hash]
Author: [author-name] <[author-email]>
Date: [date-time]
[commit-message-3]

In (HEAD -> master, origin/master), HEAD shows the current reference in the branch master (local), and origin/master(remote).

HEAD is always the last commit in the currently check-out branch.

If you’ve multiple branches, it’ll show the state of all branches.

commit [commit-hash] (HEAD -> [local-branch-name], origin/[remote-branch-name])
Author: [author-name] <[author-email]>
Date: [date-time
[commit-message-1]commit [commit-hash] (origin/[local-branch-name], origin/HEAD, [remote-branch-name])
Author: [author-name] <[author-email]>
Date: [date-time]
[commit-message-2]commit [commit-hash] (origin/[local-branch-name])
Author: [author-name] <[author-email]>
Date: [date-time]
[commit-message-2]

If a particular commit doesn’t have anything equivalent on remote, the only local branch is shown.

If you have to read a long log on not an extremely tall screen, here’s what to do.

git log --graph --all --decorate --oneline --date=short

The graph flag will help to visualize the creation and merge of the branches.

oneline will display one commit in one line. Unfortunately, date and oneline flags don’t work together. You can see logs, but without a date.

What’s the fix? You can format the output yourself.

git log --graph --pretty=format:'%C(yellow)%h %ad %Cblue%an%C(auto)%d %Creset%s' --date=short --decorate

If you pass --all , you’ll get the log from all branches.

git log --graph --all --pretty=format:'%C(yellow)%h %ad %Cblue%an%C(auto)%d %Creset%s' --date=short --decorate

%h: abbreviated commit hash. Want full? Use %H instead.

%Cblue: color blue

%c(COLOR): color from config

%Creset: reset color

Full reference here.

5. git Checkout

Checkout to another branch, if available.

git checkout [branch-name]

Create a new branch?

git checkout -b [new-branch-name]

This new branch will carry the history of the branch it was checked-out from.

That’s not all, you can also check out to a particular commit,

git checkout [commit-hash]

It doesn’t have to be a full hash, abbreviated hash (8 characters) is more than enough for most times. If you enter fewer and it happens to be ambiguous, git will throw a hint with candidates!

git checkout fc14

Output

error: short SHA1 fc14 is ambiguous
hint: The candidates are:
hint: fc1418b8 commit [commit-date] - [commit-message]
hint: fc143b6b commit [commit-date] - [commit-message
error: pathspec 'fc14' did not match any file(s) known to git.

Use some more characters from the hint hash for successful checkout.

This detaches the HEAD, you’re a free bird. Look around, debug the feature and if you need to stay then here create a new branch from currently checked-out commit with git checkout -b [new-branch-name]

You can also switch to the previous branch with git checkout -, particularly useful if you switch between two branches frequently and branches have long names.

6. git add

Add your new files and changes to the staging area. You can only commit the files you add, and only committed ones can be pushed to the remote. The reverse is also true, you can have orphan (untracked) files in your working space and keep using git with tracked files only.

git add [file-name]

To add a particular file inside a nested directory:

git add [/path/to/the/file.ext]

Add all files in the current directory

git add .

Add all files from a particular directory

git add ./relative/path/to/directory

Add all changes in all folders within repository when you’re not in the root directory.

git add *

You can also add files of a particular extension

git add *.js

Add files of particular extension in a recursive manner.

git add **/*.js

Undo Add:

You’ve staged the file and you’d like to unstage the changes. You’ve got this!

git reset -q HEAD --

7. git commit

Commit your files to staging areas in your git repository.

If you make changes after committing, commit those again. Add the files before you commit.

git commit -m "[commit-message]" 

Add and commit at once?

git commit -am "[commit-message]"

This only adds already tracked files and won’t commit if you’ve added new files.

Wish to change the commit message? You can!

git commit --amend

It’ll open the default editor for a new commit message. But, you can also pass the commit message with the same command.

git commit --amend -m "[new-commit-message]"

Add new changes to the same commit?

First, add the file(s).

git add [file-or-path]

Then, run commit with --amend .

git commit --ammend -m "[new-commit-message]"

If you’d like to commit the added file(s) but keep commit message same, you don’t have to remember the earlier message!

git commit --amend --no-edit

Un-commit the last commit? Sure thing!

git reset --mixed HEAD~

8. git push

Push your local branch to remote.

git push

It’ll push to whatever remote branch it is tracking.

Push to a particular branch

git push origin [branch-name]

PS. origin is an alias for a particular remote.

Created a branch locally, and has no remote upstream to track? Set up a new one!

git push --set-upstream origin [remote-branch-name]

It has shorthand as well!

git push -u origin [remote-branch-name]

Most of us like to keep remote and local branch name same.

9. git stash

You’re working on a branch, you’d like to switch to a different one. How do you do that? Commit the changes and checkout, right? Wrong!

Commit after you’re done with a particular feature or whenever you’ve got a part of it right. Do not commit unnecessarily, it’ll be a lot of hassle if you’ve to revert a particular feature.

This is where you should use stash.g your draft to proshore, review and submit y

git stash push

This will push your changes to stash, and working space will switch to the last commit. You can also use git stash, without a push. Now, you can check out to a different branch.

Check if you’ve something stashed with

git stash list

Example Output:

stash@{0}: On [branch-name]: [stash-message]
stash@{1}: WIP on [branch-name]: [commit-hash] [commit-message]
stash@{2}: WIP on [branch-name]: [commit-hash] [commit-message]
(END)

Pass a message with a stash entry: git stash push -m "[stash-message]".

The default stash message is the hash and message from the last commit.

Show the files changed in the stash:

git stash show

Pass an index you see in the stash list for changes in the particular stash.

git stash show 1

Example Output:

filename.ext | 2 ++
1 file changed, 2 insertions(+)
(END)

Pop the changes out of stash

git stash pop will apply changes on top of the current file(s). It reverses the stash push.

Apply the changes from stash without removing it from the stash list.

git stash apply

You can pass the index of stash entry with both stash pop and apply like stash apply 1, and stash at index 1 will be applied.

Remove a stash from the list with stash drop. If you pass an index, it’ll remove the stash at that index. stash drop 1 will drop stash at index 1, the default index is 0.

Drop all entries in the stash with git stash clear, and you’ll have a clean working space.

What are your tiny tricks with the git commands? Do you plan to pick some from this article? Post your thoughts in the comment below.

--

--