DEV Community

khaled-17
khaled-17

Posted on

Top Git Commands Every Developer Should Know

Initialize a Repository:

   git init
Enter fullscreen mode Exit fullscreen mode

Clone a Repository:

   git clone <repository_url>
Enter fullscreen mode Exit fullscreen mode

Check Repository Status:

   git status
Enter fullscreen mode Exit fullscreen mode

Stage Changes:

   git add <file_name>
Enter fullscreen mode Exit fullscreen mode

Commit Changes:

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

View Commit History:

   git log
Enter fullscreen mode Exit fullscreen mode

Create a New Branch:

   git branch <branch_name>
Enter fullscreen mode Exit fullscreen mode

Switch to a Branch:

   git checkout <branch_name>
Enter fullscreen mode Exit fullscreen mode

Merge Branches:

   git merge <branch_name>
Enter fullscreen mode Exit fullscreen mode

Pull Changes from a Remote Repository:

bash
git pull origin <branch_name>

Push Changes to a Remote Repository:

bash
git push origin <branch_name>

Check Differences between Branches:

bash
git diff <branch1> <branch2>

Discard Changes in Working Directory:

bash
git checkout -- <file_name>

Undo the Last Commit (Keep Changes):

bash
git reset --soft HEAD^

Undo the Last Commit (Discard Changes):

bash
git reset --hard HEAD^

Stash Changes:

bash
git stash

Apply Stashed Changes:

bash
git stash apply

Create a Tag:

bash
git tag -a <tag_name> -m "Your tag message"

Fetch Changes from a Remote Repository:

bash
git fetch origin

Remove Untracked Files:

bash
git clean -fd

Top comments (0)