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)