Git can be pretty scary. Especially for a person that’s new to coding like me. But the way to get better , no pun intended, is to get familiar with it. So let’s start off with what git is. It's a software that tracks changes in repos. And yes, a repo is short for repository. A repo consists of snapshots, each snapshot is called a commit. I create a repo called ‘getThanos’ in my github account. Then I clone it to my local computer by typing
git clone nameOfTheRepoGoesHere. The repo comes with its first commit named ‘main’.
I start working on my file getThanos.js. My first edit is to gather all the avengers. I like this edit so I add it to my staging area by typing
git add getThanos.js. The staging area is where you place your edits before making another commit of them. The staging area notifies git that there’s been some changes to the file.
So now the avengers have a plan to get thanos. But this is risky and dangerous, so I wanna be safe and commit now. I do this by typing
git commit -m 'gathered avengers and now have a plan'. The -m is for adding a message to my commit so that I know what edits that commit has. In this case, that commit has the avengers plan to get Thanos.
Now before going forward with the plan, I wanna make some sort of version to try out my plan, so that if something goes horribly wrong, I can go back. Well in git, they have branches, where you can do edits and it won’t affect the main branch. So I do this by typing
git branch sendOutSpiderman.
I created a branch, but I’m still on the ‘main’ branch. To do edits on the ‘sendOutSpiderman’ branch, I have to check it out with the command
git checkout sendOutSpiderman. So now I proceed with the plan. But spiderman runs into problems, he’s not focused since he wasn’t accepted into MIT. Okay so this plan isn’t gonna work, let’s go back to the main branch by typing
git checkout main. We have another plan to send out the Hulk. So we create another branch,
git checkout -b sendOutHulk. Combining ‘checkout’ with ‘-b’ is a shortcut to creating a branch and checking it out in one step.
Now we proceed with the Hulk. He couldn’t get Thanos, but there is some good in this edit, spiderman got accepted to MIT here. So I wanna save this edit and bring it into my main branch. To combine branches, git has a merge command. I save these edits and commit them,
git add getThanos.js then
git commit -m 'spiderman got into MIT'. Now I’m ready to combine them, so I checkout my main branch with
git checkout main then combine with
git merge sendOutHulk.
Git revert / git reset
Now let’s fast forward to the end, you know where Ironman snaps his fingers. Let’s say I’m too heartbroken by this and want to go back right before that happened. There’s two ways git allows this. One is reset and the other is revert. Reset will move my current branch main back a commit. Which they say is like “rewriting history”. But when sharing a branch with others, reset can confuse other users. Revert will move the current branch to a new commit. This new commit won’t have the edits made in the commit I didn’t want. So in the situation with Ironman, let’s say my commit before that was when Star-Lord got mad at Thanos for killing Gamora. Revert will make a new commit after my current commit with Ironman snapping his fingers, but this new commit will be a copy of the Star-Lord commit. And now I can rewrite the ending. In this way, my Ironman snapping his fingers is still in my history, and I can use it for my bloopers.