How To Commit and Connect Local and Online Repositoryđ
In Part 1, we learnt what Git, GitHub is and examined the main uses for GitHub. We also began the process of signing up for GitHub and creating our own local repository for code.
Now that these steps have been accomplished, letâs add the first part of your project now by making your first commit to GitHub. When we last left off, weâd created a local repository called FirstProject.
On the next line, type:
touch Readme.txt
This, again, is not a Git command. Itâs another standard navigational command prompt. touch
really means âcreate.â Whatever you write after that is the name of the thing created. If you go to your folder using Finder or the Start menu, youâll see an empty Readme.txt file is now inside.
You can see your new Readme file in the MyProject folder. But can Git? Letâs find out. Type:
git status
The command line, usually so passive up to this point, will reply with a few lines of text similar to this:
Whatâs going on? First of all, youâre on the master branch of your project, which makes sense since we havenât âbranched offâ it. Thereâs no reason to, since weâre working alone. Secondly, Readme.txt is listed as an âuntrackedâ file, which means Git is ignoring it for now. To make Git notice that the file is there, type:
git add Readme.txt
Notice how the command line gave you a hint there? All right, weâve added our first file, so itâs time to take a âsnapshotâ of the project so far, or âcommitâ it:
git commit -m âAdd Readme.txtâ
The -m
flag, as noted in the terms directory in Part 1, simply indicates that the following text should be read as a message. Notice the commit message is written in present tense. You should always write your commands in present tense because version control is all about flexibility through time. Youâre not writing about what a commit did, because you may always revert to earlier. Youâre writing about what a commit does.
Now that weâve done a little work locally, itâs time to âpushâ our first commit up to GitHub.
But wait, we never connected the online repository to the local repository. In fact, your local repository and your online one are only connecting for short bursts, when youâre confirming project additions and changes. Letâs move on to making your first real connection now.
Connecting Local Repository To GitHub Repository
Having a local repository as well as a remote (online) repository is the best of both worlds. You can tinker all you like without even being connected to the Internet, and at the same time showcase your finished work on GitHub for all to see.
This setup also makes it easy to have multiple collaborators working on the same project. Each of you can work alone on your own computers, but upload or âpushâ your changes up to the GitHub repository when theyâre ready. So letâs get cracking.
First, we need to tell Git that a remote repository actually exists somewhere online. We do this by adding it to Gitâs knowledge. Just like Git didnât acknowledge our files until we used the git add
command, it wonât acknowledge our remote repo yet, either.
Assume that we have a GitHub repo called âFirstProjectâ located at https://github.com/username/FirstProject.git. Of course, username
should be replaced with whatever your GitHub username actually is, and FirstProject
should be replaced with the actual title you named your first GitHub repository.
git remote add origin https://github.com/username/FirstProject.git
The first part is familiar; weâve used git add
already with files. Weâve tacked the word origin
onto it to indicate a new place from which files will originate. remote
is a descriptor of origin
, to indicate the origin is not on the computer, but somewhere online.
Git now knows thereâs a remote repository and itâs where you want your local repository changes to go. To confirm, type this to check:
git remote -v
This command gives you a list of all the remote origins your local repository knows about. Assuming youâve been with me so far, there should only be one, the FirstProject.git one we just added. Itâs listed twice, which means it is available to push information to, and to fetch information from.
Now we want to upload, or âpush,â our changes up to the GitHub remote repo. Thatâs easy. Just type:
git push origin master
The command line will chug through several lines on its own.
Log into GitHub again. Youâll notice that GitHub is now tracking how many commits youâve made today. If youâve just been following this tutorial, that should be exactly one. Click on your repository, and it will have an identical Readme.txt file as we earlier built into your local repository.
Congratulationsđ, you are officially a Git user! You can create repos and commit changes with the best of them.
GitHub Resources đ§
- GitHub Guides. If youâre a visual learner, GitHubâs official YouTube channel is worth your time.
- Git Reference. Got the basics down but find yourself always forgetting the commands? This handy site is great as a glossary reference
- Git- the simple guide. This tutorial is short and sweet. If you want to refresh on the basics of Git, this should be all you need.
- GitHub- Git Cheat Sheet.
Ping me on twitter for any queries. đ¤
â â Happy Hacking đ
If you are a student you might be interested in knowing more about Microsoft Learn Student Ambassador program. đ§