Skip to content

Latest commit

 

History

History
281 lines (186 loc) · 3.33 KB

File metadata and controls

281 lines (186 loc) · 3.33 KB

Git Commands CheatSheet

A comprehensive guide covering essential Git commands for version control, collaboration, and repository management.

Table of Contents

Git Configuration

Set your Git username:

git config --global user.name "Your Name"

Set your Git email:

git config --global user.email "your.email@example.com"

Check current configuration:

git config --list

Repository Management

Initialize a new repository:

git init

Clone an existing repository:

git clone <repo-url>

Branching & Merging

List branches:

git branch

Create a new branch:

git branch <branch-name>

Switch to a branch:

git checkout <branch-name>

Create and switch to a new branch:

git checkout -b <branch-name>

Merge a branch into the current branch:

git merge <branch-name>

Delete a branch:

git branch -d <branch-name>

Committing Changes

Check the status of changes:

git status

Add files to staging:

git add <file>
git add .  # Add all changes

Commit changes:

git commit -m "Commit message"

Amend the last commit:

git commit --amend

Undoing Changes

Unstage a file:

git reset <file>

Revert a commit:

git revert <commit-hash>

Reset branch to a previous commit:

git reset --hard <commit-hash>

Working with Remotes

List remote repositories:

git remote -v

Add a new remote repository:

git remote add origin <repo-url>

Fetch changes from remote:

git fetch origin

Push changes to remote:

git push origin <branch-name>

Pull latest changes from remote:

git pull origin <branch-name>

Tagging

List all tags:

git tag

Create a new tag:

git tag -a <tag-name> -m "Tag message"

Push tags to remote:

git push origin --tags

Stashing

Save uncommitted changes:

git stash

Apply stashed changes:

git stash pop

List stashes:

git stash list

Rebasing

Rebase current branch onto another branch:

git rebase <branch-name>

Continue rebase after conflict resolution:

git rebase --continue

Abort a rebase:

git rebase --abort

Cherry-Picking

Apply a specific commit from another branch:

git cherry-pick <commit-hash>

Logging & History

View commit history:

git log --oneline --graph --decorate --all

View changes in a specific commit:

git show <commit-hash>

Submodules

Add a submodule:

git submodule add <repo-url>

Initialize and update submodules:

git submodule update --init --recursive

Git Hooks

List available hooks:

ls .git/hooks/

Enable a hook (e.g., pre-commit):

chmod +x .git/hooks/pre-commit