Skip to main content

Command Palette

Search for a command to run...

Basic Git Commands

git cheatseat

Updated
2 min read
Basic Git Commands

To get started with Git, First download Git for Windows/MacOS/Linux from the official website https://git-scm.com/downloads and after installing it.

create a folder on Desktop and right-click on the folder choose the option "git bash here" & start with initializing the empty repository using the git init command. To open VScode write code . and create any type of file like HTML, CSS, javascript, python etc, and follow the below commands to push the code to GitHub.

🏁 Basic Git Commands

  1. Initialize a Git repository in your project folder: git init

  2. Add all the files to the staging area: git add -A

  3. Commit the changes with a message: git commit -m "Initial commit"

  4. Check the status of your repository: git status

  5. View the commit history: git log

🔍 Viewing Changes

  1. See the changes made to the files: git diff

  2. See the changes between a specific commit and the current state: git diff <commit>

  3. See the changes between two commits: git diff <commit1>..<commit2>

  4. See the changes in a specific file: git diff <file>

🌳 Branching and Merging

  1. Create a new branch: git branch <branch-name>

  2. Switch to a branch: git checkout <branch-name>

  3. Create a new branch and switch to it: git checkout -b <branch-name>

  4. Merge a branch into the current branch: git merge <branch-name>

  5. Delete a branch: git branch -d <branch-name>

🔄 Undoing Changes

  1. Discard changes in the working directory: git checkout -- <file>

  2. Unstage a file: git reset HEAD <file>

  3. Undo the last commit: git reset HEAD~1

  4. Undo the last commit and keep changes: git reset --soft HEAD~1

  5. Undo the last commit and discard changes: git reset --hard HEAD~1

💾 Working with Remote Repositories

  1. Clone a remote repository: git clone <repository-url>

  2. Add a remote repository: git remote add <name> <repository-url>

  3. Push changes to a remote repository: git push <remote> <branch>

  4. Pull changes from a remote repository: git pull <remote> <branch>

  5. Fetch changes from a remote repository: git fetch <remote>

📂 Stashing Changes

  1. Stash changes: git stash

  2. List stashes: git stash list

  3. Apply a stash: git stash apply <stash>

  4. Delete a stash: git stash drop <stash>

That's a comprehensive list of Git commands from basic to advanced.