DEV Community

Cover image for Git Commands ( to ease your development πŸ€— )
Shivam
Shivam

Posted on • Updated on

Git Commands ( to ease your development πŸ€— )

Git is a prevalent version control system ( VCS ) used by most programmers. In this blog, I am going to share some tips with basic commands to make using git easier.

To start with git, we need to understand how things work locally on the system while using git.

Workflow in Git

This diagram will help to understand, what actually happens while using git locally. Starting with the working area, the working area is where the files that are not handled by git, it is the place where all the untracked files are kept. Staging area has files that are going to be part of the next commit. The repository contains all the project commits. Using the repository we can have files as it was at some point in time.

Git Commands:

1. Initializing git in your directory

The very first step that you need to do is to initialize the repository.

git init
Enter fullscreen mode Exit fullscreen mode

2. git config

This command helps you to set the username and user email address to be used with your commits respectively.

git config --global user.name "Shivam164"
git config --global user.email "sp...oo6@gmail.com"
Enter fullscreen mode Exit fullscreen mode

3. git status

This command is used to see the state of the working area and staging area.

 git status
Enter fullscreen mode Exit fullscreen mode

3. git add

This command is used to add files to the staging area.

To take all the files that are untracked in the working area to the staging area, we use:

git add .
Enter fullscreen mode Exit fullscreen mode

Here β€˜.’ specifies to stage all the untracked files.

To specify files that should be tracked, we can use their file name:

git add abc.txt
Enter fullscreen mode Exit fullscreen mode

In order to stage abc.txt only.

4. git commit

The git commit command captures a snapshot of the project’s staged changes. Generally, we keep a short message with each commit in order to understand that commit in the future as well as for other people to understand.

β‡’ It is good to keep commits in the present tense and short ( about 50 characters ).

git commit -m "first commit"
Enter fullscreen mode Exit fullscreen mode

Here β€˜-m’ is for the message.
To stage all your changes and commit them in single command we can use:

git commit -am "committing all changes"
Enter fullscreen mode Exit fullscreen mode

There can be cases when you cannot explain your work in few words, even in such a scenario we should not just write a very big message in a single line, because that will be tough to read such messages on GitHub. For such a scenario after staging some file, we don't just directly commit like above. We write:

git commit
Enter fullscreen mode Exit fullscreen mode

When we write git commit without -m. You will see a console text editor known as Vim.

Vim

Now you can start writing your commit message here, you just need to press I (INSERT). And even within the long message, you should keep a short line at the start to before the long message, then after a line space, you can write your long message. When you are done writing your message, you can close this vim by first pressing β€˜ESC’ and then β€˜:wq’ to save and quit.

Git long commit messages

One of other useful git commit command:

git commit --amend -m "commit message'
Enter fullscreen mode Exit fullscreen mode

This command will merge this commit with the last commit and it will have a new commit message.

5. git log

The git log command displays all of the commits in a repository’s history.

There are many ways to see commit history :

  • Most basic one: Shows all the commits from end to start, with all the info related to commits.

    git log
    

    It shows commits based on the HEAD.

  • To see some number of the last commits:

    git log -5
    

    To see the last 5 commits.

  • To see commit from some commit to some other commit: We use the hash value of those commits.

    git log af156ff..32509d0
    
  • To see the stat of the commits:

    git log --stat
    

    This command gives the basic info along with the summary of number of changes made between each commit.

  • One of the handiest git log commands:

    git log --all --oneline --graph --decorate
    

    β‡’ all: to see all the commits

    β‡’ oneline: to keep all the data of commits within one line.

    β‡’ graph: to keep commits in graphical form, very useful when working with branches.

    β‡’ decorate: to make it visually better.

6. git show

The git show command will help to see details of a particular commit.

git show 1c16945
Enter fullscreen mode Exit fullscreen mode

git show

7. git diff

The git diff command is to see the difference between different commits.

git diff 1c16945..9dcff6
Enter fullscreen mode Exit fullscreen mode

To see the difference between commit with hash value 1c16945 and 9dcff6.

git diff

We can also use it directly.

git diff
Enter fullscreen mode Exit fullscreen mode

This will display the difference in files of the working area and directory.

We can use - - staged with git diff

git diff --staged
Enter fullscreen mode Exit fullscreen mode

This will display the difference in files of the working area and staging area.

8. git stash

The git stash temporarily shelves (or stashes) changes you’ve made to your working copy so you can work on something else, and then come back and re-apply them later on.

Only the files that are in the staging area can be stashed.


git stash
Enter fullscreen mode Exit fullscreen mode

After using git stash, if we use the command git status we will not see any file in the staging area.

We can also give a message while using git stash so that if we need it later we will be able to recall what we had done with the stashed files.

git stash save "changed title"
Enter fullscreen mode Exit fullscreen mode

git stash

To view all the stashed changes:

git stash list
Enter fullscreen mode Exit fullscreen mode

To view some particular stash, let’s say with index 1

git stash show -p stash@{1}
Enter fullscreen mode Exit fullscreen mode

To retrieves stash changes:

  • To retrieve last stashed change.
git stash pop
Enter fullscreen mode Exit fullscreen mode
  • To retrieve stash change with particular index
