Hey there! Welcome to my blog.
If you are reading this post, I know your time is very precious and you need to learn how to use git quickly in time for a hackathon hack! Well don't worry because you have come to the best place, and I promise to go through everything quickly and clearly.
Before we get setup, lets go over some prerequisites and briefly explain what git is.
- Know how to navigate folders using the command line
- Mac Users: make sure you have homebrew installed
What is Git
Git is featureful and versatile a version control system (VCS). A VCS is used to track changes in computer files among multiple people (or just yourself too). Using git you can easily:
- see who worked on the code and what they did
- revert back to a previous version of the code
- divert from the main code to build a certain feature, and then merge that feature back into the main code (accomplished by branches)
- show off your code and commits on a site such as GitHub or Bitbucket
How to install Git
First, set yourself up with a Github account.
Next, follow the appropriate steps below to install git. Mac or Linux users must run the commands in the terminal.
brew install git
sudo apt-get install git
Install git for Windows (Note for windows users: When entering the following git commands, use git bash)
How to Setup Git
Open up your terminal / git bash. Next, enter the following commands with your own name and GitHub email. For example, I would write:
git config --global user.name "Dillion Verma" git config --global user.email "email@example.com"
How to Use Git
Scenario 1: Brand new repository
First, lets consider the scenario where you are making a brand new repository.
- Go to github.com and create new repository
- Now you will be presented with many steps on GitHub which are self explanatory. Here is a quick recap
To make a brand new repository from the command line:
echo "# test-repo" >> README.md git init git add README.md git commit -m "first commit" git remote add origin https://github.com/dillionverma/test-repo.git git push -u origin master
To push existing code to GitHub:
cd test-repo # navigate to your folder git init git remote add origin https://github.com/dillionverma/test-repo.git git push -u origin master
Scenario 2: Contribute to repository
Let's consider a scenario where your group members have already created a git repository on GitHub, and you want to write code for this repository.
- Go to the repository url. It should look something like this:
- Click on "clone or download" button and copy the "clone with HTTPS url"
- Open terminal / git bash and navigate to your desired code folder and clone the Github repo:
cd Documents/workspace git clone https://github.com/username/repository_name.git
The purpose of
git clone is to copy all contents of the repository from GitHub to your machine. The alternative solution would be to download as a zip. Now you are ready to dive into the code and modify it with your favorite text editor.
Once you are done making your changes, go back to the terminal/git bash and follow these steps:
cd Documents/workspace/repository_name git add . git commit -m "worked on landing page" git push origin master
I will try to break down the commands by using a container analogy to explain what the steps mean.
git add is a way to add files to the staging area. Think of this as placing your code into a container. The
. is a way to specify the current directory. Hence,
git add . adds all files in current directory to staging.
git commit -m "worked on landing page" is used to write a commit message. Think of this as closing your container of code, and labeling the container with whats inside.
git push origin master is a way to push your code to the origin on the master branch. Think of this as a way to ship your container to the main floor of an apartment building. If you look closely at scenario 1 you can figure out where we set the url of the origin. For scenario 2 we don't have to set the origin url because when we clone, we already get that information.
And your code is now on GitHub!
Thank you for reading and I hope you have learned just enough git to get going on your hackathon hack. Good luck!
Here are some extra resources to learn more about git: