Coming from a developer background, saying I use Git often is an understatement. Lately, I have been sharing my knowledge for git with others at Concurrency, so I wanted to create a resource for anyone new to git. This blog will introduce the basic commands when using Git Bash (git’s built in command window) as well as basic git concepts.
Creating a Repository
First, you will need a repository (repo). The repo can be one that is currently created for you, or a newly created repo.
I recommend creating the repo online (GitHub, Bitbucket, etc.) and cloning it to your local machine. In this example, I will be using GitHub to demonstrate.
To create a repo, create an account or sign in. Once logged into GitHub, navigate to Your Repositories. Click the green New button, name your repository, choose Public or Private, and check Initialize this repository with a README. Click Create Repository to finalize your settings and create the repo.
At this point your repository should have one file in it, called README. Having at least one file in the repo unlocks the ability to clone. Cloning a repo copies whatever is currently in the repository to a directory locally on your computer. The new local copy is what becomes your working directory.
Next, navigate to the folder on your computer where you would like your local working directory to be saved.
Use the following command to clone your repository.
git clone https://github.com/username/repository-name.git
NOTE: To find your exact link, navigate to your repository’s page and click the green Clone or download button.
Creating a Branch
Now that you have a local copy of your repository, I recommend making a branch. Branches help isolate parts of your repository and make collaborating with other teammates on a repository easier. Branches keep different versions of the code, allowing many people to develop at the same time. Branches can be made for many reasons, most commonly bug fixes or features. To create a branch, use the command below.
git checkout -b BRANCH_NAME
Just in case you wish to switch back to the main branch, referred to as the master branch:
git checkout master
A branch is not available to others in the repository unless you push it to the repository.
git push origin BRANCH_NAME
If you have trouble remembering checkout commands, think about a library book. You checkout the library book, use the book, and then return it when you are finished reading. Same thing goes for the branches, checkout the branch, work on the branch, and then push your changes when you are finished, and switch branches again if necessary.
What happens if a teammate works on the project and has updated the code in the repository? You might need the updated code, to continue your piece of the project. In this case, pulling the code from the repository is what you need. To help remember, pulling on an object brings it closer to you. In this case, your computer. To pull code to your computer, use the command below.
Now you are the one who made changes your teammates might find beneficial, or you are frantic about your hard drive failing. These concerns are all solved by pushing. To help remember, pushing an object normally brings it away from you. In this case, your repository.
Pushing requires a few steps. I like to use the status command as a catch-all to ensure I know which files are modified. It is possible some files that you modify could break the code in the repository, thus it is a better decision not to push those files. To check the status, use the command below.
NOTE: All the files listed in red are modified files.
All the modified files are ready to be added to a commit. To add all the modified files, use the following command.
git add . or git add *
Let’s say there is only a specific file you wish to add to a commit. Instead of the period, type the filename instead.
git add FULL_FILE_NAME
Now that all the files are added to the commit, the next step is to create the commit. A commit groups the changes each time you or a teammate pushes code. Commits help sort when code was added, as well as who added it. Inside the quotation marks, type a short summary of the changes made in the commit. To commit your modified files, use the command below.
git commit -m “MESSAGE SUMMARIZING THE COMMIT”
I know this is a tedious process, but your code is almost pushed! To push code to your repository, use the command below.
git push origin BRANCH_NAME
Now that your code is pushed, you know all the beginner’s tools for git! I hope that this has been beneficial for you to help get your projects up and running. Best of luck on your git adventures!