DEV Community

Manikanta Yarramsetti
Manikanta Yarramsetti

Posted on

Essential Git Commands You Should Know

Git is the most popular version control system used by developers. Whether you’re new or need a quick refresher, here are the fundamental Git commands to boost your productivity.


1. Check Git Version

Verify the installed Git version:

git --version
Enter fullscreen mode Exit fullscreen mode

2. Configure Git (Username & Email)

Set your user name and email for commits:

git config --global user.name "Your Name"
git config --global user.email "Your Mail"
Enter fullscreen mode Exit fullscreen mode

3. Initialize a New Git Repository

Create a new Git repo in your current folder:

git init
Enter fullscreen mode Exit fullscreen mode

4. Clone a Remote Repository

Download a remote repo to your machine:

git clone https://github.com/user/repo.git
Enter fullscreen mode Exit fullscreen mode

5. Check Repository Status

See changed files and staging status:

git status
Enter fullscreen mode Exit fullscreen mode

6. Stage Files for Commit

Add a specific file to staging area:

git add <filename>
Enter fullscreen mode Exit fullscreen mode

Add all changes:

git add .
Enter fullscreen mode Exit fullscreen mode

7. Commit Changes

Commit staged files with a message:

git commit -m "Your descriptive commit message"
Enter fullscreen mode Exit fullscreen mode

8. View Commit History

Show commit logs:

git log
Enter fullscreen mode Exit fullscreen mode

9. Create a New Branch

Create a branch named feature-branch:

git branch feature-branch
Enter fullscreen mode Exit fullscreen mode

10. Switch to a Branch

Switch to the branch:

git checkout feature-branch
Enter fullscreen mode Exit fullscreen mode

Or create & switch in one command (Git 2.23+):

git switch -c feature-branch
Enter fullscreen mode Exit fullscreen mode

11. Merge Branches

Merge feature-branch into current branch:

git merge feature-branch
Enter fullscreen mode Exit fullscreen mode

12. Push Changes to Remote

Push your commits to the remote repository:

git push origin <branch-name>
Enter fullscreen mode Exit fullscreen mode

13. Pull Latest Changes from Remote

Fetch and merge changes from remote:

git pull origin <branch-name>
Enter fullscreen mode Exit fullscreen mode

14. Delete a Branch

Delete a local branch:

git branch -d <branch-name>
Enter fullscreen mode Exit fullscreen mode

15. Undo Last Commit (Keep Changes)

Undo last commit but keep changes staged:

git reset --soft HEAD~1
Enter fullscreen mode Exit fullscreen mode

Top comments (0)