DEV Community

Cover image for Essential Git Commands: A Comprehensive Guide for Version Control
Adam Bahloul
Adam Bahloul

Posted on

Essential Git Commands: A Comprehensive Guide for Version Control

Git Commands


To initialize a repository:

  • git init: Initialize a project as a Git repository.

To connect to a remote repository:

  • Add or connect to a remote repository:
  git remote add origin <link-to-github-remote-repo>
Enter fullscreen mode Exit fullscreen mode
  • View connected remote repositories:
  git remote
Enter fullscreen mode Exit fullscreen mode

To manage file status:

  • View the status of files in your local repository (tracked, untracked, modified):
  git status
Enter fullscreen mode Exit fullscreen mode
  • Stage modified or untracked files:
  git add <file name>
Enter fullscreen mode Exit fullscreen mode

Use git add . to stage all unstaged files.

  • Unstage files:
  git reset
Enter fullscreen mode Exit fullscreen mode

To commit changes:

  • Commit staged files:
  git commit
Enter fullscreen mode Exit fullscreen mode
  • Commit staged files with a commit message:
  git commit -m "<commit message>"
Enter fullscreen mode Exit fullscreen mode

To push changes:

  • Push committed files to the remote repository in the specified branch. Use this command for the first push:
  git push -u origin <branch-name>
Enter fullscreen mode Exit fullscreen mode
  • Push committed files to the remote repository:
  git push
Enter fullscreen mode Exit fullscreen mode

To fetch and pull changes:

  • Fetch the most updated version of your local repository:
  git fetch
Enter fullscreen mode Exit fullscreen mode
  • Pull the fetched information into your local repository, updating it to the most recent version of the remote repository:
  git pull
Enter fullscreen mode Exit fullscreen mode

Top comments (0)