"The best antidote for fear is competence" - Chris Hadfield
The only reason something is ever scary is because we don't understand it. Understanding git can be hard. This often makes it scary for a lot of people, especially those new to it.
Here are 3 things you can do that will massively help you to understand git (note that these principles will absolutely transfer to using GitHub and Gitlab):
1. Draw it out
Visualising the flow of git is probably the most useful thing you can do to clarify the steps in your mind - even if it's just listing them out in a good old fashioned bullet point. If you're feeling adventurous, even drawing it out as a comic can help (check out the one I created below):
2. Make a cheat sheet.
Having a summary of the key words and terminology used in git is just handy to have. I've started mine off with the basics, but feel free to add it it as you go and discover new levels of git-ness (sure).
3. Do it!
Step 0: Install git and create a GitHub account
Most computers already come with Git installed. You can check by opening up your terminal or command prompt and checking the version of git:
Step 1: Fork an existing git repository
Click on the fork button in the top right and choose your account.
Now you have a copy of this repository in your own account. This means we can make whatever changes we like now. Cool beans.
Step 2: Clone a local git repository
In your own, newly forked Hello-World project, click on the green code drop-down button, and copy the URL that's there.
Open up your terminal navigate to a folder that you want to store your new git repository in. It's best to make a specific folder for this. Once there, run this command in your terminal to clone the repo:
git clone https://github.com/octocat/Hello-World.git
Step 3: Create a new branch
Nice work! Now we'll make a new branch called ReadMe_Update.
First let's check what branches are already there:
Awesome! We can see master there already. Let's create our new branch using the checkout -b command:
git checkout -b"ReadMe_Update"
If we run git branch again, we'll see our new branch.
Step 4: Make a change
Now that we are safely on our own new branch, we can make a change to the repo. Open up the README file and type in whatever you want. For example, instead of 'Hello World!' mine now says:
Hello Jupiter! It is I, planet Earth. Hope you are doing well. I was worried about your spot for a bit but it seems okay. Love, Earth.
Save the file.
Step 5: Add and commit the change
In your terminal, navigate into the Hello-World folder. Then add your change to the staging environment of your 'ReadMe_Update' branch. To do this, just type in the command:
git add .
Now the change is staged! To commit it to your branch, run the command:
git commit -m"Changed message to say hello to Jupiter"
Your message (between the quotation marks) can say anything you like.
Step 6: Push your branch
Easy peasy - pushing your branch will update it on GitHub. To do this, run:
It will come up with a message that says 'there is no upstream branch.' This is basically telling you that because this is the first time you are using this branch, you need to set it up properly. Copy the command they give you and run that. It will look something like this:
git push --set-upstream origin ReadMe_Update
Step 7: Merge your branch with master
Now if we navigate back to our GitHub account and have a look at that repo, we can see a little banner that notifies us of the recent push we did.
Let's click that big green button and make a pull request! A pull request is how we merge two branches. When we create a pull request we can see the two branches we are merging, the changes that have been made, and we can add in some comments to explain those changes. Make sure to change your base repo to your own HelloWorld repo instead of the OctoCat one. It should look like this:
Once you've added in any comments you think are useful, go ahead and click create pull request.
Whohoo! We can merge these branches. There are no conflicts, which is fantastic, so click that merge pull request button and give yourself a high-five.
Step 8: Update your current branch with the latest changes
If we go back into our terminal now, we can checkout out our master branch and then pull down our recent changes. Do this by running the following commands:
git checkout master
This pulls down all our recent changes from our merge. Congratulations, you've just run through using git! You're a rock star.