Git Basic Commands Cheat Sheet

Initialize a Git repository

git init  # Create a new Git repository in the current folder

Add files to staging area

git add .         # Add all modified and new files
git add filename  # Add a specific file

Commit changes

git commit -m "Your commit message"  # Save staged changes with a message

Check the status of your working directory

git status  # See which files are modified, staged, or untracked

View commit history

git log  # Show a list of past commits

Push to remote repository

git push origin main    # Push local main branch to remote
git push origin master  # Push local master branch to remote

Clone a remote repository

git clone https://github.com/your-username/your-repo.git  # Copy remote repo to local

Pull the latest changes from remote

git pull  # Fetch and merge changes from the remote branch

Set the remote repository (only needed once)

git remote add origin https://github.com/your-username/your-repo.git  # Link to remote repo

Create and switch to a new branch

git checkout -b new-branch-name  # Create and switch to a new branch

Switch to an existing branch

git checkout branch-name  # Switch to another branch

Merge a branch into current branch

git merge branch-name  # Merge changes from specified branch

Delete a branch

git branch -d branch-name  # Delete a local branch