Think of Git not just as a tool for saving files, but as a time machine for your code. To use this time machine effectively, you need to understand how Git tracks your work through different stages.
Here is a breakdown of the Git lifecycle and the two most important commands you'll use daily.
1. The Git Lifecycle (The Big Picture)
Git manages your project through three main sections (often called the "Three Trees" of Git). Understanding these is key to knowing why we use add and commit.
Working Directory: This is your actual project folder on your computer. When you edit a file, the changes live here first.
Staging Area (Index): This is a middle ground. Think of it as a "loading dock" where you gather all the changes you want to include in your next save point.
Local Repository (HEAD): Once you "commit," the changes are permanently recorded as a version in your
.gitfolder.
2. git add: Preparing Your Changes
The git add command moves changes from your Working Directory to the Staging Area.
It’s like picking out specific items to put in a box before you tape it shut. You might have changed ten files, but you only want to save three of them right now—git add lets you choose exactly which ones.
Examples:
Add a specific file:
git add index.html
Add all files in the current folder:
git add .
Add all files ending in .css:
git add *.css
3. git commit: Saving the Snapshot
The git commit command (which you might have seen as "got commit" in some typos!) takes everything currently in your Staging Area and wraps it up into a permanent "snapshot" in the Local Repository.
Each commit gets a unique ID and a message describing what changed. This is your official save point.
Example:
git commit -m "Fix the navigation bar bug"
Pro Tip: Always use the
-mflag to write a descriptive message. If you forget it, Git might open a complex text editor like Vim to force you to write one!
4. Summary Table
| Command | Action | Where does the data go? |
| git add | Staging | Working Directory $\rightarrow$ Staging Area |
| git commit | Recording | Staging Area $\rightarrow$ Local Repository |
Putting it all together: A Real-World Example
Imagine you are building a website and you just finished the "About Me" page and fixed a typo in the "Contact" page.
Edit your files: You modify
about.htmlandcontact.html.Check status: You run
git status. Git shows both files are "Modified" but not yet staged.Stage your work: You run
git add about.html contact.html. Now they are in the "loading dock."Save your work: You run
git commit -m "Complete About page and fix Contact typo".
Now, your work is safely tucked away in the repository. Even if you delete the files by accident, you can recover them from that commit!
గిట్ (Git) అనేది కేవలం ఫైల్స్ సేవ్ చేసే సాధనం మాత్రమే కాదు, ఇది మీ కోడ్ కోసం ఒక కాలయంత్రం (Time machine) లాంటిది. దీన్ని సరిగ్గా ఉపయోగించాలంటే, గిట్ మీ పనిని ఏయే దశల్లో ట్రాక్ చేస్తుందో మీరు అర్థం చేసుకోవాలి.
గిట్ లైఫ్ సైకిల్ మరియు దానిలోని ముఖ్యమైన కమాండ్స్ గురించి వివరణ ఇక్కడ ఉంది:
1. గిట్ లైఫ్ సైకిల్ (The Big Picture)
గిట్ మీ ప్రాజెక్ట్ను మూడు ప్రధాన విభాగాలుగా విభజిస్తుంది. వీటినే గిట్ యొక్క "త్రీ ట్రీస్" (Three Trees) అని కూడా అంటారు.
వర్కింగ్ డైరెక్టరీ (Working Directory): ఇది మీ కంప్యూటర్లోని అసలైన ప్రాజెక్ట్ ఫోల్డర్. మీరు ఏదైనా ఫైల్ను ఎడిట్ చేసినప్పుడు, ఆ మార్పులు మొదట ఇక్కడే ఉంటాయి.
స్టేజింగ్ ఏరియా (Staging Area / Index): ఇది ఒక మధ్యంతర దశ. మీరు తదుపరి 'సేవ్ పాయింట్'లో చేర్చాలనుకుంటున్న మార్పులన్నింటినీ ఒక దగ్గర చేర్చే "లోడింగ్ డాక్" (Loading dock) లాంటిది.
లోకల్ రిపోజిటరీ (Local Repository): మీరు "కమిట్" చేసిన తర్వాత, మార్పులు మీ
.gitఫోల్డర్లో ఒక వెర్షన్గా శాశ్వతంగా రికార్డ్ చేయబడతాయి.
2. git add: మార్పులను సిద్ధం చేయడం
git add కమాండ్ మీ వర్కింగ్ డైరెక్టరీ నుండి మార్పులను స్టేజింగ్ ఏరియాకు మారుస్తుంది.
ఉదాహరణకు, మీరు ఒక పెట్టెను ప్యాక్ చేసి టేప్ వేయడానికి ముందు, అందులో ఏయే వస్తువులు ఉండాలో ఎంచుకోవడం లాంటిది ఇది. మీరు 10 ఫైల్స్ మార్చి ఉండవచ్చు, కానీ ప్రస్తుతం కేవలం 3 ఫైల్స్ మాత్రమే సేవ్ చేయాలనుకుంటే.. ఆ మూడింటిని మాత్రమే git add ద్వారా ఎంచుకోవచ్చు.
ఉదాహరణలు:
ఒక నిర్దిష్ట ఫైల్ను యాడ్ చేయడానికి:
git add index.html
ఫోల్డర్లోని అన్ని మార్పులను యాడ్ చేయడానికి:
git add .
కేవలం .css ఫైల్స్ మాత్రమే యాడ్ చేయడానికి:
git add *.css
3. git commit: స్నాప్షాట్ను భద్రపరచడం
git commit కమాండ్ స్టేజింగ్ ఏరియాలో ఉన్న వాటన్నింటినీ ఒక శాశ్వత 'స్నాప్షాట్' (Snapshot) గా మార్చి లోకల్ రిపోజిటరీలో భద్రపరుస్తుంది.
ప్రతి కమిట్కు ఒక ప్రత్యేక ఐడి (ID) మరియు మీరు చేసిన మార్పుల వివరణ (Message) ఉంటుంది. ఇదే మీ అధికారిక సేవ్ పాయింట్.
ఉదాహరణ:
git commit -m "నేవిగేషన్ బార్ బగ్ని సరిచేశాను"
చిట్కా: ఎల్లప్పుడూ
-mఫ్లాగ్ ఉపయోగించి వివరణాత్మక మెసేజ్ రాయండి. లేకపోతే, గిట్ 'Vim' వంటి కష్టమైన ఎడిటర్ను ఓపెన్ చేసి మిమ్మల్ని మెసేజ్ రాయమని బలవంతం చేస్తుంది!
4. సారాంశం (Summary Table)
| కమాండ్ | పని | డేటా ఎక్కడికి వెళ్తుంది? |
| git add | స్టేజింగ్ (Staging) | వర్కింగ్ డైరెక్టరీ $\rightarrow$ స్టేజింగ్ ఏరియా |
| git commit | రికార్డింగ్ (Recording) | స్టేజింగ్ ఏరియా $\rightarrow$ లోకల్ రిపోజిటరీ |
ఒక నిజజీవిత ఉదాహరణ:
మీరు ఒక వెబ్సైట్ నిర్మిస్తున్నారని అనుకుందాం. మీరు "About Me" పేజీని పూర్తి చేశారు మరియు "Contact" పేజీలో ఒక తప్పును సరిచేశారు.
ఫైల్స్ ఎడిట్ చేయండి: మీరు
about.htmlమరియుcontact.htmlలో మార్పులు చేశారు.స్టేటస్ చెక్ చేయండి:
git statusరన్ చేస్తే, ఆ రెండు ఫైల్స్ "Modified" అని చూపిస్తాయి.స్టేజ్ చేయండి:
git add about.html contact.htmlఅని టైప్ చేయండి. ఇప్పుడు అవి "లోడింగ్ డాక్" (స్టేజింగ్ ఏరియా) లో ఉన్నాయి.సేవ్ చేయండి:
git commit -m "About page పూర్తి చేశాను మరియు Contact typo సరిచేశాను"అని రన్ చేయండి.
ఇప్పుడు మీ పని రిపోజిటరీలో సురక్షితంగా భద్రపరచబడింది. ఒకవేళ పొరపాటున మీరు ఆ ఫైల్స్ డిలీట్ చేసినా, ఈ కమిట్ నుండి వాటిని తిరిగి పొందవచ్చు!
