How to Create Your First GitHub Commit
How to Create Your First GitHub Commit
This article shows you how to create your own GitHub commit by making your repo, create your commit, and link your remote repo.
Join the DZone community and get the full member experience.Join For Free
Hello everybody. My name is Mar, and today I want to guide you through the most important step of your Tech career, your first interaction with Github.
I started off as a Computer Science student not long ago, in 2016. However, it took me a whole year to give this Github thing a try. I was hesitant and confused at first, but it did not turn out to be as complex as my peers made it sounds when they were talking about it. All it takes is some motivation! Now let’s dive right into it.
I have a few projects on there already that you can just disregard. Now let’s start by creating our first repo. Repo is short for repository, and my personal explanation for it is that it is just a regular folder that will hold your project files. What is special about this folder is that it “watches” your files. And here is where the magic of version control comes, but more on that to come later. To initialize a repo, go to the repositories tab and click on the green button labeled “New.”
You may also enjoy: GitHub: Be Social, Contribute, Learn
Creating a Repo
You can now give your new Repo a name (usually you project’s name), and a short description. Do not initialize a README at this time as it might cause complications later. You can always go back and do it later.
I got this far without frustration when I was starting up on Github, but then I came at this screen and I said: “Okay, what now?”
Now let’s forget about what is online, and let’s dive into the Git part of Github. If you have a Windows or Linux machine, you will need to download and install Git. If you are using a Mac, you should have Git installed by default as long as you have Xcode.
Now make a folder in your preferred location, and open a terminal window (cmd for Windows) and navigate to your folder on it. We want to initialize this folder as a Git Repo. Which means that we are now going to make this folder special, “watched for” as I said earlier. Here you can see me navigate to my folder called myfirstwebpage that I created on my desktop.
cd stands for change directory and it is how you navigate from a folder to another.
Now make sure you are in the correct folder and type the command
git init. Congratulation, you just created your first repo on your local machine! You can type
git status into the terminal, to see all that is going on.
At this point, you can start adding whatever files you wish to work on! For this demo, I am going to create an HTML file and add some code to it on the code editor VSCode. You could use any editor to accomplish this. Make sure you are still working in the same folder.
Time for the Commit
It is about time for a commit. I wrote some code, I am satisfied with it, and I want to save this state of my project now. Let’s go back to the terminal window and make our first commit. Use the command
git status. You will get a different output from the first time you ran the same command. This is because Git detected that something is up in the folder that it was not notified about. Remember, it is “watching” it.
You will need to run a couple of simple commands on the terminal. First:
This command will add all the files in your repo to the staging area which means that Git now thinks, “Ah, I’m asked to actually pay attention to the changes that happen to all these files now!” It is good to type
git status every once in while to see what is going on at every step.
"<commit message>" with an actual message such as “initial commit,” or “server side hot-fix,” or whatever it might be. Make it short and descriptive! Note that the commit message is required and if you omit it, you might open the terminal editor which is annoying to get out of (try
:q if this happens).
Congrats! You just made your first commit!! This short video sums up the commit process in case you missed something:
Link Remote Repo
The next step is to link the remote repo that you created on your Github to the one sitting on your desktop. To do so, you will need to get your remote repo’s “link.” This is found by clicking “clone or download” on your repository’s page. You can click on the rightmost button next to the link to copy it.
Next, go back to the terminal, and type this command:
You should only have to use the above command once to get the remote branch setup. Afterwards, you could repeatedly push to the remote branch whenever you want to publish your commits to Github using this command:
Now go up to Github and lo and behold, your files that were just on your local machine are in there, too. You just uploaded some files to the cloud by only using the terminal! You should be proud of this accomplishment!
If you made it this far, you are now set to up for success. You can continue exploring the power of version control hassle free. All you have to do is look up the commands, and Google is your best friend! I personally took much longer to get to this point than to do anything after that. Some of the most powerful things you can do with Github are branching, reverting commits (or literally just going back to an older commit to copy some stuff you want to get back!), and most importantly, the ability to have multiple people working on a single project.
Commands and Operations in Git
Published at DZone with permission of Marouen Helali . See the original article here.
Opinions expressed by DZone contributors are their own.