Updated on 2024-07-05 GMT+08:00

Using the Git Client

Background

Before using the Git client, you need to understand the workflow and master basic operations, such as installing Git, creating and cloning repositories, adding, committing, and pushing changes, creating, updating, and merging branches, creating tags, and replacing local changes.

Prerequisites

The Git client has been installed.

Usage Process

The following figure shows the basic process of using the Git client.

Table 1 Procedure

Procedure

Description

Install the Git client

Install the Git client for your operating system.

  • Git for Windows
  • Git for macOS X
  • Git for Linux

Create a repository

Create and open a new folder, and run the following command:

git init

A Git repository is created.

Clone a repository

Run the following command to create a clone of a local repository:

git clone /path/to/repository

If the repository is on a remote server, run the following command:

git clone username@host:/path/to/repository

Local repository structure

There are three components in a local repository: working directory, index, and HEAD.

  • Working directory contains the files that you are working on.
  • Index caches changes you have made.
  • HEAD points to the latest commit.

Add and commit changes

Run the following command to add the changes to the index:

git add <filename>
git add *

Run the following command to commit the changes:

git commit -m "Code submission information"

The changes are committed to the HEAD but not to the remote repository.

Push changes

The changes are in the HEAD of the local repository. Run the following command to push the changes to the remote repository:

git push origin master

You can replace master with any other branch to be pushed.

If you have not cloned an existing repository, run the following command to connect the local repository to a remote server before the push:

git remote add origin <server>

Then push the changes to the added server.

Create a branch

Branches enable you to develop features separately. When a repository is created, the master branch is the main branch by default. Develop features on other branches and then merge them to the main branch after the development.

  1. Create a branch named feature_x and check out the branch.
    git checkout -b feature_x
  2. Check out the main branch.
    git checkout master
  3. Push the main branch to the remote repository. (If the branch is not pushed, the branch can be seen only in your local repository.)
    git push origin <branch>
  4. Delete the created branch.
    git branch -d feature_x

Update and merge branches

  1. Run the following command to update the local repository to the latest remote commits:
    git pull

    The remote changes are fetched and merged to your working directory.

  2. Run the following command to merge other branches to the current branch (for example, the master branch):
    git merge <branch>
    NOTE:

    Automatic merges may fail and conflicts occur. In this case, you need to modify these files to manually merge the conflicts.

  3. After the modification, run the following command to add your changes.
    git add <filename>
  4. Before the modification, you can run the following command to compare the source and target branches.
    git diff <source_branch> <target_branch>

Create a tag

You are advised to create tags for releases. For example, run the following command to create a tag named 1.0.0:

git tag 1.0.0 1b2e1d63ff

1b2e1d63ff is the first 10 characters of the commit ID to be tagged. Run the following command to obtain the commit ID:

git log

You can enter the first several characters of the commit ID as long as it can distinguish the commit from others.

Replace local changes

Run the following command to replace the unwanted local changes:

git checkout -- <filename>

The files in the working directory are replaced by the latest content in the HEAD. Changes added to the index and new files are not affected.

To discard all local changes and commits, fetch the latest commit from the server and reset the local main branch to the commit.

git fetch origin
git reset --hard origin/master