Git Hub Commands

LOCAL REPO :
๐ฆ Basic Git & File Setup
git init
๐ฆ Staging & Committing Changes
git add .
Stages all changes in the current directory.
git status
Shows the current status of changes (staged, unstaged, untracked).
git restore --staged names.txt
Unstages
names.txt
if it was added usinggit add
.
๐ฆ Commit History & Undo
git commit -m "message"
Commits all staged changes with a message.
git log
Shows the list of past commits.
git reset <commit_id>
Uncommits all commits after the specified commit.
Use the commit ID fromgit log
.
๐ฆ Pushing Code
git push origin main
Pushes to remote named
origin
, branchmain
.
๐ฆ Stash Commands
git stash
Saves all staged and unstaged changes, and cleans working directory.
โ Keeps local changes safe without committing.
git stash pop
Applies the latest stashed changes back to working directory.
git stash clear
Deletes all stashed entries (irreversible).
REMOTE REPO :
git remote add origin url # Naming your remote repo as origin with url
git remote -v # List all URLs saved
git branch # Show current branch
git branch feature # Create new branch called feature
git checkout feature # Shift to feature branch
WORKING WITH EXISTING PROJECTS :
i)
Fork ur desired repo to ur account.
git clone < forked_repo_url >
Make ur changes and while working:
Never push on main branch, create on newly created branch only.
Every time u are going to push, create a new branch for every pr.
ii)
1) Just like removing commits above i-th commit with git reset id-of-commit, similarly u can remove prs by u above i-th pr by simply git reset id-of-commit, and then do git push origin raj -f where raj is branch u previously pushed those extra prs
[ useful while ur pushing many prs on a same branch in ur remote repo. ]
2) How to sync upstream repo and ur [ remote repo, local repo ] ??
git checkout main
git pull upstream main [ Now both upstream and local repos synced]
git push origin main [ Now ur remote repo also synced]
Subscribe to my newsletter
Read articles from Rajesh Gurajala directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
