Using git and git hub



This content originally appeared on DEV Community and was authored by Levis Chiri

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

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


This content originally appeared on DEV Community and was authored by Levis Chiri