Clone – Bring a repo hosted in somewhere into a folder in local machine
add – Track your file and changes in git
commit - Save your file in git
push – Upload git commit in remote repos
pull – Download changes from remote repo to your local machine, opp. of push
git status – tells files which are changed or which are not present in the git repo but are in local m/c.
git log - Logs with hash value of each commit.
git reset (hash value) – Undo changes till the hash value of that commit
git branch – Tells all the branches and the current branch starting with *
git checkout – Switch between the branches
git checkout -b ________ - Checkout from current branch and create new branch with name ____.
git push –set-upstream origin (branch name eg main)/ git push -u origin (branch name eg main) –
To shorten the thing and after that in that branch you just have to do git push.
git diff – It shows two versions of the code and compares and shows what are the lines that are
changed.
git branch -d (branch name) – This will delete the branch
git reset – Refer Undo git section
How to commit
git commit -m “Message about the things you do” -m “Description message”
git init
Used to initialize .git in that folder that is created locally in the machine.
after that do – git staus
git commit -m “ ” -, “
git push origin main ” - But here we’ll face error because there is no repo on githut to push this
commit in
To resolve cerate a repo in git hub than on gitbash type
- git remote add origin git@[Link]:shadygetsin/[Link]
- git remote -v
Now simply do git push origin main
git push upstream
It is used to avoid to type extra after git push, just type git push no need for origin main and set this
to default whenever you’re pushing something.
GIT BRANCHING
git branch – How many branches are there and *Branch is the branch in which we are currently in.
git checkout - Used to switch between branches.
git checkout -b – To checkout and create new branch
Eg – git checkout -b feature-readme-instruction
git checkout feature-readme-instruction - Then change the file in the new branch. and do git add,
and git commit.™
And when you do git checkout main – All the changes will go away as you open your master branch.
Merge
Before merging you need to see difference (using git diff) in the code of main and feature-readme-
instruction branch.
Steps -
First push your changes in feature branch.
Open that repository on github and it will pick up that we’ve pushed changes in the feature
branch.
And it will ask for compare the new branch with another branch(maybe main) and pull
request.
And with arrow you can see which branch is merged in which branch.
After creating pull request, if everything’s looks fine just accept Merge Pull request.
But after this you’ll not see the changes locally because you’ve merged the code remotely in github.
And there you switch by git checkout main and then do git pull origin master.
Now local main will also be updated with the changes with the feature branch.
After that we can delete the feature branch with git branch -d (branch name) – This will delete the
branch.
Exception -
But in real life we many times get merge conflicts. Some times your code is redundant or different
things on both main and feature branch than git will get conflict which one’s to add and which ones
to leave or add both but which one’s first while merging the main or feature branch things.
So when you do git merge main when you are on feature branch and it will show conflict. Your code
editor will be opened and delete the lines that git added and add what you wanted to add and let git
know what to do.
Then again do git commit and after that do git merge master.
UNDOING in git
Undo an add
If you do changes in a file(code) and add it using git add [Link] then to undo this last
operation you can do git reset to unstage that the last add change.
Undo a commit
git reset HEAD~1
Head is telling git it is about last commit ~ means instead of go to last commit get 1 step further that
is before the last commit and reset or unstage the changes.
Undo many commit before –
git log
It is the log of each commit that you have made and each commit has a hash value the you can
simply copy the hash value of that commit and do the following
git reset (hast value of the commit) – It will unstage all things till that commit
git reset –hard (hast value of the commit) – It will not only unstage but also erase every thing till
that commit.
FORKING in git
Forking means make a complete of a someone’s repository and make it yours. So that there will be
no restrictions when you will be doing changes in that code there will not be any issue for pull and
mere request because you will be the administrator of that copied repository.
Just click the fork
Incomplete Learn more about forking.