Welcome to "Git that Code!" The 2nd post in my Git series. In this post, I'm going to talk about the popular remote servers, GitHub, GitLab, and BitBucket, how to get your code on the server, and retrieve code from it. Let's get started!
There are 3 Git server options that you hear about, well you might actually only hear about the first 2. You could also roll your own, but that's a bit out of the scope here. So, here are some options that are free and offer quite a bit.
These are just the most "popular" options and each have their pluses and minuses, but here are some general features that each offer on their free tiers.
- Free, unlimited private repos (where the code lives)
- Issue tracking
- Wiki per repo
Now here is what each is mainly known for:
- GitHub: Open source software. Most people put their OSS on GitHub for others to contribute to or use
- GitLab: Contains more devop options like Continuious Integration and more access levels
- BitBucket: Has unlimited private repos and integrates well with JIRA and other atlassian software
In this post, I'll be demoing using GitHub, but it is relatively the same on all 3 platforms.
Now, that we know where to store code, let's talk about getting code onto the server. To get code onto the server, you must
push the code into a repository.
What I like to do is start the repo in your choice of server, mine is GitHub and then get the repo onto my computer, but in the last post I walked through making a git repo locally, so let's get that repo onto GitHub.
The first thing I need to do is make a blank repository. This repository is going to be where the code will be stored.
- Open your browser of choice
- Navigate to your server
- Sign in
- Create a new repository
- Don't check any of these boxes, but make sure to give your repo a name.
Now, this is the remote repo that needs to be added to our local repo. GitHub gives us some handy quick setup which is what we are going to follow.
- Open your terminal app
- Navigate to your local repository
- Add a remote url to the local git repository
git remote add origin
- Now we need to create a branch locally
git branch -M main
- Then, we
pushit to the server. basically we are uploading it to the server now.
git push -u origin main
Now the code should be in my GitHub repo. Let's refresh and BAM it's there.
After commits have been made, we need to run
git push which will push the commits to the remote server.
When do you push your code? All the time. Sometimes I do it after a long session. Sometimes I do it after each commit. Just make sure your code in the remote server is as up to date as possible. In case things go south, you want a way o bring that work back and the history back.
We just talked about pushing code to the server, now let's talk about pulling and cloning code from the server. There are two basic commands to get the code from the server. They are
git clone and
git pull. Each are used in different scenarios.
git cloneis mainly used to get a repository from the server that doesn't exist on the local computer. If you stumble upon a project on GitHub that you want to hack on, then you clone it to your machine.
git pullis mainly used to get changes to a repository that you are working on locally. It can be used if you're working on a team or make a change on the web that you need on your computer.
Let's walk through an example of cloning a repo. I'll clone my
hello-world-js repo from forever ago.
- Open terminal
- Navigate where you want to store the repo for me that'll be my developer folder
- Grab the URL to clone from the repo
- In the terminal type the command
git clone firstname.lastname@example.org:maeganwilson/hello-world-js.gitand press enter
Now, we can edit the file and push it back.
Alright, cloning was kinda easy, now let's try pulling. Like I said, pulling can only happen when there are changes on the server that you need to git. So, I'm going to make some changes in GitHub.com in the iHog repo.
Now, in my terminal, I'm going to change my directory to iHog and then run
In the terminal, we see how many changes were made and a bit more.
That's a very basic overview of using a remote server and how to pull and clone code.
In the next post, I'm going to cover branches and not the outside tree kind.
If you want to chat with me and point out some tips and tricks, you can watch me work on iOS apps on Tuesday, Thursday, and Friday mornings at twitch.tv/maeganwilson_ Make sure to drop by and say hi!