सबसे उपयोगी Git कमांड्स का त्वरित संदर्भ — सेटअप, स्टेजिंग, ब्रांचिंग, बदलाव पूर्ववत करना और रिमोट के साथ काम करना।
Git लगभग हर आधुनिक सॉफ़्टवेयर प्रोजेक्ट के पीछे का वर्शन-कंट्रोल सिस्टम है। यह चीट शीट उन Git कमांड्स को एकत्र करती है जिनका आप रोज़ उपयोग करते हैं, कार्य के अनुसार समूहित ताकि आप सही कमांड जल्दी ढूंढ सकें।
अपने पहले commit से लेकर rebase करने और गलतियां पूर्ववत करने तक, इस पेज को त्वरित संदर्भ के रूप में बुकमार्क रखें। प्रत्येक कमांड को उसके कार्य के संक्षिप्त विवरण के साथ दिखाया गया है।
Setup & Configuration
| Command | What it does |
|---|
git config --global user.name "Name" | Set the name attached to your commits. |
git config --global user.email "email" | Set the email attached to your commits. |
git config --list | List all configured settings. |
git config --global init.defaultBranch main | Use 'main' as the default branch name. |
git config --global alias.co checkout | Create a shortcut alias (e.g. git co). |
Creating & Cloning
| Command | What it does |
|---|
git init | Create a new Git repository in the current folder. |
git clone <url> | Clone a remote repository locally. |
git clone <url> <dir> | Clone into a specific directory. |
git clone --depth 1 <url> | Shallow clone with only the latest commit. |
Staging & Committing
| Command | What it does |
|---|
git status | Show changed, staged, and untracked files. |
git add <file> | Stage a specific file for commit. |
git add . | Stage all changes in the current directory. |
git reset <file> | Unstage a file, keeping its changes. |
git commit -m "message" | Commit staged changes with a message. |
git commit -am "message" | Stage tracked files and commit in one step. |
git commit --amend | Modify the most recent commit. |
Branching & Merging
| Command | What it does |
|---|
git branch | List local branches. |
git branch <name> | Create a new branch. |
git switch <name> | Switch to an existing branch. |
git switch -c <name> | Create and switch to a new branch. |
git merge <branch> | Merge a branch into the current one. |
git rebase <branch> | Reapply commits on top of another branch. |
git branch -d <name> | Delete a merged branch. |
git branch -D <name> | Force-delete a branch. |
Remote Repositories
| Command | What it does |
|---|
git remote -v | List configured remotes. |
git remote add origin <url> | Add a remote named 'origin'. |
git fetch | Download objects and refs without merging. |
git pull | Fetch and merge changes from the remote. |
git push | Upload local commits to the remote. |
git push -u origin <branch> | Push and set the upstream branch. |
Inspecting & Comparing
| Command | What it does |
|---|
git log | Show the commit history. |
git log --oneline | Compact one-line-per-commit history. |
git diff | Show unstaged changes. |
git diff --staged | Show staged changes. |
git show <commit> | Show details of a specific commit. |
git blame <file> | Show who last changed each line. |
Undoing Changes
| Command | What it does |
|---|
git restore <file> | Discard unstaged changes to a file. |
git reset --soft HEAD~1 | Undo last commit, keep changes staged. |
git reset --hard HEAD~1 | Undo last commit and discard changes. |
git revert <commit> | Create a new commit that undoes a commit. |
git clean -fd | Remove untracked files and directories. |
Stashing
| Command | What it does |
|---|
git stash | Save uncommitted changes for later. |
git stash list | List all stashes. |
git stash pop | Apply and remove the latest stash. |
git stash apply | Apply the latest stash, keeping it. |
git stash drop | Delete the latest stash. |
Git कमांड्स चीट शीट से जुड़े सामान्य प्रश्न
git merge और git rebase में क्या अंतर है?
Merge दो ब्रांच को जोड़ता है और एक merge commit बनाता है, इतिहास को यथावत रखता है। Rebase आपके commits को दूसरी ब्रांच के ऊपर ले जाता है ताकि इतिहास रैखिक रहे। उन commits को rebase करने से बचें जिन्हें आप पहले ही push और साझा कर चुके हैं।
मैं अपना आखिरी commit कैसे पूर्ववत करूं?
git reset --soft HEAD~1 का उपयोग commit पूर्ववत करने पर बदलाव staged रखने के लिए करें, या git reset --hard HEAD~1 बदलाव पूरी तरह हटाने के लिए। पहले से push किए commit को पूर्ववत करने के लिए git revert का उपयोग करें।
git fetch और git pull में क्या अंतर है?
git fetch रिमोट से नए commits डाउनलोड करता है बिना आपकी वर्किंग ब्रांच बदले। git pull fetch करता है और फिर उन बदलावों को आपकी मौजूदा ब्रांच में merge कर देता है।
मैं नई ब्रांच कैसे बनाऊं और उस पर स्विच करूं?
एक ही चरण में ब्रांच बनाने और उस पर स्विच करने के लिए git switch -c branch-name चलाएं। पुराना समकक्ष git checkout -b branch-name है।
मैं किसी फ़ाइल के स्थानीय बदलाव कैसे हटाऊं?
किसी फ़ाइल के unstaged बदलाव हटाने के लिए git restore file का उपयोग करें, या इसे unstage करते हुए बदलाव रखने के लिए git restore --staged file।