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 in…


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


Print Share Comment Cite Upload Translate Updates
APA

Levis Chiri | Sciencx (2025-03-12T11:27:33+00:00) Using git and git hub. Retrieved from https://www.scien.cx/2025/03/12/using-git-and-git-hub/

MLA
" » Using git and git hub." Levis Chiri | Sciencx - Wednesday March 12, 2025, https://www.scien.cx/2025/03/12/using-git-and-git-hub/
HARVARD
Levis Chiri | Sciencx Wednesday March 12, 2025 » Using git and git hub., viewed ,<https://www.scien.cx/2025/03/12/using-git-and-git-hub/>
VANCOUVER
Levis Chiri | Sciencx - » Using git and git hub. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/03/12/using-git-and-git-hub/
CHICAGO
" » Using git and git hub." Levis Chiri | Sciencx - Accessed . https://www.scien.cx/2025/03/12/using-git-and-git-hub/
IEEE
" » Using git and git hub." Levis Chiri | Sciencx [Online]. Available: https://www.scien.cx/2025/03/12/using-git-and-git-hub/. [Accessed: ]
rf:citation
» Using git and git hub | Levis Chiri | Sciencx | https://www.scien.cx/2025/03/12/using-git-and-git-hub/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.