Getting Started
Configure Git
git config --global user.name "Your Name"
Set your username for Git commits.
git config --global user.email "you@example.com"
Set your email for Git commits.
git config --global core.editor "editor"
Set your default text editor (e.g., nano
, vim
).
git config --global core.autocrlf input
Handle line endings automatically.
Initialize Repository
git init
Initialize a new Git repository.
Clone Repository
git clone URL
Clone an existing repository from a remote URL.
Basic Commands
Check Status
git status
Show the working directory and staging area status.
Add Files
git add file
Stage a specific file for commit.
git add .
Stage all changes in the directory.
Commit Changes
git commit -m "Commit message"
Commit staged changes with a message.
git commit --amend
Amend the last commit with new changes.
Remove Files
git rm file
Remove a file from the working directory and staging area.
git rm --cached file
Remove a file from the staging area only.
Branching
List Branches
git branch
List all local branches.
Create Branch
git branch branch-name
Create a new branch.
Switch Branch
git checkout branch-name
Switch to an existing branch.
git checkout -b branch-name
Create and switch to a new branch.
Delete Branch
git branch -d branch-name
Delete a branch that has been merged.
git branch -D branch-name
Force delete a branch.
Merging
Merge Branch
git merge branch-name
Merge a branch into the current branch.
Abort Merge
git merge --abort
Abort a merge in progress.
Rebase
Rebase Branch
git rebase branch-name
Rebase the current branch onto another branch.
Abort Rebase
git rebase --abort
Abort a rebase in progress.
Continue Rebase
git rebase --continue
Continue a rebase after resolving conflicts.
Stashing
Stash Changes
git stash
Stash uncommitted changes.
List Stashes
git stash list
List all stashes.
Apply Stash
git stash apply
Apply the latest stash.
git stash apply stash@{n}
Apply a specific stash.
Drop Stash
git stash drop
Remove the latest stash.
git stash drop stash@{n}
Remove a specific stash.
Remote Repositories
Add Remote
git remote add name URL
Add a new remote repository.
List Remotes
git remote -v
List all configured remote repositories.
Fetch Changes
git fetch remote
Fetch changes from a remote repository.
Pull Changes
git pull remote branch
Fetch and merge changes from a remote branch.
Push Changes
git push remote branch
Push changes to a remote branch.
Delete Remote Branch
git push remote --delete branch-name
Delete a branch on the remote repository.
Viewing History
View Log
git log
View commit history.
git log --oneline
Simplified commit history.
Show Commit
git show commit-hash
Show details of a specific commit.
View Difference
git diff
Show unstaged changes.
git diff --staged
Show staged changes.
Tags
List Tags
git tag
List all tags.
Create Tag
git tag tag-name
Create a lightweight tag.
git tag -a tag-name -m "Message"
Create an annotated tag with a message.
Delete Tag
git tag -d tag-name
Delete a local tag.
git push origin --delete tag tag-name
Delete a remote tag.
Undo Changes
Unstage Changes
git reset file
Unstage a file while keeping changes.
Revert Changes
git checkout -- file
Discard changes in the working directory.
git revert commit-hash
Create a new commit to undo changes from a specific commit.
Reset to Commit
git reset --soft commit-hash
Reset to a commit, keeping changes staged.
git reset --hard commit-hash
Reset to a commit, discarding all changes.
Advanced Commands
Cherry-pick
git cherry-pick commit-hash
Apply changes from a specific commit.
Bisect
git bisect start
Start a binary search to find a buggy commit.
git bisect good commit-hash
git bisect bad commit-hash
Mark commits as good or bad.
Submodules
git submodule add URL
Add a submodule.
git submodule update --init
Initialize and update submodules.
References
For more in-depth information, visit the official Git documentation.