Github Cheat Sheet

Git is a awesome SCM solution.  Today GitHub is where everything happens.  Pretty much everything in sense of open source is there. It`s a great model todo contribution based on the idea of Pull Requests or just PR.

 However git and github could be complicated sometimes and there are situations you need do some commands get things done properly. IMHO command line tool is best them UI tools - maybe because when i start using git was not good tools around.

So today i just want to share a very simple Cheat Sheet for GitHub/Git that i use pretty much everyday - I would really love to create less branches and spent less part of my day doing this but unfortunately everybody is doing git full witch is not nice.


GitHub Cheat Sheet

1 - How to Create a Branch in Git/GitHub and PUSH it REMOTE?

git checkout -b [name_of_your_new_branch]
git push origin [name_of_your_new_branch]
git branch

2 - How to CLONE a remote branch in GitHub?

git clone -b [branch_name] [remote_repo_git]

2.1 - How create remote branch upstream in GitHub?

git checkout -b [name_of_your_new_branch]
git push origin [name_of_your_new_branch]
git push upstream [name_of_your_new_branch]

3 - How to DELETE a LOCAL BRANCH?

Delete LOCAL

git branch -d [NAME_OF_LOCAL_BRANCH]
# IF you want to FORCE to delete you can do
git branch -D [NAME_OF_LOCAL_BRANCH]

Delete REMOTE

git push origin --delete NAME_OF_REMOTE_BRNACH

4 - How to Download a GitHub Pull Request?

git clone [GIT_URL_MASTER]
git fetch origin pull/[PULL_REQUEST_ID]/head:[GIT_BRANCHNAME]
git checkout [GIT_BRANCHNAME]

5 - How to Sync a FORK with/from MASTER?

git remote -v 
git remote add upstream [HTTPS_URL_GIT_MASTER]
git remote -v 
git fetch upstream 
git checkout master 
git merge upstream/master 

6 - GitHub How to Sync a Branch inside a FORK with the Original Master for PR?

git checkout [BRANCH_I_WANT_SYNC]
git remote -v 
git remote add upstream [HTTPS_URL_GIT_MASTER] 
git remote -v 
git fetch upstream 
git merge upstream/master 
# RESOLVE ALL CONFICTS - IF THERE ARE ANY
git commit -m "Merge Master" 
git push 

7 - How to download a remote branch from github/git?

git fetch origin $remote-branch:$new-local-branch

8 - How to create a Tag?

git tag
git tag -a v1.4 -m "my version 1.4"
git tag
git push origin v1.4

9. How to undo git add?

git reset 
git status

10. How to Delete a remote Branch in github?

git push origin --delete $remote-branch-name

11. How to create an Empty branch in github?

git checkout --orphan $NEWBRANCH-NAME
git rm -rf .
# Edit your files
git add .
git commit -m "my commit"
git push

12. How to list all REMOTE branchs?

git branch -a

13. How to clone a TAG ?

git clone <repo_git>
git checkout tags/<tag_name> -b <branch_name>

14. Git Sync / Merge (master) - Preference Upstream and remove deleted?

git fetch upstream
git merge upstream/master --strategy-option theirs
git status --porcelain | awk '{if ($1=="UD") print $2}' | xargs git rm
git commit -m "Merge Master"
git push 

15. REVERT last 3 commits(will remove files from FS)

# Backup first :-)
git reset --hard HEAD~3

16. How to checkout remote branch?

git checkout -b master origin/master 

Have Fun :-)

Cheers,
Diego Pacheco

Popular posts from this blog

Having fun with Zig Language

C Unit Testing with Check

Cool Retro Terminal