Version control with Git — 1

Ron Yeh
Ron Yeh
Jul 22, 2017 · 4 min read

Git is a free and open source version control system. Learning how to use it is a lesson for every developer. There's a lot of articles and clips teaching you how to do it, I'm not going to completely introduce Git. Just providing a brife note I took when I learned to do it. So, if you're new to Git, too, follow the steps bellow and get confortable making any changes.

Open your Terminal on Mac

You can use Mac spotlight to find it.

Create directory and file

First, I'm going to the desktop. By typing the code below:

$ cd desktop

And then, I make a new directory named git-fun:

$ mkdir git-fun

Now you can see there’s a new folder shows on your desktop, we can do

$ cd git-fun
$ git init

to get into git-fun and initialize the directory. Always need to initialize it when you create it, and it can be used with git.

Now I create a new file named README.md in git-fun.

$ touch README.md

If you type

$ ls

it will show all the files in this directory, and now there is only README.md, which you just created.

$ vim README.md

We can edit README.md, and let’s add a line in the file like this:

press i can insert content, when you finish edit it, press ESC two times and press Shift + : and type X and press Enter button can save and leave the edit mode.

Add and Commit the file

By doing

$ git status 

it will show the message like this:

It means that git is not tracking this file, it will not go in your git repository unless you start tracking file. Now we need to save this file, the first thing we need to do is add this file by:

$ git add README.md

and then $ git status check what is change,

It become green, and says there is a new file README.md now. So now we can commit it. Any time you commit a file it’s like a time stamp, it save this version in your history. Anytime when you go back to see, it will like the way it did when you did that commit.

$ git commit -m "first commit"

Check $ git status , and you will see there is noting to commit, working directory is clean. We have now committed that change in history.

Command + K can clear the screen of terminal.

Edit README.md and Add A New File

Add a new line in the file. (Just trying to make some change, and we can use version control to recover the previous version.

$ vim README.md//Edit the file, here I add a new line
$ git status    //it will show modified: README.md$ touch code.js   //just for show the difference$ git add -A     //Add all the files at the same time$ git commit -m "modified README.md and added code.js file"$ls  //README.md   code.js

Git log and branch

Now we had edited our README.md file. BUT! If now you regret the change , you want to bring back the previous version which only one line in the file.

First,

$ git log

It will show all the commit you have done. And now you will know why the message is so important, it help you to know which one you are finding.

$ git checkout 951e1fa// 951e1fa is the first seven digits of that commit.

Now if you do $ ls , there is only one file README.md

Version control has now checked out the the previous version. Now we can put this version in another branch to save it.

$ git branch  // Show all the braches I have now $ git checkout -b readme-one-line  //name this new branch as readme-one-line$ ls    // README.md (one line version)$ git checkout master   //Switch to master branch$ ls    // README.md (two lines version)  code.js

And now I successfully have multiple versions of our code.

Notes: The more often you commit, the more safe you code can be. But don’t commit bug, and don’t commit trash. Always do $ git status , to make sure what you did.

Ron Yeh

Written by

Ron Yeh

Eager to make the world less boring.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade