DEV Community

Levis Chiri
Levis Chiri

Posted on • Edited on

Using git and git hub

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
Enter fullscreen mode Exit fullscreen mode

🐧 Linux (Debian/Ubuntu)

Update and install Git using APT:

sudo apt update
sudo apt install git
Enter fullscreen mode Exit fullscreen mode

βœ… Verify Git Installation

Check if Git is installed by running:

git --version
Enter fullscreen mode Exit fullscreen mode

Setting Up Git

Configure your username and email:

git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
Enter fullscreen mode Exit fullscreen mode

Check your configuration:

git config --list
Enter fullscreen mode Exit fullscreen mode

Set your default branch name to main:

git config --global init.defaultBranch main
Enter fullscreen mode Exit fullscreen mode

1. Initializing a Repository

Before using Git, you need to initialize or clone a repository.

πŸ”Ή Create a new Git repository:

git init
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή Clone an existing repository:

git clone <repo_URL>
Enter fullscreen mode Exit fullscreen mode

πŸ’Ύ 2. Committing

πŸ” Check current status:

git status
Enter fullscreen mode Exit fullscreen mode

πŸ“₯ Clone code from GitHub:

git clone <repo_URL>
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή Stage all changes:

git add .
Enter fullscreen mode Exit fullscreen mode

πŸ“ Commit with a message:

git commit -m "your message here"
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή Push changes to remote:

git push
Enter fullscreen mode Exit fullscreen mode

πŸ”— Add remote repository:

git remote add origin <repo_URL>
Enter fullscreen mode Exit fullscreen mode

⬆️ Push to remote for the first time:

git push -u origin master
Enter fullscreen mode Exit fullscreen mode

πŸ” Verify remote URL:

git remote -v
Enter fullscreen mode Exit fullscreen mode

❌ Remove a remote:

git remote remove origin
Enter fullscreen mode Exit fullscreen mode

3. Branching and Merging

πŸ” List all branches:

git branch
Enter fullscreen mode Exit fullscreen mode

πŸ”ΉCreate a new branch:

git branch <branch-name>
Enter fullscreen mode Exit fullscreen mode

πŸ”€ Switch to a branch:

git checkout <branch-name>
Enter fullscreen mode Exit fullscreen mode

πŸ”ΉCreate and switch to new branch:

git checkout -b <branch-name>
Enter fullscreen mode Exit fullscreen mode

πŸ”— Merge a branch into current:

git merge <branch-name>
Enter fullscreen mode Exit fullscreen mode

πŸ—‘οΈ Delete a merged branch:

git branch -d <branch-name>
Enter fullscreen mode Exit fullscreen mode

πŸ”ΉπŸ—‘οΈ Force delete a branch:

git branch -D <branch-name>
Enter fullscreen mode Exit fullscreen mode

πŸ“¦ 4. Stashing Changes

🧳 Save current changes:

git stash
Enter fullscreen mode Exit fullscreen mode

πŸ“š View stash list:

git stash list
Enter fullscreen mode Exit fullscreen mode

πŸ”ΉApply and remove the latest stash:

git stash pop
Enter fullscreen mode Exit fullscreen mode

πŸ” Apply the latest stash (keep it):

git stash apply
Enter fullscreen mode Exit fullscreen mode

πŸ—‘οΈ Delete the most recent stash:

git stash drop
Enter fullscreen mode Exit fullscreen mode

🧹 Clear all stashes:

git stash clear
Enter fullscreen mode Exit fullscreen mode

πŸ”„ 5. Pushing and Pulling from GitHub

πŸ”— Add remote origin:

git remote add origin <repo_URL>
Enter fullscreen mode Exit fullscreen mode

⬆️ Push a branch:

git push origin <branch-name>
Enter fullscreen mode Exit fullscreen mode

πŸ” Push and set upstream:

git push -u origin <branch-name>
Enter fullscreen mode Exit fullscreen mode

⬇️ Pull changes:

git pull origin <branch-name>
Enter fullscreen mode Exit fullscreen mode

πŸ“₯ Fetch changes without merging:

git fetch
Enter fullscreen mode Exit fullscreen mode

πŸ“‘ Fetch all remotes:

git fetch --all
Enter fullscreen mode Exit fullscreen mode

🧼 6. Undoing Changes

🚫 Unstage a file:

git reset <file>
Enter fullscreen mode Exit fullscreen mode

