Installing and Using Git
🖥️ Windows
Go to git-scm.com and download Git for your operating system.
Run the installer and follow the setup wizard (default settings are fine).
🍎 macOS
Install Git using Homebrew:
brew install git
🐧 Linux (Debian/Ubuntu)
Update and install Git using APT:
sudo apt update
sudo apt install git
✅ Verify Git Installation
Check if Git is installed by running:
git --version
Setting Up Git
Configure your username and email:
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
Check your configuration:
git config --list
Set your default branch name to main
:
git config --global init.defaultBranch main
1. Initializing a Repository
Before using Git, you need to initialize or clone a repository.
🔹 Create a new Git repository:
git init
🔹 Clone an existing repository:
git clone
💾 2. Committing
🔍 Check current status:
git status
📥 Clone code from GitHub:
git clone
🔹 Stage all changes:
git add .
📝 Commit with a message:
git commit -m "your message here"
🔹 Push changes to remote:
git push
🔗 Add remote repository:
git remote add origin
⬆️ Push to remote for the first time:
git push -u origin master
🔍 Verify remote URL:
git remote -v
❌ Remove a remote:
git remote remove origin
3. Branching and Merging
🔍 List all branches:
git branch
🔹Create a new branch:
git branch
🔀 Switch to a branch:
git checkout
🔹Create and switch to new branch:
git checkout -b
🔗 Merge a branch into current:
git merge
🗑️ Delete a merged branch:
git branch -d
🔹🗑️ Force delete a branch:
git branch -D
📦 4. Stashing Changes
🧳 Save current changes:
git stash
📚 View stash list:
git stash list
🔹Apply and remove the latest stash:
git stash pop
🔁 Apply the latest stash (keep it):
git stash apply
🗑️ Delete the most recent stash:
git stash drop
🧹 Clear all stashes:
git stash clear
🔄 5. Pushing and Pulling from GitHub
🔗 Add remote origin:
git remote add origin
⬆️ Push a branch:
git push origin
🔁 Push and set upstream:
git push -u origin
⬇️ Pull changes:
git pull origin
📥 Fetch changes without merging:
git fetch
📡 Fetch all remotes:
git fetch --all
🧼 6. Undoing Changes
🚫 Unstage a file:
git reset
🔙 Undo last commit but keep changes:
git reset --soft HEAD~1
🔹Undo last commit and changes:
git reset --hard HEAD~1
↩️ Revert a specific commit:
git revert
🧽 Discard local changes to a file:
git checkout --
📜 7. Viewing History and Logs
📖 View commit log:
git log
🧱 Condensed graph view:
git log --oneline --graph --all
🧩 Show changes in last 2 commits:
git log -p -2
📚 View reference log:
git reflog
🔖 8. Working with Tags
List all tags:
git tag
Create a lightweight tag:
git tag
📝 Create an annotated tag:
git tag -a -m "Message"
🔹 Push all tags to GitHub:
git push origin --tags
🔄 Checkout a tag:
git checkout
🤝 9. Collaborating In GitHub
🔍 View remote repositories:
git remote -v
➕ Add a new remote:
git remote add origin
🔄 Rebase with latest changes:
git pull --rebase origin main
⬆️ Push current branch:
git push origin
🍴 10. Fork and Pull Request
🍽️ Clone a forked repo:
git clone
🔹Create a feature branch:
git checkout -b
⬆️ Push your branch:
git push origin
📬 Open a Pull Request on GitHub.
🧩 11. Submodules
🧱 Add a submodule:
git submodule add
🔄 Initialize and update submodules:
git submodule update --init --recursive
🛠️ 12. Fixing Mistakes
🍒 Apply a specific commit from another branch:
git cherry-pick
✍️ Interactively edit last 3 commits:
git rebase -i HEAD~3
💥 Reset local branch to match remote:
git reset --hard origin/main
🗑️ 13. Deleting a Repository (Caution!)
⚠️ Deletes the Git history in current folder:
rm -rf .git
Setting Up SSH for GitHub
Generate an SSH key:
ssh-keygen -t ed25519 -C "[email protected]"
Start the SSH agent:
eval "$(ssh-agent -s)"
Add the key to SSH agent:
ssh-add ~/.ssh/id_ed25519
Copy the SSH key:
cat ~/.ssh/id_ed25519.pub
Go to GitHub -> Settings -> SSH and GPG keys -> Add a new key -> Paste the copied key.
Test the connection:
ssh -T [email protected]
Set Git to use SSH:
git remote set-url origin [email protected]:username/repository.git