GitHub was a confusing and intimidating platform for me when I first started programming. I wasn’t sure what the point of it was or why I should be using it. After a few years using GitHub, I figured I would write a quick breakdown of the platform that would have been useful for me back then.
To get started, let’s discuss why GitHub is used. GitHub is a place to host code, collaborate with other developers, and keep track of how the code has changed over time. This is also known as version control and it is important because if there is a problem with the code that has been pushed to production, there is a way to easily roll back changes to the last version of functional code. Also, it is not a good idea to only have versions of your code on your local machine in case your machine becomes compromised.
I will assume you already have a GitHub account and Git installed on your system and text editor. Now let’s get you started.
Go to GitHub and create your new repository.
Create your project's folder in your text editor, add a file, and add some code.
Open the terminal in your text editor or open the terminal/cmd application and change directories into your project folder.
- You will now want to run the
git initcommand. This command is how you create, or initialize, a new project repository on your local machine.
When you run the command above in the terminal and then run the
ls -lacommand, you will see a folder named .git listed in the project structure. This folder is used to store project data that is used to keep track of changes occurring in each file.
Once you have initialized the repository, you will want to add a file from your text editor to your local repository. For example, I have a file named index.html that needs to be added to my local repository. In the terminal, I am going to run
git add index.html. This command moves the index.html file from the working environment into the staging environment. The staging environment is for files that are ready to be added to the repository.
If you run the command
git status, you will be able to see that index.html has been added to the staging environment, but has yet to be committed.
To commit a file, use the
git commit -m "commit message here"command. When a file change has been committed, it updates the .git folder that will save the current state of that file. To verify that this has been done, run the
git logcommand to view information on commit history.
git branchcommand to see what branch you are on. Git defaults to the master branch, but you are able to change the name if you want to. If you would like to change the name of this branch, you can run
git branch -M “newBranchNameHere”.
The steps above created the local repository, but the last few steps involve creating the remote repository to push code onto GitHub's platform.
Grab the url listed on the setup page.
- The command
git remote add origin https://github.com/ceblakely/practice1.gitconnects your local repository to the GitHub repository where your commits will be published. The term
originis the universally accepted term for the remote repository, but you could change the name if you wanted to. Lastly, run the
git push -u origin mastercommand to push index.html from the local repository to the remote repository on the master branch.
You will have now successfully added a file to your GitHub repository! Below are a few extra tips:
If you wanted to add all files in your project to the staging area, instead of one at a time, run the command
git add .
Note the difference between staging, committing, and pushing.
- places a completed file in another area to show that it is ready to be committed. This allows you to work on multiple files, stage each one as you finish them, and then commit them at the same time.
- adds staged changes to the local repository. This must happen before they can be pushed to the remote repository.
- transfers the commits to the remote repository on GitHub.
Branches are used to save different states of the code. You can have production-ready code on the master branch, create another branch named
mainand try out new features on that branch without affecting the deployed code. If you wanted to create a different branch to experiment, use the command
git branch branchNameHere.
I know the GitHub documentation can be a bit overwhelming, so I hope this helps someone getting started with GitHub! 💫
Top comments (0)