Getting started with Git

Git basic concepts

Repository

This is where we store our code. On our local machine, a repository is a directory contain code.

Version control

To put it short, version control systems are tool to keep track of code changes. It helps users easily investigate, view the state of the whole repository at a specific commit.

Commits

When you make a commit, you create a snapshot of your repository. That means at any time, you can travel to that snapshot and view the code at that specific state.

Each commit is represented by a commit hash. In the following screenshot, the text starts with 94eece is a commit hash:

Branches

A branch contains a series of commit. All changes in a branch does not affect other branches.

Branches are usually created to:

  • Develop a new feature
  • Fix bugs
  • Try out new ideas

Create a new repository

To create a new Git repository simply open your terminal and type git init. A new empty repository will be created for you.

Create an empty git repository

If we issue this command

ls -la

We’ll see there is a new directory called .git was created.

new .git folder is created

That folder is not empty. If we travel into that folder, there are quite many files. Let’s do that by using the tree command (available on *nix systems)

Initial structure of the git folder

Daily Git Workflow

As a developer, your day to day work with Git mostly involves with these tasks:

  • Write code
  • Commit code
  • Push code to remote repository
  • Pull code from remote repository
  • Merge/rebase
  • Resolve conflicts

Let’s create some files and add to git.

Create and add files to git

Here are the commands in the screen above

# Create an empty file
touch first-file 

# show the current status of the git repository
git status

# add file to version control
git add first-file

# show the current status of the git repository
git status

If we issue a tree command on the .git directory, we’ll see there are some interesting changes now:

tree .git
the structure of the .git folder after new files created

We can see that there are new files/directories under .git/objects and .git/hooks

That’s the result of git version control system at work. We’ll dig into that in the next article.

Conclusion

In this article, we’ve learned how to create a new git repository, how to add file and make a commit and see the structure of the .git directory changed when new files are added. In the next post, we’ll learn about the Git internal structure.


Leave a Reply

Your email address will not be published.