Git - vimlesh-verma16/Notes GitHub Wiki

Git Notes

Git command for setting up directory

git init
git add . 
git commit -m "first commit"
git remote add origin https://github.com [ur link will come here from github when you create branch]
git push -u origin master

Edit last commit and add files that were not added

git add .
git commit --amend --no-edit
git push --force

squash changes into 1 commit

git rebase -i HEAD~10
Keep the top most commit as pick and change rest of them to s (squash) again editor will open give the name to final commit
git push -f

To Save password credential

git config --global credential.helper store


To make the branch exactly like the remote..

git fetch --all
git reset --hard origin/master (Works sometime )
git reset --hard origin/<BRANCH_NAME>
git clean -n (To see what will be deleted)
git clean -fdx # STAY ALERT! Untracked (.gitignore) directories and files will be gone!

Revert to last commit

git log --online
git reset --hard HEAD~1  (for reverting the last 1 commit)

Deleting multiple branches

git branch -D vimlesh (Delete a branch)
delete all the branches except the one you are using
for /f "delims=" %i in ('git branch ^| findstr /v "vimlesh/scan') do git branch -D %i

Git commands for manupulating the directory (folder)

  1. mkdir phantoms (creating a folder)
  2. rmdir phantoms (deleting a folder)
  3. git mv <D:repo/fullpath/file.py> <D:repo/fullpathtofolder> (Renaming and moving a file)

Revert a specific commit

git revert --no-commit 8d933eebc3a6553e0f6a6eb15921dd3cb7013920


Remove a file from commit history or PR

Make sure that you have checked out the feature branch from which the PR has been created. Search the file commit history:

git log "C:\GitRepos\MyProject\SomeFile.cs"
git checkout <commit id> "C:\GitRepos\MyProject\SomeFile.cs"
git commit -m "Remove the SomeFile.cs from PR"
git push -f

Advanced Commands:

git reset --soft HEAD~1 : WORK — This removes the last commit but keeps your changes intact so you can fix them in your local. 
git blame filename.ext : WORK — This shows who changed each line in a file — perfect for tracking bugs (or reminding your teammate that they broke something and now it’s their turn to take responsibility).
git config --global alias.cm "commit -m" :  WORK — Now, instead ofgit commit -m, just type git cm. Time saver!
git config --global alias.st "status" 
git checkout -- filename.ext :  WORK — This reverts the file to its last committed state. No stress, no mess. 
git checkout - : WORK — This takes you back to the last branch you were on — just like hitting ‘Undo’ on your branch navigation. So quick and easy.
git branch -d branch-name   # Delete locally
git push origin --delete branch-name  # Delete remotely :  WORK — Keeps your repo clean and organized!
git log --oneline --graph --decorate --all :  WORK — This shows a beautiful, easy-to-read commit history.  
git commit --amend -m "New message" : WORK — This updates your last commit message without creating a new commit.
git clone --depth=1 repo-url.git : WORK — Perfect when you don’t need old commits and want a fast setup. 

⚠️ **GitHub.com Fallback** ⚠️