Git & Github Cheatsheet
Git & Github Cheatsheet
Hello Everyone !
https://dev.to/zinox9/git-github-cheatsheet-22ok 1/18
17/04/2020 Git & Github Cheatsheet - DEV Community
Basic Commands
Git Config :
git config -- global user.name NAME = set user name globally
Creating repo
git init = creates a git repository in the directory currently in
Staging
git status = to check status , if staged or unstaged
https://dev.to/zinox9/git-github-cheatsheet-22ok 2/18
17/04/2020 Git & Github Cheatsheet - DEV Community
Commiting
git commit -m "Specific Changes Made" = commits the staging area
giving them a specific id
git log = shows all the commits with details
git log --oneline = shows all the commits in one line each
SPECIAL log : this will log the info in a nice format (Try it once 😉)
Git Stash
git stash = clears the changes to the initial state (last commit)
& creates a unique id for the current state
https://dev.to/zinox9/git-github-cheatsheet-22ok 3/18
17/04/2020 Git & Github Cheatsheet - DEV Community
using git stash multiple times creates a list of stashes of all states
with multiple ids
git stash list = shows all the stash (States) with their ID
Gitignore
a .gitignore file can be created , in which you can specify all the
folders/files that should not be staged and commited
For example : node_modules/ .css.map etc.
It's Good to create a gitignore at the start of Project
https://dev.to/zinox9/git-github-cheatsheet-22ok 4/18
17/04/2020 Git & Github Cheatsheet - DEV Community
https://dev.to/zinox9/git-github-cheatsheet-22ok 5/18
17/04/2020 Git & Github Cheatsheet - DEV Community
changes as needed) ... might take you to vim editor (type ":wq"
then "Enter" to exit)
git reset commit_ID --hard = will remove all the commits after
the provided id and even delete all the files and lines from local
directory too
Branches
Used to test a new feature or code , by creating a branch .. then
merging it to master only if needed
can be used for multiple developers working on same project .. create
different branch for each developer adding their own feature then
merging at the end
git branch branch_name = to create a new branch
Merging branches
after completing changes in a branch and commiting them
come back to master and run
git merge branch_name = this will merge the branch to master (all
commits show in master) = automatic
git merge --squash branch_name = this will merge the branch to master
(only the commit after merge is shown in master) = manual
Conflicts
If Branch's Base (First Commit) is Master's Head (Last Commit) = No
Conflict
If Master had commits after creating Branch = Conflicts Might Come
to solve this , edit the files manually , Solve The Conflicts then ..
run git add . and then git commit -m "Message" and the changes will
be made
https://dev.to/zinox9/git-github-cheatsheet-22ok 7/18
17/04/2020 Git & Github Cheatsheet - DEV Community
https://dev.to/zinox9/git-github-cheatsheet-22ok 8/18
17/04/2020 Git & Github Cheatsheet - DEV Community
NOTE! : It is specified in the git docs that rebase should not be used in
public repos (collaboration) as it can cause major errors and conflicts, it
can be used in private repos.
Github
Creating new & Cloning Repo
create a new repo on Github and copy the URL
now push your code to it with
git push git_url master = pushing code of master branch (to
push all branches replace master with --all)
creating an alias to not always type URL
git remote add origin git_url = origin can be name of
anything else, but origin is the word most commonly used
git push origin master = to push code to using alias
https://dev.to/zinox9/git-github-cheatsheet-22ok 9/18
17/04/2020 Git & Github Cheatsheet - DEV Community
Collaborating
Most of the collaboration features are already available on
Github, Example
git pull git_url = to pull changes from remote to local repo
https://dev.to/zinox9/git-github-cheatsheet-22ok 10/18
17/04/2020 Git & Github Cheatsheet - DEV Community
create a Compare & Pull Request when you want are ready for
the branch to be merged (with a message)
the reviewer of the repo will accept the changes and merge it
(and specify a merge commit message)
pull the project every time before editing to see the changes
git branch -r = helps us to see the remote branches & the
connections
Forking (Contributing)
to contribute to an open source project
click on fork , which will copy the repo to your account
make changes by pulling the repo, then push it ( this will
happen on your account )
then go to the owner account's repo and create a pull request
there
the owner can compare the changes and accept your changes
which will end up merging your changes to their project
https://dev.to/zinox9/git-github-cheatsheet-22ok 11/18
17/04/2020 Git & Github Cheatsheet - DEV Community
A developer, always getting into new stuff, exploring things and learning from experiences.
@zinox9 zinox9
PREVIEW SUBMIT
Apr 7
Pacharapol Withayasakpunt
Is git rebase --continue eventually similar to git merge ? I only tried git merge today.
2 REPLY
Apr 7
Arjun Porwal
You can try out git stash on some sample files and you will get the idea of what it is.
https://dev.to/zinox9/git-github-cheatsheet-22ok 12/18
17/04/2020 Git & Github Cheatsheet - DEV Community
git rebase --continue works when you started rebasing ( git rebase master ) a branch with other branch
(or master) that was ahead of it.
but yeah, if the branch that you are merging isnt ahead, git rebase might work in one shot.
git merge merges the branches (their commits) into one marking that it was merged from a branch.
Whereas takes the head of other branch, puts it in base of the branch that you are in, like
git rebase
the other branch was never there.
1 REPLY
Apr 7
Pacharapol Withayasakpunt
1 THREAD
Apr 7
Arjun Porwal
Yes VSCode's Version Control features , can help you a lot to track all this.
1
REPLY
Apr 9
Sujith Quintelier
Assume you are working in a certain branch, modifying and adding stuff.
https://dev.to/zinox9/git-github-cheatsheet-22ok 13/18
17/04/2020 Git & Github Cheatsheet - DEV Community
You need to create urgently a hot fix but you work has not been finished yet and you don’t want to
commit the changes yet. You can not switch branches if you have uncommitted changes.
This is where git stash comes into play. It resets the branch to the latest commit but saves a copy with
the current state before. Make sure you do a git add . before to include the new files as well.
No you can safely switch branches and make your hot fix.
After that, go back to your previous branch and apply the stash to continue where you left off.
2 REPLY
Apr 9
Arjun Porwal
Whoa ! That's really very helpful. Thanks For sharing the information 🤘
1 REPLY
Apr 7
Khoi Hoang
2 REPLY
Apr 7
Arjun Porwal
https://dev.to/zinox9/git-github-cheatsheet-22ok 14/18
17/04/2020 Git & Github Cheatsheet - DEV Community
1 REPLY
Apr 7
Tej-Singh-Rana
Good stuff . :)
2 REPLY
Apr 8
Arjun Porwal
Thank You 😄
1 REPLY
Apr 7
paras594
2 REPLY
Apr 7
Arjun Porwal
Thanks Buddy !
1 REPLY
https://dev.to/zinox9/git-github-cheatsheet-22ok 15/18
17/04/2020 Git & Github Cheatsheet - DEV Community
Apr 7
codebond
2 REPLY
94 22
https://dev.to/zinox9/git-github-cheatsheet-22ok 16/18
17/04/2020 Git & Github Cheatsheet - DEV Community
88 40
52 10
https://dev.to/zinox9/git-github-cheatsheet-22ok 17/18
17/04/2020 Git & Github Cheatsheet - DEV Community
https://dev.to/zinox9/git-github-cheatsheet-22ok 18/18