πŸ”™ Undo last commit but keep changes:

git reset --soft HEAD~1
Enter fullscreen mode Exit fullscreen mode

πŸ”ΉUndo last commit and changes:

git reset --hard HEAD~1
Enter fullscreen mode Exit fullscreen mode

↩️ Revert a specific commit:

git revert <commit-hash>
Enter fullscreen mode Exit fullscreen mode

🧽 Discard local changes to a file:

git checkout -- <file>
Enter fullscreen mode Exit fullscreen mode

πŸ“œ 7. Viewing History and Logs

πŸ“– View commit log:

git log
Enter fullscreen mode Exit fullscreen mode

🧱 Condensed graph view:

git log --oneline --graph --all
Enter fullscreen mode Exit fullscreen mode

🧩 Show changes in last 2 commits:

git log -p -2
Enter fullscreen mode Exit fullscreen mode

πŸ“š View reference log:

git reflog
Enter fullscreen mode Exit fullscreen mode

πŸ”– 8. Working with Tags

List all tags:

git tag
Enter fullscreen mode Exit fullscreen mode

Create a lightweight tag:

git tag <tag-name>
Enter fullscreen mode Exit fullscreen mode

πŸ“ Create an annotated tag:

git tag -a <tag-name> -m "Message"
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή Push all tags to GitHub:

git push origin --tags
Enter fullscreen mode Exit fullscreen mode

πŸ”„ Checkout a tag:

git checkout <tag-name>
Enter fullscreen mode Exit fullscreen mode

🀝 9. Collaborating In GitHub

πŸ” View remote repositories:

git remote -v
Enter fullscreen mode Exit fullscreen mode

βž• Add a new remote:

git remote add origin <repo_URL>
Enter fullscreen mode Exit fullscreen mode

πŸ”„ Rebase with latest changes:

git pull --rebase origin main
Enter fullscreen mode Exit fullscreen mode

⬆️ Push current branch:

git push origin <branch-name>
Enter fullscreen mode Exit fullscreen mode

🍴 10. Fork and Pull Request

🍽️ Clone a forked repo:

git clone <forked_repo_URL>
Enter fullscreen mode Exit fullscreen mode

πŸ”ΉCreate a feature branch:

git checkout -b <feature-branch>
Enter fullscreen mode Exit fullscreen mode

⬆️ Push your branch:

git push origin <feature-branch>
Enter fullscreen mode Exit fullscreen mode

πŸ“¬ Open a Pull Request on GitHub.


🧩 11. Submodules

🧱 Add a submodule:

git submodule add <repo_URL> <path>
Enter fullscreen mode Exit fullscreen mode

πŸ”„ Initialize and update submodules:

git submodule update --init --recursive
Enter fullscreen mode Exit fullscreen mode

πŸ› οΈ 12. Fixing Mistakes

πŸ’ Apply a specific commit from another branch:

git cherry-pick <commit_hash>
Enter fullscreen mode Exit fullscreen mode

✍️ Interactively edit last 3 commits:

git rebase -i HEAD~3
Enter fullscreen mode Exit fullscreen mode

πŸ’₯ Reset local branch to match remote:

git reset --hard origin/main
Enter fullscreen mode Exit fullscreen mode

πŸ—‘οΈ 13. Deleting a Repository (Caution!)

⚠️ Deletes the Git history in current folder:

rm -rf .git
Enter fullscreen mode Exit fullscreen mode

Setting Up SSH for GitHub

Generate an SSH key:

ssh-keygen -t ed25519 -C "your.email@example.com"
Enter fullscreen mode Exit fullscreen mode

Start the SSH agent:

eval "$(ssh-agent -s)"
Enter fullscreen mode Exit fullscreen mode

Add the key to SSH agent:

ssh-add ~/.ssh/id_ed25519
Enter fullscreen mode Exit fullscreen mode

Copy the SSH key:

cat ~/.ssh/id_ed25519.pub
Enter fullscreen mode Exit fullscreen mode

Go to GitHub -> Settings -> SSH and GPG keys -> Add a new key -> Paste the copied key.

Test the connection:

ssh -T git@github.com
Enter fullscreen mode Exit fullscreen mode

Set Git to use SSH:

git remote set-url origin git@github.com:username/repository.git
Enter fullscreen mode Exit fullscreen mode

Top comments (0)