Logo

dev-resources.site

for different kinds of informations.

Essential Git Commands Every Developer Should Know

Published at
12/26/2024
Categories
gitcommands
versioncontrol
gittutorial
gitforbeginners
Author
abhay_yt_52a8e72b213be229
Author
25 person written this
abhay_yt_52a8e72b213be229
open
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 "[email protected]"
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! 😊

Featured ones: