Git commands

Initialize a repository

git init

Clone a repository

git clone <repository_url>

Check repository status

git status

Add files to staging area

git add <file> # Add specific file git add . # Add all files

Commit changes

git commit -m "Commit message"

View commit history

git log

Create a new branch

git branch <branch_name>

Switch branches

git checkout <branch_name> git switch <branch_name> # (Preferred in newer Git versions)

Create and switch to a new branch

git checkout -b <branch_name> git switch -c <branch_name> # (Newer Git versions)

Merge a branch into the current branch

git merge <branch_name>

Delete a branch

git branch -d <branch_name>

Check remote repositories

git remote -v

Push changes to a remote repository

git push origin <branch_name>

Pull latest changes from remote

git pull origin <branch_name>

Fetch changes from remote without merging

git fetch origin

Undo the last commit (discard changes)

git reset --hard HEAD~1

Revert a specific commit (without losing history)

git revert <commit_hash>

Stash uncommitted changes

git stash

Apply the last stash

git stash pop

Cherry-pick a commit from another branch

git cherry-pick <commit_hash>

Squash multiple commits into one

git rebase -i HEAD~n