DEV Community

Dhruv
Dhruv

Posted on

Basic Git Commands Essential git commands every developer should know

If you’re one of those developers who still don’t use any version control system, I don’t know how you’re still managing to get work done.
In this post, I’m focusing on important git commands that gets all (almost) your work done ( I know you’re a GUI person ).

1) git config

Utility : To set your user name and email in the main configuration file.
How to : To check your name and email type in git config --global user.name and git config --global user.email. And to set your new email or name git config --global user.name = “Dhruv Nenwani” and git config --global user.email = “nendhruv@gmail.com

2) git init

Utility : To initialise a git repository for a new or existing project.
How to : git init in the root of your project directory.

3) git clone

Utility : To copy a git repository from remote source, also sets the remote to original source so that you can pull again.
How to : git clone <:clone git url:>

4) git status

Utility : To check the status of files you’ve changed in your working directory, i.e, what all has changed since your last commit.
How to : git status in your working directory. lists out all the files that have been changed.

5) git add

Utility : adds changes to stage/index in your working directory.
How to : git add .

6) git commit

Utility : commits your changes and sets it to new commit object for your remote.
How to : git commit -m”sweet little commit message”

7) git push/git pull

Utility : Push or Pull your changes to remote. If you have added and committed your changes and you want to push them. Or if your remote has updated and you want those latest changes.
How to : git pull <:remote:> <:branch:> and git push <:remote:> <:branch:>

8) git branch

Utility : Lists out all the branches.
How to : git branch or git branch -a to list all the remote branches as well.

9) git checkout

Utility : Switch to different branches
How to : git checkout <:branch:> or **_git checkout -b <:branch:> if you want to create and switch to a new branch.

10) git stash

Utility : Save changes that you don’t want to commit immediately.
How to : git stash in your working directory. git stash apply if you want to bring your saved changes back.

11) git merge

Utility : Merge two branches you were working on.
How to : Switch to branch you want to merge everything in. git merge <:branch_you_want_to_merge:>

12) git reset

Utility : You know when you commit changes that are not complete, this sets your index to the latest commit that you want to work on with.
How to : git reset <:mode:> <:COMMIT:>

13) git remote

Utility : To check what remote/source you have or add a new remote.
How to : git remote to check and list. And git remote add <:remote_url:>

These are the commands that I feel are essential and get things done, at least for me. Comment here if you think I’ve missed something important or if something can be done differently.

This was originally published on Medium

Top comments (21)

Collapse
 
andy profile image
Andy Zhao (he/him)

Definitely git add -p too, for an interactive view of what you've made changes on.

Collapse
 
leomeloxp profile image
Leo Melo

This! git add -p was probably was helped me the most when swapping Source Tree for CLI git.

Collapse
 
loujaybee profile image
Lou (🚀 Open Up The Cloud ☁️)

Always git add -p !

Collapse
 
ratraze_54 profile image
ratraze

I think rebase is one thing that should be present in this list.
Personally, I use it quite often and it's really helpful, especially if you work in a team.

And interactive rebase allow to squash commits and keep your history clean and clear.

Collapse
 
garrincha_7 profile image
Benoit Goepfert

Totally agree, I use it so often I can't even imagine working without it now.

Collapse
 
tensaga profile image
TENsaga

Definitely! Extremely useful.

Collapse
 
hkly profile image
hkly

Also git diff, I use this all the time to review changes before committing them.

Collapse
 
leomeloxp profile image
Leo Melo

git log --graph if you want to see the classic git branch timeline view.

git log takes has a huge number of options, I normally use it with:

git log --color --oneline --decorate=no --graph
Enter fullscreen mode Exit fullscreen mode

Optionally I also passing --all in case I want to see all branches.

Collapse
 
virup profile image
Viru • Edited
Collapse
 
arvinderakgp profile image
Arvind Gupta

At Point 10). You can also use "git stash pop" for getting back locally save code.

for your reference find the difference between "git stash apply" and "git stash pop" in below link

stackoverflow.com/questions/152860...

Collapse
 
dimanjan profile image
Dimanjan

Also , Search

Search the working directory for foo():

git grep "foo()"
Collapse
 
k4ml profile image
Kamal Mustafa

To see history of changes at specific line:-

git blame -L337,+5 -- src/myproject/services.py

To see evolution at line 337:-

git log --pretty=short -u -L 337,337:src/myproject/services.py
Collapse
 
techgeekbuzz profile image
Techgeekbuzz

Hi Dhruv,

You have shared a nice list of Git Commands, I have shared my own GIT Commands List Please check, if you find anything which is missing from your list, please add them into your websites: techgeekbuzz.com/git-commands/

Collapse
 
thomaswdmelville profile image
Thomas Melville

I would replace merge with rebase, but that's just me :-)

Collapse
 
profplum profile image
profPlum

Definitely git bisect too, I though the idea of commits were cool but was really skeptical of the practicality of using them to debug. git bisect stream lines this very efficiently and is an incredibly useful debugging tool!