GIT (Global Information Tracker) is a distributed version control system used to track
changes in code, collaborate with teams, and manage project history efficiently. It allows
multiple developers to work on the same codebase simultaneously without conflicts.
Key Concepts of Git
1. Repository (Repo) – A directory containing your project files and the entire history of
changes.
2. Commit – A snapshot of your code at a specific point in time.
3. Branch – A parallel version of the project to work on new features without affecting
the main codebase.
4. Merge – Combining changes from different branches.
5. Clone – Creating a copy of an existing repository.
6. Pull – Fetching and merging changes from a remote repository.
7. Push – Sending your committed changes to a remote repository.
Basic Git Commands
Configuration
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
Initialize a Repository
git init
Clone a Repository
git clone <repo-url>
Add & Commit Changes
git add . # Stage all changes
git commit -m "Message" # Commit changes with a message
Check Status & Log
git status # View changes
git log # View commit history
Branching & Merging
git branch <branch-name> # Create a new branch
git checkout <branch-name> # Switch to branch
git merge <branch-name> # Merge branch into the current branch
Push & Pull
git push origin <branch-name> # Push changes to remote
git pull origin <branch-name> # Pull latest changes
Undo & Reset
git reset --soft HEAD~1 # Undo last commit but keep changes
git reset --hard HEAD~1 # Undo last commit and discard changes
git revert <commit-id> # Create a new commit that undoes a previous one
Basic Commands
1. Check Git Version
git --version
If Git is installed, it will display something like:
git version 2.34.1
2. Initialize a New Git Repository
git init
If you're inside a project folder and want to track changes with Git:
cd my_project
git init
// creates a hidden .git folder, initializing a new Git repo.
3. Check the Status of Your Repository
git status
On branch main
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
index.html
style.css
tells which files are new, modified, or staged for commit.
4. Add Files to Staging Area
git add <filename>
To add all files:
git add .
git add index.html
5. Commit Changes
git commit -m "Your commit message"
git commit -m "Added homepage design"
6. View Commit History
git log
commit 5f2b37a (HEAD -> main)
Author: John Doe <john@example.com>
Date: Mon Jan 29 12:00 2025
7. Create a New Branch
git branch <branch-name>
git branch feature-login
8. Switch to a Branch
git checkout <branch-name> or git switch <branch-name>
git switch feature-login
9. Merge a Branch into Main
git merge <branch-name>
git checkout main
git merge feature-login
10. Delete a Branch
git branch -d <branch-name>
git branch -d feature-login
11. Clone a Repository
git clone <repo-url>
git clone https://github.com/user/myrepo.git
12. Push Changes to Remote Repository
git push origin <branch-name>
git push origin main
//uploads commits to the remote repository on GitHub/GitLab.
13. Pull Latest Changes from Remote
git pull origin <branch-name>
git pull origin main
//updates local repo with the latest changes from GitHub.
14. Undo Last Commit (Soft Reset)
git reset --soft HEAD~1
// removes the last commit but keeps your changes.
15. Undo Last Commit (Hard Reset)
git reset --hard HEAD~1
// removes the last commit and deletes all changes.
16. Remove a File from Staging Area
git reset <filename>
git reset index.html
unstages index.html without deleting changes.
Remove a File from Git (but keep locally)
git rm --cached <filename>
git rm --cached secret.txt
//This removes secret.txt from Git but keeps it in your folder.
18. Show Differences in Files
git diff
// This shows changes you made before committing.
19. Stash Changes (Save Temporarily)
git stash
// This saves changes without committing and resets your working directory.
To apply the stashed changes later:
git stash pop
20. Set Upstream Branch
//pushing to a remote repo for the first time:
git push --set-upstream origin <branch-name>
git push --set-upstream origin main // links local branch to the remote branch.