- Git command line usage guide
Git command line usage guide
Git is a powerful distributed version control system used to track and manage changes to software projects. This guide will detail the basic usage of Git to help you get started using Git to manage your projects.
Part One: Configuring Git
1.1 Set user information
Before you start using Git, you need to configure your user information, including name and email address. This will be used to record your submission information.
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
1.2 Configure line break processing
Newline characters are represented differently on different operating systems. To ensure consistency across platforms, you can configure how Git handles newlines.
git config --global core.autocrlf <value>
<value>
The options for are true
, false
, or input
, depending on your needs. Typically, use true
on Windows and false
or input
on Unix/Linux.
Part 2: Create and configure the warehouse
2.1 Initialize the warehouse
To start using Git, you need to create a new repository or clone an existing repository. Initialize a new repository using the following command:
git init my-repo
This will create a new repository named my-repo
in the current directory.
2.2 Clone the repository
If you want to clone an existing repository, you can use the following command:
git clone <repository-url>
This will copy the contents of the remote repository to your local computer and create a local copy that is identical to the remote repository.
2.3 Recursive cloning
Some repositories contain submodules, which are nests of other repositories. To recursively clone a repository containing submodules, use the --recursive
option:
git clone --recursive <repository-url>
This will clone the main repository and all its submodules.
2.4 Deep cloning
Deep cloning refers to cloning only part of the history of the repository, rather than completely cloning the entire history locally. This is useful for reducing warehouse size and saving bandwidth.
To deep clone, use the --depth
option and specify the number of commits to clone:
git clone --depth=<depth> <repository-url>
If you only want the latest content, use git clone --depth=1 <repository-url>
.
If you have cloned a repository using deep cloning and want to get more history, you can use the following command:
git fetch --unshallow
This will get the complete history from the remote repository.
Part 3: Basic Operations
3.1 Add files
In Git, you need to explicitly tell Git which files should be tracked. Use the following command to add the file to the staging area:
git add .
.
Represents all files in the current directory. You can also add a single file by specifying a specific file name.
3.2 Commit changes
Once you have added the files to the staging area, you can use the following command to commit the changes:
git commit -m "描述你的更改"
This will create a commit that saves your changes to the Git repository, along with a description.
3.3 View status and submission history
To view the status of the repository and its commit history, you can use the following command:
git status
git log
git status
shows uncommitted changes, while git log
shows commit history.
3.4 Create and switch branches
Branching is one of the powerful features of Git, allowing you to develop new features or fix bugs without affecting the main branch. To create and switch branches, use the following commands:
git branch <branch-name>
git checkout <branch-name>
<branch-name>
is the name you gave the branch.
3.5 Conflict resolution
When multiple people collaborate or switch branches, conflicts may be encountered. Conflicts occur when two branches modify the same portion of a file. To resolve a conflict, open the conflict file, manually edit the file to preserve the changes you need, and then mark the conflict resolved using the following command:
git add <resolved-file>
git commit -m "解决冲突:描述冲突解决的内容"
3.6 Merge branches
To merge a branch to master or another branch, use the following command:
git merge <branch-name>
This will merge the changes from the specified branch into the current branch.
3.7 Pull remote changes
In collaboration, you need to pull other people's changes to the repository to stay in sync. Use the following command to pull remote changes:
git fetch --all
3.8 Create local branches to track remote branches
To create a branch locally and associate it with a remote branch, use the following command:
git checkout -b 本地分支名 origin/远程分支名
This will create a local branch and associate it with the specified remote branch in order to track changes to the remote repository.
3.9 Add submodules
Git submodules allow you to make one Git repository a subdirectory of another Git repository. To add a submodule, you can use the following command:
git submodule add <repository-url> <path>
This will add a submodule in the specified <path>
directory, associated with the specified <repository-url>
repository.
Part 4: Remote warehouse operations
4.1 Add remote warehouse
To interact with a remote repository, you need to add it as a remote source for your Git repository. Use the following command to add a remote repository:
git remote add origin <repository-url>
origin
is the default name typically used for remote repositories, but you can choose another name.
4.2 Push changes to the remote warehouse
To push local changes to the remote repository, use the following command:
git push origin <branch-name>
This will ensure your changes are in sync with the remote repository.
Part 5: Undo changes
5.1 Undo changes
Sometimes, you may need to undo a commit or change. Different levels of undo can be achieved using the following commands:
git reset
Used to cancel the commit and keep the changes in the workspace.git reset HEAD~1
Used to cancel the latest commit and keep the changes in the workspace and staging area.
These are the basic uses of Git, but Git is a very powerful tool with many advanced features and options to explore. Continuing to learn and master Git will make you better
For more information, please refer to: https://git-scm.com/docs