git stash pop stash@{1}
Enter fullscreen mode Exit fullscreen mode

To delete stashed changes:

  • To delete all
git stash clear
Enter fullscreen mode Exit fullscreen mode
  • To deleted some specific ones
git stash drop stash@{0}
Enter fullscreen mode Exit fullscreen mode

9. git branch

The git branch command is used to work with branches in git.

Creating new branch:

  • From the branch where HEAD is currently pointing to:

    git branch newBranch
    
  • From a branch where HEAD is not currently pointing to:

    git branch newBranch fromBranch
    
  • Creating a new branch and switching to it:

    git checkout -b newBranch fromBranch
    

Switching to different branch:

git checkout toSwitchBranchName
Enter fullscreen mode Exit fullscreen mode

Comparing different branches:

git diff master..new_feature
Enter fullscreen mode Exit fullscreen mode

Renaming branches:

To change the name of the branch, HEAD is currently pointing to:

git branch -m newBranchName
Enter fullscreen mode Exit fullscreen mode

To change the name of the branch irrespective of HEAD:

git branch -m oldName newName
Enter fullscreen mode Exit fullscreen mode

Deleting branches

git branch -d branch_to_delete
Enter fullscreen mode Exit fullscreen mode
  • If we are currently on the branch that we are trying to delete, we will get error.

    So we have to make sure that we are not on branch we want to delete.

  • If the branch we want to delete is unmerged and has commits than this command won’t work. Git does this to stop accidental deletion of branches.

    So we need to use β€˜-D’ instead of β€˜-d’

10. git merge

The git merge command lets you take the independent lines of development created by git branch and integrate them into a single branch.

git merge feature/Nav
Enter fullscreen mode Exit fullscreen mode

The above written command will merge feature/Nav branch with current checked out branch.

Some of the good practices while merging branches, you can checkout here

Currently it has commands only related to handling files locally. I will be updating it soon. πŸ‘Š

Top comments (29)

Collapse
 
dakdad profile image
Asanka

Good summary for useful basic git commands.

git stash pop only retrieves the first item (stash@{0}) from the stash list and then the index is refreshed (stash@{1} if it was there becomes the stash@{0} to take the place of the one popped out)

Collapse
 
shivam164 profile image
Shivam

Thanks for correction, done updating. πŸ‘Š

Collapse
 
protium profile image
protium • Edited

Aliases are also totally needed for productivity πŸ’ͺ🏼. Theses are the ones I use the most:

# checkout or create new branch
ck = "!f() { git checkout $1 2>/dev/null || git checkout -b $1; }; f"

sync = !git pull origin main --rebase
up = !git push -u origin `git branch --show-current`

main = checkout main
Enter fullscreen mode Exit fullscreen mode
Collapse
 
ritamchakraborty profile image
Ritam Chakraborty


git commit -am "Your commit message"

Changed my life. Read more about it here

stackoverflow.com/questions/429896...

Collapse
 
shivam164 profile image
Shivam

Added ✌️

Collapse
 
zaff profile image
Zafer Gurel • Edited

If you're working with a remote repository and want to see the location of this remote repo, the following command might be helpful:

git remote -v

In addition to that, tagging is also a crucial feature of git. So, git tag command is also worth exploring.

Collapse
 
erikgmatos profile image
Erik Garces Matos

Good summary,

A tip:
We also can to put untracked files in stash, like this:

git stash --include-untracked

or

git stash -u

Collapse
 
shivam164 profile image
Shivam

Sure I will be adding it soon. Thanks for suggestion

Collapse
 
nstvnsn profile image
Nathan Stevenson • Edited

Let me introduce you to git reflog!

While git log shows us the repository's public commit history, git reflog (or git reference log) shows us our workplace-specific local commit history.

I'll tell you why this is important.

Ever accidentally mess up a rebase and lose everything in the last few commits? You check you log and there's no trace of the last several hours worth of work to be found? Oops

With reflog, no problemo. It will shown you those commits you thought were gonezo. Run the command, identify the latest commit, make note of the branch tip and run:

git reset --hard my-branch-tip-before-rebase

A sanity saver, for sure. Learned from experiencing my own moment of panic.

Collapse
 
theme_selection profile image
ThemeSelection

Great one...!!

Collapse
 
yolitzr profile image
Yolit ZacarΓ­as

Excellent summary!!

Collapse
 
minhaj48 profile image
Minhaj T

really helpful :)

Collapse
 
robsonmuniz16 profile image
Robson Muniz

Great Content, thanks for sharing itπŸ™
I'm working on a video explaining how to use Git Version control in VS Code.
Happy Coding my friends!

Collapse
 
andrewbaisden profile image
Andrew Baisden

Very good article!

Collapse
 
leeuw profile image
leeuw

appreciated bro πŸ‘.

Collapse
 
zabdeldjallil profile image
Djilou

Awesome post, will surely share it with my co-workers πŸ‘ŒπŸ‘‹

Collapse
 
yongchanghe profile image
Yongchang He

Thank you for sharing this article!

Collapse
 
moose_said profile image
Mostafa Said

Great article! Very useful and informative πŸ‘