2.9 KiB
2.9 KiB
Git Commands Guide
1. Installation and Setup
Install Git
Download from git-scm.com.
Verify Installation
git --version
Configure User Info
Set your name and email for commits:
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
2. SSH Key Configuration
Use a custom SSH key:
git config --add --local core.sshCommand 'ssh -i <PATH_TO_SSH_KEY>'
Clone with custom key:
git -c core.sshCommand="ssh -i <key-path>" clone host:repo
3. Initialize Repository
Create a new Git repo:
git init -b main
-b main: Sets default branch name tomain.
4. Basic Workflow
Stage and Commit
Stage all changes:
git add -A
Commit changes:
git commit -m "Initial Commit"
Connect to Remote
Link local repo to remote:
git remote add origin <Repo-Link>
Push to Remote
git push origin main
5. Repository Status and History
Check Status
git status
Shows staged, unstaged, and untracked files.
View History
git log
git log -p: Shows diffs per commit.git log -3: Shows last 3 commits.git log --graph: Shows history as a graph.git log --oneline: Shows compact log.
View Changes
git diff
Shows unstaged changes.
git diff --staged
Shows staged changes.
6. File Operations
Stage Specific Files
git add <filename>
Unstage Files
git reset <filename>
Discard Changes
git checkout -- <filename>
Reverts file to last committed state.
Rename File
git mv <old-name> <new-name>
Remove File
git rm <filename>
7. Branch Management
Create and Switch to Branch
git checkout -b <branch-name>
List Branches
git branch
List with details:
git branch -v
Delete Branch
git branch -d <branch-name>
Switch Branch
git checkout <branch-name>
8. Merging
Merge Branch
git merge <branch-name>
Merge Types:
- Fast-forward: No conflicts; branch is ahead of target.
- Three-way: Conflicts possible; creates new merge commit.
9. Remote Operations
List Remotes
git remote
Show Remote Info
git remote -v
git remote show origin
Fetch Changes
git fetch
Get updates without merging.
git fetch --all
Fetch from all remotes.
10. Commit Management
Edit Last Commit Message
git commit --amend
Show Commit Changes
git show <commit-id>
Revert Commit
git revert HEAD
Revert last commit.
git revert <commit-id>
Revert specific commit.
11. Miscellaneous
Change Default Editor
git config --global core.editor "vim"
Clone Repository
git clone <Repo-Link>