Common Git Commands

Global Git Settings

After installing Git (macOS users can install Xcode), you need to configure user information (username and email). This user information will be used for every Git commit.

Setting User Information

Open the terminal and enter the following commands:

1
git config --global user.name "YourUsername"

Example:

1
git config --global user.name "kxzhu"
1
git config --global user.email "YourEmail"

Example:

1
git config --global user.email "xxxxxx@qq.com"

Viewing Configuration Information

1
git config --list

Getting a Git Repository

To use Git for version control, you first need to obtain a Git repository. There are two ways to do this:

  1. Initialize a Local Repository (less commonly used)

    • Create an empty directory locally to act as your Git repository.

    • Navigate to the directory in the terminal and run the command:

      1
      git init

    After success, a hidden .git folder will be created in the directory. On macOS, press Command + Shift + . to view hidden files.

  2. Clone a Remote Repository (commonly used)

    • Navigate to a new directory in the terminal (do not perform this operation within an existing Git repository).

    • Go to your Gitee repository, click HTTPS, and copy the link.

    • Run the following command:

      1
      git clone [RemoteRepositoryURL]

      Example:

      1
      git clone https://gitee.com/kxzhu93/hellogit.git
    • Enter your Gitee username and password to complete the process.

    After success, the remote repository will be cloned into the new directory. The directory will contain a .git folder.

Working Directory, Staging Area, and Repository

  • Repository: Represented by the .git folder.

  • Working Directory: The directory containing the .git folder.

  • Staging Area: For example, create a new test.java file in the working directory (hellogit). Navigate to the hellogit folder in the terminal and run:

    1
    git add *

    This command moves changes to the staging area. An index file will appear in the .git folder, representing the staging area.

Afterwards, you can run:

1
git commit

to commit the changes to the repository.

State transitions:

  • git add (Working Directory -> Staging Area)
  • git commit (Staging Area -> Local Repository)

File Status

Git tracks changes (additions, deletions, updates) in the working directory. The file status changes as you execute Git commands.

View file status:

1
git status

Process:

  • A newly created file in the working directory is untracked.
  • Modifying an existing file makes it unstaged.
  • After running git add, the file is tracked and marked as:
    • unmodified: A tracked file with no changes.
    • modified: A tracked file with changes (red if not added to the staging area, green if added to the staging area).
    • staged: Added to the staging area.
  • After running git commit, the changes are committed to the repository.

Common Commands for Local Repositories

  1. git status: View file status.

  2. git add: Add changes to the staging area.

  3. git reset: Unstage files or switch to a specific version.

    • Unstage a file:

      1
      git reset test.xml

      This moves test.xml back to the untracked state.

    • Switch to a specific version:

      1
      git reset --hard [commitID]
  4. git commit: Commit changes to the repository.

    Example:

    1
    git commit -m "Comment" test.java
  5. git log: View the commit log.

Common Commands for Remote Repositories

  1. git remote: View remote repositories.

    • Use git remote -v for detailed information.
  2. git remote add: Add a remote repository.

    Syntax:

    1
    git remote add [ShortName] [URL]

    Example:

    1
    git remote add origin https://gitee.com/kxzhu93/git_test.git
  3. git clone: Clone a remote repository.

    Syntax:

    1
    git clone [URL]
  4. git pull: Pull updates from a remote repository.

    Syntax:

    1
    git pull [ShortName] [BranchName]

    Example:

    1
    git pull origin master
  5. git push: Push changes to a remote repository.

    Syntax:

    1
    git push [RemoteName] [BranchName]

    Example:

    1
    git push origin master

Branches

Introduction

Branches allow you to separate your work from the main development line to avoid interference. A repository can have multiple branches, which are independent of each other.

When creating a repository with git init, a default master branch is created.

Common Commands for Branches

  1. View branches:

    • git branch: List all local branches.
    • git branch -r: List all remote branches.
    • git branch -a: List all local and remote branches.
  2. Create a branch:

    1
    git branch [BranchName]
  3. Switch branches:

    1
    git checkout [BranchName]

    For example, git checkout b1, or git checkout master

  4. Push a branch to a remote repository:

    1
    git push [RemoteName] [BranchName]

    For example, git push origin b1

  5. Merge branches:

    1
    git merge [BranchName]

Merge the b1 and b2 branches into the master branch:

  • Switch back to the master branch:git checkout master
  • Merge b1: git merge b1
    • This will open an editor window. Type :wq to save and exit. (If editing is needed, type i to enter edit mode, make changes, then press ESC followed by :wq to save and exit.)
    • After pressing Enter, the b1 branch will be merged into master.
  • Merge b2: git merge b2
    • Similarly, this will open an editor window. Type :wq to save and exit.
    • After pressing Enter, the b2 branch will be merged into master.

Resolving Conflicts During a Branch Merge

If the same file is modified in both branches, a conflict may occur during the merge.

Steps to resolve conflicts:

  1. Manually resolve the conflict by editing the file at the indicated conflict markers.
  2. Use the following command to inform Git that the conflict has been resolved and the file is ready to be staged: git add [file_name]
  3. Commit the changes to the repository: git commit -m "Comment" [file_name]

If you encounter the error:

1
fatal: cannot do a partial commit during a merge

You can resolve it by using:

1
git commit -m "Comment" [file_name] -i
  1. Push the changes to the remote repository:
    1
    git push origin master

Tags

Tags are used to mark specific points in a repository’s history, often for released versions.

  1. List all tags:

    1
    git tag
  2. Create a tag:

    1
    git tag [TagName]

    For example, git tag v0.1

  3. Push a tag to a remote repository:

    1
    2
    3
    4
    5
    6
    7
    8
       git push [RemoteName] [TagName]
    ```
    For example, `git push origin v0.1`

    4. Check out a tag:

    ```bash
    git checkout -b [BranchName] [TagName]

    Example:

    1
    git checkout -b b0.1 v0.1