Git Commands
Commands Function
git init initialize the folder into git
git log shows the history
git restore —staged sample.txt it will unstage the changes that is commited
git reset <hashcode> rollback to that commit stage…
all the changes we made are stored temp and roll
git stash
back to initial code.
git stash pop temp changes are poped and changes the code
git stash clear delete the temp stash
git remote add origin <url> connect our remote branch with local
git remote -v show the origin url and name
git status give the current status of the code changes.
git branch <branch-name> creates a new branch
git checkout <branch-name> switch branch
git merge <branch-name> merges the branch to main
git push origin master pushes the code to the origin and master branch
let’s say we are cloning a open source project and we
git remote add upstream <url> have already the origin as our own repo link. We are
adding the upstream remote url…
force push when we are commiting on the same pull
git push -f
request.
Consider a scenario, we are commiting 3 files in
git pull upstream main ( for pulling
another branch and we need to update it in the main
in local)
branch, we use pull the upstream.
if we have 3 commits and we need to combine those
commits into single one, we can use this rebase and
git rebase -i <hashcode>
replace pick with s and press
(squashing)
ESC + :x and add a message and again :x ( or press
:wq)
Git Commands 1
git reset HEAD~2 it will unstage the last 2 commits
detailed info about the hashcode(folder or file)
git cat-file -p <hashcode>
authors etc.
git log —all —graph —decorate
(or)
give detailed log with timeline
git log —all —graph —decorate —
oneline
git diff shows the difference between new and old changes
git diff <commit1_hash>
shows only the diff between file.txt in that commits
<commit2_hash> file.txt
git add -p filename we can partially stage the changes in the file
git ignore feature
add specific file names or regex inside .gitignore file to stop tracking that
particular file
Rebase:
it is making the changes above the
master branch.
moving our commit on top of the latest
commit.
git rebase master
it will put the feature branch on top of
master, execute this in feature branch
Squash:
Git Commands 2
it is combining all the commits and
merging it as 1 commit.
Git Commands 3