DEV Community

Cover image for Easy-Breezy Clarifications of Git Commands
S.M. Faizul Islam Fair
S.M. Faizul Islam Fair

Posted on • Edited on

Easy-Breezy Clarifications of Git Commands

Yes, yes, that's right guys! This is it, this is the perfect place to get a grasp on some tight git commands. Pretty explanations in just 1 or 2 sentences. So, let's get started.....

Git Commands

Image description

To setup username globally

git config --global user.name "username"
Enter fullscreen mode Exit fullscreen mode

To setup email globally

git config --global user.email "email"
Enter fullscreen mode Exit fullscreen mode

To change username globally

git config --global user.name "username"
Enter fullscreen mode Exit fullscreen mode

To change email globally

git config --global user.email "email"
Enter fullscreen mode Exit fullscreen mode

To check if the setup is complete

git config --list
Enter fullscreen mode Exit fullscreen mode

To exit the list

press 'q'(just q)
Enter fullscreen mode Exit fullscreen mode

To see the username

git config user.name
Enter fullscreen mode Exit fullscreen mode

To see the email

git config user.email
Enter fullscreen mode Exit fullscreen mode

To delete branch locally

git branch -D branch_name
Enter fullscreen mode Exit fullscreen mode

To delete branch remotely

git push origin --delete branch_name
Enter fullscreen mode Exit fullscreen mode

To download latest changes from a remote repository to local machine,

git fetch
Enter fullscreen mode Exit fullscreen mode

To remove a specific staged file (only added/staged, not committed) or unstage

git rm --cached file_name(with extension) 
Enter fullscreen mode Exit fullscreen mode

or,

git reset file_name(with extension)
Enter fullscreen mode Exit fullscreen mode

To remove all unstaged changes and move head to specific point

git reset commit_id
Enter fullscreen mode Exit fullscreen mode

To remove all staged and unstaged changes and move head to specific point

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

To remove all staged files (only added/staged, not committed) or unstage

git reset HEAD
Enter fullscreen mode Exit fullscreen mode

To remove all staged changes (even from files) and staged files(only added/staged, not committed) or unstage

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

To exit from git log press 'q' (just q)

To view all commits with ids in a clean way

git log --pretty=oneline
Enter fullscreen mode Exit fullscreen mode

To view all commits with ids in a clean way (shorter version)

git log --oneline
Enter fullscreen mode Exit fullscreen mode

To display changes between a specific commit and current head

git diff commit_id
Enter fullscreen mode Exit fullscreen mode

To abort merge after getting conflict message

git merge --abort
Enter fullscreen mode Exit fullscreen mode

To not lose changes by not committing yet and moving to another branch to work on something

git stash <br>
Enter fullscreen mode Exit fullscreen mode

or,

git stash -u 
Enter fullscreen mode Exit fullscreen mode

and then to apply changes after coming back later

git stash apply index_number(0)
Enter fullscreen mode Exit fullscreen mode

To save stash with a custom message

git stash push -m "message_name"
Enter fullscreen mode Exit fullscreen mode

To change name of most recent commit that hasn't been pushed to remote repo yet

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

To change name of most recent commit that has been pushed to remote repo

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

Plus

git push --force
Enter fullscreen mode Exit fullscreen mode

To forcefully revert back to the state of the latest commit, discarding any local changes and disregarding any conflicts or warnings

git checkout HEAD~ --force
Enter fullscreen mode Exit fullscreen mode

To move HEAD to a particular commit, push to repo and pull to master branch

git checkout commit_id
git add .
git commit -m "commit message"
git push origin HEAD:master --force
Enter fullscreen mode Exit fullscreen mode

Plus

git checkout master
git pull 
Enter fullscreen mode Exit fullscreen mode

To bring in changes from a specific commit

To bring in changes from one commit and commit directly in my branch

git cherry-pick commit_id
Enter fullscreen mode Exit fullscreen mode

or,

To bring in changes from more than one commit and commit directly in my branch

git cherry-pick commit_id_one commit_id_two
Enter fullscreen mode Exit fullscreen mode

To bring in changes from one commit and without committing directly in my branch

git cherry-pick commit_id -n
Enter fullscreen mode Exit fullscreen mode

then commit,

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

To undo changes introduced in a previous commit and commit

git revert commit_id
Enter fullscreen mode Exit fullscreen mode

Plus

:q (to exit)
Enter fullscreen mode Exit fullscreen mode

To replay all commits on our current branch on top of specified branch

git rebase branch_name
Enter fullscreen mode Exit fullscreen mode

To edit commit history before rebasing

git rebase -i branch_name
Enter fullscreen mode Exit fullscreen mode

Top comments (0)