Skip to content

Instantly share code, notes, and snippets.

@RJ722
Last active July 21, 2017 09:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RJ722/a9602ef70567ca65ce0ba698747c9fed to your computer and use it in GitHub Desktop.
Save RJ722/a9602ef70567ca65ce0ba698747c9fed to your computer and use it in GitHub Desktop.
The very basics of git.

Initialisation

Git needs to know what content does it needs to track (which directory), therefore we initialise them as a git repository by the command:

cd hello-world/
git init

It will output something like:

Initialized empty Git repository in /Users/rahuljha/Documents/hello-world/.git/

Now, git has started tracking the contents of the hello-world folder. Check the status next.

git status

This will show up something like:

On branch master


Initial commit


nothing to commit (create/copy files and use "git add" to track)

The status command comes in handy, when we need to have an overview on what all has changed, and what changes are gonna be commited.

Commits

commits are the atomic unit of changes, i.e. changes are recorded in certain chunks known as commits along with a helpful message the commit author has given to describe the change

Let's add a file, make some changes to it and commit it.

cat "Hello World, my name is Rahul Jha" > hello_world.txt # This writes the given text in a file called example.txt.
git status # Check the current status
git add hello_world.txt # Add the files, changes of which are to be committed.
git commit

And then simple supply a helpful commit message.

Tip: Commit messages are generally less than 50 characters and are always written in imperative mood. For ex. for the above change, the correct commit message would be: Add hello world file having my name. Some examples of wrong commit messages:

  • Added hello world file having my name
  • hello world file having my name is added
  • hello world file having my names has been added
  • It adds a hello world file having my name
  • Hurray, we have created our first commit.

Log

You may use the command git log to view the lists of all commits.

Task for you

Create a hello_world program in C . Name the file: hello_c.c Commit the results and tell us what commit message you used here at slack.

Run the log command to view both your commits.

Checking out older versions of code.

Now, since we are learning "version" control. It would be pretty useless if we couldn't go back to prior versions of our code -That was the whole point. So, to check out older versions of code, git has a command: git checkout

Now, how would git know which version of the code you need to check out. Therefore commit id of the commit you want to checkout to is passed in to checkout. You can view the commit ID by running the log command (git log).

NOTE: A Git commit ID is a SHA-1 hash of every important thing about the commit.

So, let's say that commit ID to our first commit is: 3dd55bcf4d45c559f3b2c97a4813527292a29b79

Now, if we want to checkout the first commit, we would run the command:

git checkout 3dd55bcf4d45c559f3b2c97a4813527292a29b79

Actually, the first four letters would do the same, therefore the following command would also have the exact same effect. git checkout 3dd5

Both of these commands might show some warning like:

Note: checking out '3dd5'.


You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.


If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:


  git checkout -b <new-branch-name>


HEAD is now at 3dd55bc... Add hello_world file with my name

This just means that the changes (commits) you do now won't be saved until you switch to another branch (More on branches later)

Note: We will have a task on checkout after the github workflow session.

Hopefully, you now know how to use the stupid content tracking system and are super excited to learn even more. You would be surprised to know that we have just touched the surface of the git - It's a lot more powerful and fun to work with.

See you in the next session! :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment