DEV Community

Abhay Singh Kathayat
Abhay Singh Kathayat

Posted on

Essential Git Commands Every Developer Should Know

Essential Git Commands

Here’s a list of commonly used Git commands, categorized for convenience:


1. Setup and Configuration

  • Check Git Version:
  git --version
Enter fullscreen mode Exit fullscreen mode
  • Configure Username:
  git config --global user.name "Your Name"
Enter fullscreen mode Exit fullscreen mode
  • Configure Email:
  git config --global user.email "your_email@example.com"
Enter fullscreen mode Exit fullscreen mode
  • View Configuration:
  git config --list
Enter fullscreen mode Exit fullscreen mode

2. Repository Management

  • Initialize a Repository:
  git init
Enter fullscreen mode Exit fullscreen mode
  • Clone a Repository:
  git clone <repository-url>
Enter fullscreen mode Exit fullscreen mode
  • View Repository Status:
  git status
Enter fullscreen mode Exit fullscreen mode

3. Working with Files

  • Add Files to Staging Area:
  git add <file>   # Add a specific file
  git add .        # Add all changes in the current directory
Enter fullscreen mode Exit fullscreen mode
  • Commit Changes:
  git commit -m "Your commit message"
Enter fullscreen mode Exit fullscreen mode
  • Remove Files:
  git rm <file>
Enter fullscreen mode Exit fullscreen mode
  • Move/Rename Files:
  git mv <old_filename> <new_filename>
Enter fullscreen mode Exit fullscreen mode

4. Branching and Merging

  • 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
  • Create and Switch to a New Branch:
  git checkout -b <branch-name>
Enter fullscreen mode Exit fullscreen mode
  • Merge a Branch:
  git merge <branch-name>
Enter fullscreen mode Exit fullscreen mode
  • Delete a Branch:
  git branch -d <branch-name>
Enter fullscreen mode Exit fullscreen mode

5. Remote Repositories

  • Add a Remote Repository:
  git remote add origin <repository-url>
Enter fullscreen mode Exit fullscreen mode
  • View Remote Repositories:
  git remote -v
Enter fullscreen mode Exit fullscreen mode
  • Push Changes to Remote Repository:
  git push origin <branch-name>
Enter fullscreen mode Exit fullscreen mode
  • Pull Changes from Remote Repository:
  git pull origin <branch-name>
Enter fullscreen mode Exit fullscreen mode
  • Fetch Changes from Remote:
  git fetch
Enter fullscreen mode Exit fullscreen mode

6. Viewing and Inspecting

  • View Commit History:
  git log
Enter fullscreen mode Exit fullscreen mode
  • View a Specific Commit:
  git show <commit-hash>
Enter fullscreen mode Exit fullscreen mode
  • View Changes in Files:
  git diff
Enter fullscreen mode Exit fullscreen mode
  • View Changes in Staged Files:
  git diff --staged
Enter fullscreen mode Exit fullscreen mode
  • List Branches:
  git branch
Enter fullscreen mode Exit fullscreen mode

7. Undo Changes

  • Unstage Files:
  git reset <file>
Enter fullscreen mode Exit fullscreen mode
  • Undo Last Commit (Keep Changes):
  git reset --soft HEAD~1
Enter fullscreen mode Exit fullscreen mode
  • Undo Last Commit (Discard Changes):
  git reset --hard HEAD~1
Enter fullscreen mode Exit fullscreen mode
  • Discard Unstaged Changes:
  git checkout -- <file>
Enter fullscreen mode Exit fullscreen mode

8. Stashing

  • Save Changes Temporarily:
  git stash
Enter fullscreen mode Exit fullscreen mode
  • Apply Stashed Changes:
  git stash apply
Enter fullscreen mode Exit fullscreen mode
  • List Stashes:
  git stash list
Enter fullscreen mode Exit fullscreen mode
  • Drop a Stash:
  git stash drop
Enter fullscreen mode Exit fullscreen mode

9. Tags

  • Create a Tag:
  git tag <tag-name>
Enter fullscreen mode Exit fullscreen mode
  • List Tags:
  git tag
Enter fullscreen mode Exit fullscreen mode
  • Push Tags to Remote:
  git push origin <tag-name>
Enter fullscreen mode Exit fullscreen mode

10. Collaboration

  • Rebase:
  git rebase <branch-name>
Enter fullscreen mode Exit fullscreen mode
  • Cherry-pick a Commit:
  git cherry-pick <commit-hash>
Enter fullscreen mode Exit fullscreen mode
  • Resolve Merge Conflicts:

    • Edit conflicting files manually.
    • Stage resolved files:
    git add <file>
    
    • Complete the merge:
    git commit
    

These commands cover most Git functionalities for beginners and intermediate users. If you need clarification or advanced commands, feel free to ask! 😊

Top comments (2)

Collapse
 
rogueloop profile image
Sreedeep

you missed git pull --rebase

Fetches the latest changes from the remote branch and then applies your local commits on top of them - pretty useful right

Collapse
 
abhay_yt_52a8e72b213be229 profile image
Abhay Singh Kathayat

Great point! Let me clarify what git pull --rebase does:

When you use git pull --rebase, Git first fetches the latest changes from the remote branch (just like git pull normally does). But instead of merging them with your local commits, it rebases your commits on top of the remote ones. This effectively rewrites your commit history to make it look like your changes were applied after the remote changes.

This is especially helpful when you want to avoid creating unnecessary merge commits, which can clutter the history. By rebasing, you keep the commit history cleaner and linear, making it easier to read and follow.

Example Use Case:

Before Rebase:
You and a colleague are working on the same branch. Your colleague has pushed some changes, and you’ve made your own local commits. Now, you want to pull the changes and apply your local commits on top, rather than merging them, to avoid the extra merge commit.

With git pull --rebase:
git pull --rebase will fetch the changes from the remote branch, then reapply your commits on top of them, as if your work was based on the most up-to-date version of the branch.

Benefits of Using git pull --rebase:

Cleaner Commit History: Avoids unnecessary merge commits.
Easy to Read: Makes it easier for others to follow the project’s history.
Better Collaboration: Keeps your work in sync with others without the clutter of merge commits.
So, yes, git pull --rebase is indeed a handy command for maintaining a tidy and efficient Git workflow!