DEV Community

Christina Blakely
Christina Blakely

Posted on

GitHub Basics: Init, Commit, & Push

GitHub Heat Map
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 new repository on GitHub

Create your project's folder in your text editor, add a file, and add some code.

text here

Open the terminal in your text editor or open the terminal/cmd application and change directories into your project folder.

  1. You will now want to run the git init command. This command is how you create, or initialize, a new project repository on your local machine.
  2. When you run the command above in the terminal and then run the ls -la command, 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.

  3. 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.

  4. 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.

  5. 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 log command to view information on commit history.

  6. Run the git branch command 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.
text here

  1. The command git remote add origin https://github.com/ceblakely/practice1.git connects your local repository to the GitHub repository where your commits will be published. The term origin is 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 master command 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:

  1. 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 .

  2. Note the difference between staging, committing, and pushing.

    Staging

    • 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.

    Committing

    • adds staged changes to the local repository. This must happen before they can be pushed to the remote repository.

    Pushing

    • transfers the commits to the remote repository on GitHub.
  3. Branches are used to save different states of the code. You can have production-ready code on the master branch, create another branch named main and 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)