LabHub

Blog

Git Basics

한국어English日本語

Git Basics

Git is a Distributed Version Control System (DVCS) used in most projects. This article introduces the basic usage of Git.

Getting a Git Repository

There are two ways to get a Git project. You can either import an existing project or directory into Git, or clone an existing Git repository from another server.

Initializing a Repository in an Existing Directory

To start tracking an existing project with Git, navigate to the project directory and enter the following command.

$ git init

This command creates a new subdirectory called .git that contains all the necessary repository files. At this point, nothing in the project is being tracked yet.

To start tracking files, use the git add command to prepare an initial commit as follows.

$ git add .
$ git commit -m 'initial commit'

Cloning an Existing Repository

If you want to get a copy of an existing Git repository, use the git clone command. For example, to clone a project called libgit2, enter the following.

$ git clone https://github.com/libgit2/libgit2

This command creates the libgit2 directory, initializes the .git directory, downloads all the data, and checks out a working copy of the latest version.

Recording Changes

Once you have a Git repository and a working copy of the project files, you can make changes and commit each snapshot to the repository.

Checking File Status

You can check the status of files using the git status command. For example, to check the status after adding a new file README, do the following.

$ echo 'My Project' > README
$ git status

Tracking New Files

To track new files, use the git add command.

$ git add README
$ git status

Now the README file is being tracked and is ready to be committed.

Staging Modified Files

To stage modified files, use the git add command again.

$ git add benchmarks.rb
$ git status

Committing Files

Once the staging area is set up, you can commit your changes.

$ git commit -m "initial project version"

Skipping the Staging Area

If you want to skip the staging area, use the -a option to automatically stage all modified files and commit them.

$ git commit -a -m 'added new benchmarks'

Removing Files

To remove a file from Git, use the git rm command.

$ git rm grit.gemspec
$ git commit -m "remove grit.gemspec"

Moving Files

To move a file, use the git mv command.

$ git mv file_from file_to
$ git status

Viewing Commit History

You can view the commit history using the git log command.

$ git log

You can adjust the output format using various options.

$ git log --pretty=format:"%h - %an, %ar : %s"
$ git log --graph --oneline

We have covered the basic usage of Git. You can use these commands to effectively manage Git.

Quiz

Q1: What is the main topic covered in "Git Basics"? Learn the basic usage of Git.

Q2: What is Getting a Git Repository? There are two ways to get a Git project. You can either import an existing project or directory into Git, or clone an existing Git repository from another server.

Q3: Explain the core concept of Recording Changes. Once you have a Git repository and a working copy of the project files, you can make changes and commit each snapshot to the repository. Checking File Status You can check the status of files using the git status command.

Q4: What are the key aspects of Removing Files? To remove a file from Git, use the git rm command.

Q5: How does Moving Files work? To move a file, use the git mv command.

Comments

No comments yet.

Sign in to leave a comment