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)