DEV Community

Prarthana
Prarthana

Posted on

Git Commands

Here are 20 commonly used Git commands every beginner should know:

  1. git init: Initializes a new Git repository in your project folder.
   git init
Enter fullscreen mode Exit fullscreen mode
  1. git config: Configures user details such as name and email.
   git config --global user.name "Your Name"
   git config --global user.email "your.email@example.com"
Enter fullscreen mode Exit fullscreen mode
  1. git clone: Clones an existing repository from a remote source.
   git clone <repository_url>
Enter fullscreen mode Exit fullscreen mode
  1. git status: Shows the current status of the working directory and staging area.
   git status
Enter fullscreen mode Exit fullscreen mode
  1. git add: Adds files or changes to the staging area.
   git add <file_name>
   git add . # Stages all changes
Enter fullscreen mode Exit fullscreen mode
  1. git commit: Saves changes to the local repository.
   git commit -m "Your commit message"
Enter fullscreen mode Exit fullscreen mode
  1. git log: Displays the commit history.
   git log
Enter fullscreen mode Exit fullscreen mode
  1. git branch: Lists, creates, or deletes branches.
   git branch        # Lists branches
   git branch <name> # Creates a branch
Enter fullscreen mode Exit fullscreen mode
  1. git checkout: Switches between branches.
   git checkout <branch_name>
Enter fullscreen mode Exit fullscreen mode
  1. git merge: Merges a specified branch into the current branch.

    git merge <branch_name>
    
  2. git push: Pushes local commits to the remote repository.

    git push origin <branch_name>
    
  3. git pull: Fetches and merges changes from the remote repository.

    git pull origin <branch_name>
    
  4. git fetch: Fetches changes from the remote repository without merging them.

    git fetch origin
    
  5. git diff: Compares file changes.

    git diff
    
  6. git reset: Undoes changes in the staging area or moves the HEAD to a previous commit.

    git reset <file_name>
    git reset --hard <commit_hash>
    
  7. git revert: Reverts a specific commit by creating a new commit.

    git revert <commit_hash>
    
  8. git stash: Temporarily saves uncommitted changes.

    git stash
    
  9. git stash pop: Applies the stashed changes and removes them from the stash.

    git stash pop
    
  10. git rm: Removes a file from the working directory and staging area.

    git rm <file_name>
    
  11. git show: Shows details of a specific commit.

    git show <commit_hash>
    

These commands should give you a solid foundation for managing your projects with Git. Let me know if you'd like further clarification on any of them! πŸš€πŸ’»

Top comments (0)