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 "your.email@example.com"
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 <repo_URL>
πΎ 2. Committing
π Check current status:
git status
π₯ Clone code from GitHub:
git clone <repo_URL>
πΉ 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 <repo_URL>
β¬οΈ 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 <branch-name>
π Switch to a branch:
git checkout <branch-name>
πΉCreate and switch to new branch:
git checkout -b <branch-name>
π Merge a branch into current:
git merge <branch-name>
ποΈ Delete a merged branch:
git branch -d <branch-name>
πΉποΈ Force delete a branch:
git branch -D <branch-name>
π¦ 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 <repo_URL>
β¬οΈ Push a branch:
git push origin <branch-name>
π Push and set upstream:
git push -u origin <branch-name>
β¬οΈ Pull changes:
git pull origin <branch-name>
π₯ Fetch changes without merging:
git fetch
π‘ Fetch all remotes:
git fetch --all
π§Ό 6. Undoing Changes
π« Unstage a file:
git reset <file>
π 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 <commit-hash>
π§½ Discard local changes to a file:
git checkout -- <file>
π 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 <tag-name>
π Create an annotated tag:
git tag -a <tag-name> -m "Message"
πΉ Push all tags to GitHub:
git push origin --tags
π Checkout a tag:
git checkout <tag-name>
π€ 9. Collaborating In GitHub
π View remote repositories:
git remote -v
β Add a new remote:
git remote add origin <repo_URL>
π Rebase with latest changes:
git pull --rebase origin main
β¬οΈ Push current branch:
git push origin <branch-name>
π΄ 10. Fork and Pull Request
π½οΈ Clone a forked repo:
git clone <forked_repo_URL>
πΉCreate a feature branch:
git checkout -b <feature-branch>
β¬οΈ Push your branch:
git push origin <feature-branch>
π¬ Open a Pull Request on GitHub.
π§© 11. Submodules
π§± Add a submodule:
git submodule add <repo_URL> <path>
π Initialize and update submodules:
git submodule update --init --recursive
π οΈ 12. Fixing Mistakes
π Apply a specific commit from another branch:
git cherry-pick <commit_hash>
βοΈ 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 "your.email@example.com"
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 git@github.com
Set Git to use SSH:
git remote set-url origin git@github.com:username/repository.git
Top comments (0)