DEV Community

Terry Li
Terry Li

Posted on

18 basic git commands you don't want to miss

I do use some GUI to run git, but for most basic and simple actions I prefer using cli as it is way faster. Here is a summary of my most used git commands.

Create a new branch and tracking remote branch

git checkout -b branch_name origin/branch_name
Enter fullscreen mode Exit fullscreen mode

See status

git status
Enter fullscreen mode Exit fullscreen mode

Stage a file

git add file_name
Enter fullscreen mode Exit fullscreen mode

Unstage a single file

git reset file_name
Enter fullscreen mode Exit fullscreen mode

Stage all files

git add .
Enter fullscreen mode Exit fullscreen mode

Unstage all files

git reset
Enter fullscreen mode Exit fullscreen mode

Discard un-staged changes of a single file

git checkout file_name
Enter fullscreen mode Exit fullscreen mode

Discard all un-staged/staged changes

git reset --hard
Enter fullscreen mode Exit fullscreen mode

Commit staged changes

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

Add and commit all changes in one step

git commit -am 'message'
Enter fullscreen mode Exit fullscreen mode

Diff between un-staged files and staged files

git diff
Enter fullscreen mode Exit fullscreen mode

Diff between staged files and HEAD

git diff --cached
Enter fullscreen mode Exit fullscreen mode

See commits

git log
Enter fullscreen mode Exit fullscreen mode

Diff between HEAD and its previous commit

git diff @^ @
Enter fullscreen mode Exit fullscreen mode

Diff between any commit and its previous commit

git diff commit_hash^ commit_hash
Enter fullscreen mode Exit fullscreen mode

Reset to a commit and keep the changes un-staged

git reset commit_hash
Enter fullscreen mode Exit fullscreen mode

Reset to a commit and discard the changes

git reset commit_hash --hard
Enter fullscreen mode Exit fullscreen mode

As always: bonus

Add change to the previous commit but without having a new commit

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

That's it. Hope you enjoy it!

Top comments (1)

Collapse
 
olsard profile image
olsard

Great! Thanks for sharing.