Github Tricks — A Short Tutorial

Alena Dagneau
Make School
Published in
5 min readNov 17, 2015

Over the years as a developer, I’ve started writing down little short cuts and reminders on how to do all things Github on the command line. Sure, there are those commands that I use everyday and so I don’t think twice about it but then there are those commands that you know exist but you just aren’t sure about the syntax anymore.

So here’s my collection of all Github commands I’ve used over the years:

Forking

Mainly this is just the same as using the same repository (also called repo), though updating to the latest code changes slightly.

  • On Github, go to the repo you want to fork and click on the “Fork” button on the top right. In my example, I’m using the MakeSchool Swift-Intro-Playgrounds repo.
Forking a repository
  • Choose to fork it to your own Github name.
Selecting an account to fork a repository
  • Go to the new forked repository and clone it to a folder on your desktop on your computer using the command:
git clone _your_forked_repo_

(something like git clone git@github.com:MakeSchool/Swift-Intro-Playgrounds.git)

You can find the correct path on your repo page (see image below)

Finding the git clone path
  • You still need to add this forked repo as a “remote”.
git remote add upstream git@github.com:MakeSchool/Swift-Intro-Playgrounds.git

=> This command will create a remote called “upstream”

  • Now get the latest code from the upstream repo.
git fetch — all or git fetch upstreamgit pull upstream master

Collaborating on Forks

This is pretty much it for working on your own with a forked repository. However, if you work in a team you might need to check out someone else’s branch to work on it.

  • Checking someone else’s branch on a fork out.
git checkout -b a-new-branch-name => make a new branchgit pull git@github.com:nsafai/Swift-Intro-Playgrounds.git name-of-the-branch-you-want => pulls someone else’s branch onto your new branch

Now, you have a new branch with someone else’s code in it. If you want to push it, it will go into your own branch on your own fork.

Remove all merged remote branches

When you work with forks, you end up with a bunch of branches that have been merged. To remove them, you can use this command:

git remote prune origin

Selectively commit code

The command git add . can add all of your changes into a commit at once. I think it’s bad practice to use this command though because it doesn’t allow you to see all your changes one more time. Make sure that you didn’t leave a debugger or console.log in the code. Instead of adding everything at once to your commit, you should add it chunk by chunk.

git add -p

When you use this command, you get a little prompt showing you the chunk that you’re about to commit and you can choose from several options. These are the ones, I use most:

  • y => yes, commit this
  • n => no, don’t commit this
  • q => quit the adding process completely
  • s => split the chunk you can see into smaller chunks

Recursively delete files

Sometimes, you have a whole lot of files that have been deleted in one go. When you delete a folder with lots of files for example. Instead of deleting those files one by one, you can use this command:

git rm $(git ls-files — deleted)

Lots of Other Useful Commands

There are of course loads more but this is a good start on Git in the command line.

BASICS

  • Getting Help => git help <verb> e.g. git help config
  • Cloning an existing repository git clone [url]
  • To figure out what status your files are at git status
  • Tracking individual files git add filename
  • Committing files git commit
  • Committing files without staging them first git commit -a
  • Committing files with a custom message git commit -m ‘some message’
  • View the differences between the same file in staged and unstaged git diff
  • Shows the lines of code that are staged git diff — cached
  • Remove file from Git git rm filename
  • Viewing the commit history in historical order (latest first) git log
  • Same as above but only showing the differences in each commit git log -p
  • Showing only the last 2 entries git log -2
  • Shows a summarised version of the log git log — stat
  • Shows a version of the log that is easier to read git log — pretty
  • Shows a version of the log in oneline git log — pretty=oneline
  • Create new branch git checkout -b ‘a-branch-name-you-like’

GIT STASH

  • Lets you “stash” away your code. Careful with this as it is branch independent.
  • Stash away all you have uncommitted git stash
  • Undo the last stash git stash pop
  • Look at all stash’s git stash list
  • Remove all stash’s git stash clear
  • Get only one stash of the stack git stash pop stash@{?} => ? is the number that you get when you do git stash list
  • Delete one stash of the stack git stash drop stash@{?}
  • Show the inside of the stash git stash show stash@{?} -v

UNDOING THINGS

  • Amend the last commit git commit — amend
  • Adding a forgotten file to the previous commit
git commit -m ‘initial commit’git add forgotten_filegit commit — amend
  • Unstaging a staged file git reset HEAD file_name
  • Unmodifying a modified file (reverts to the checked-out file) git checkout — file_name

REMOTE SERVERS

  • Showing remote servers git remote
  • Adding remote repositories git remote add [shortname] [url]
  • Fetching data from remote projects git fetch [remote-name]
  • Pushing data to remote projects git push [remote-name] [branch-name] e.g. git push origin master
  • Merging two branches
git checkout mastergit merge test_branch
  • Deleting a branch git branch -D test_branch (you need to be on a different branch for this to work)
  • Checking which branches are checked out git branch (Result with an * is the master branch that’s worked on)
  • Checking which branches are merged or not merged yet git branch — merged OR git branch — no-merged

That’s all I got for now. Have fun!

--

--