GitHub CheatSheet

Arnav Zek
4 min readApr 10, 2020

Get the list of untracked files and unstaged commits

git status

Remove a Git commit before a push

Undo commit and keep all files staged: git reset --soft HEAD~Undo commit and unstage all files: git reset HEAD~Undo the commit and completely remove all changes: git reset --hard HEAD~

Remove directory from git and local

git rm -r one-of-the-directories // This deletes from filesystem
git commit . -m "Remove duplicated directory"
git push origin <your-git-branch> (typically 'master', but not always)

Open Git UI window

gitk

How to create a new branch?

git branch newBranchName

Get the list of branch

git branch

How to switch branch

before push command

git checkout branch-name

How to create a new branch and switch to it at the same time

git checkout -b branch-name

How to merge a branch to master and then delete it

git branch -D branchName//if u use lowercase d, git will ask u first to merge

Adds all the untracked files

git add -A

but if you want to be sure no additional files are added, just give an -n parameter as well

git add -A -n

Update local repo

git pull origin master

See all new commits

git log

Crate local Repo

git initgit .addgit commit

Create GitHub Repo

If you want to make use of git hub, you will have to first create a project on github then

Make sure you don’t add lisence or readme when you create the repo from github otherwise it will give an error on pushing commits Updates were rejected because the remote contains work that you do not have locally.

git remote add origin https://github.com/cubeton/mynewrepository.gitgit pull origin master  //if you already have something uploadedgit add.git commit -amgit push -u origin master

Ignore Files

Add files under git .gitinore there is no format necessary, just file name and line breaks

apps
client
.env
node_modules

To push changes onto a new branch on GitHub, you’ll want to run git push origin yourbranchname.

after that changes can be merged into the main branch

What is origin?

It’s essentially shorthand for the remote repository’s URL. So, to push your changes to the remote repository, you could’ve used either the command: git push git@github.com:git/git.git yourbranchname or git push origin yourbranchname

Pull Request

A pull request (or PR) is a way to alert a repo’s owners that you want to make some changes to their code. It allows them to review the code and make sure it looks good before putting your changes on the master branch.

This is what the PR page looks like before you’ve submitted it:

Merging

You might see a big green button at the bottom that says ‘Merge pull request’. Clicking this means you’ll merge your changes into the master branch.

Note that this button won’t always be green. In some cases it’ll be grey, which means you’re faced with a merge conflict. This is when there is a change in one file that conflicts with a change in another file and git can’t figure out which version to use. You’ll have to manually go in and tell git which version to use.

When you are done, it’s best to delete the branch

Working with Git remotes and pushing to multiple Git repositories

exammple case: let’s say you have a Heroku git repo and you want to put that to GitHub as well

You can have as many remotes as you want, but you can only have one remote named “origin”. The remote called “origin” is not special in any way, except that it is the default remote created by Git when you clone an existing repository. You can configure a second remote, push to/pull from that remote, and setup some branches to track branches from that remote instead of origin.

Try adding a remote called “github” instead:

$ git remote add github https://github.com/Company_Name/repository_name.git# push master to github
$ git push github master
# Push my-branch to github and set it to track github/my-branch
$ git push -u github my-branch
# Make some existing branch track github instead of origin
$ git branch --set-upstream other-branch github/other-branch

Obviously the added files will be the same

Or you can make origin push to more than one git repository server at a time. (I don’t recommend as the earlier method has more control)

git remote set-url --add origin ssh://git@bitbucket.org/user/myproject.git

To Get list of remotes

git remote

How to Fix `Updates were rejected because the remote contains work that you do`

git push -f origin master

To change url of remote

git remote set-url origin ssh://git@bitbucket.org/user/myproject.git

we just skipped--add

Instead of ‘origin’ you can uses your own remote name

Resources

https://github.com/cubeton/git101/tree/master/TurtorialInfo

https://product.hubspot.com/blog/git-and-github-tutorial-for-beginners

--

--