Basically everything you need to know about Git

Learning Version Control is undeniably a must for any Software Developer to learn and Git is one of the best tools to do it, whether they’re working on their own projects or collaborating with others. The power to retain versions of your code, undo destructive changes and merge updates from different areas of code into one single codebase is just too useful for the developer to pass on.

While some things in Git are straightforward, complicated situations can get… well, complicated. Fortunately, if you just follow the standard Git workflow properly, you won’t have to get into those situations.

That being said, here’s basically everything you need to know about controlling your versions. Keep in mind pretty much all of these are going to be commands you run on the terminal. I will also be saying VCS a lot here, which in case you don’t know, means Version Control System (like Github or Bitbucket).

Creating a new Repository

Create a new directory and inside it, perform

git init


This sets up a repository inside your folder so all files inside that directory can be readily added to Git for committing and stuff.

Connecting your local repository to a remote server

The true power of VCS only comes when you connect and commit your codebase online. To do this, create a new repository using the VCS of your choice.

Then simply run the commands

git remote add origin
git push -u origin master


Now your directory is ready for Git.

Cloning a Repository

If there’s a repository online (On Github or Bitbucket per say) that you want to clone into a local directory, use the git clone command.

git clone /pathorurl/to/repository


Some VCS websites like Github and Bitbucket also make it slightly easier for you by either giving you the URL or the whole command to simply paste into your terminal.

When you do this, the local repository which you have just cloned will already be connected to the remote server.


Adding and Commiting Files

Although your directory is already marked as a repository (from either cloning or running git init), you may need to add files into the git index manually.

git add <filename>


Or to add all files in the current directory

git add *


And then you can commit your added files

git commit -m "Commit message"


What committing essentially does is place your files in a limbo called the HEAD. The HEAD tracks your last commit and gets it ready for pushing into the remote repository (or online).

To finally push your commit into the remote repository, send them off with

git push origin master


“master” in this case points to the branch. More on branches… right now!

Branching 101

You can view a Git Repository like a tree. On top you have the master branch. This is supposedly where you keep production-working versions of your code.

It’s general practice to have a develop branch below the master branch and branches stemming from develop for every added feature or bug fix. This is so those individual branches can be worked on individually, then merged into the develop branch where the whole codebase is tested before being pushed to master.

Checking your current branch

You can check check your current branch and all the changes it contains by simply running

git status


Creating a new branch

To create a new branch off of your current branch, simply execute

git checkout -b <branch name>


Deleting a branch

To delete a branch, execute

git branch -d <branch name>


Switching between branches

To move from one branch to another, simply call

git checkout <branch name>


This returns you to the last commit on the branch you’ve switched to. You might notice that if you made any changes to your current branch that aren’t committed, you can’t switch out of that branch. That’s where stashing comes in.


Stashing 102

To stash is to save the changes you’ve made on your current branch, in case you want to switch to another branch without committing the changes you’ve made on your current one.

Save a Stash

To simply save your stash, you can use

git stash save "stash name"


Note that when you stash, all of your uncommited changes on that branch will be undone after it is saved.

View your Stashes

To see the stashes you’ve saved, run

git stash list


This will give you a list like this

stash@{0}: On develop: updated the fishies
stash@{1}: On Feature/added-fishy-food
stash@{2}: On Bugfix/fix-red-leicester-error


Apply a Stash

To apply a stash you’ve previously saved, run this (replacing n with the number of the stash you want, as shown in the stash list).

git stash apply stash@{n}


You can also run git stash apply to automatically apply the stash@{0}.


Deleting a Stash

If you don’t manage your stash list well, it can be cluttered with stashes you don’t need. To delete a stash, call

git stash drop stash@{n}


Just like with apply, you can simply use git stash drop to delete the stash@{0}.

Using git stash to undo all changes made in the current branch

Due to the nature of stashing, you can use it to undo all uncommitted changes in your current branch.

git stash save
git stash drop


Running these two commands is an effective undo all changes command. You can also omit the drop if you want to keep the stash as a backup you can return to, in case you want to redo your changes again.

Legal Logging

Often, you’ll need to look into various parameters within the repository, whether it’s the git actions you’ve recently performed, commits you’ve made on your branch and plenty of other stuff.

git log


There are about a million parameters you can add to this as well. To find out, let your terminal tell you by calling git log –help

Undoing Oopsies

If you’ve made an oopsie, don’t fret. That’s what Git is for.

If you want to undo the working changes of one file..

git checkout -- <filename>


If you want to undo all changes and commits in your working tree and reset it to the latest commit on the remote server, we finally resort to the hard stuff.

git fetch origin
git reset --hard origin/master


And that’s pretty much it… except it’s not

There’s so much to git, but I hope this covered all the basics. If you ever find yourself in a real pinch because you’ve done an oopsie that isn’t as simply undone as the problems addressed here, StackOverflow is your friend. Happy coding ༼ つ ◕_◕ ༽つ