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:
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, pressCommand + Shift + .
to view hidden files.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 thehellogit
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
git status
: View file status.git add
: Add changes to the staging area.git reset
: Unstage files or switch to a specific version.Unstage a file:
1
git reset test.xml
This moves
test.xml
back to theuntracked
state.Switch to a specific version:
1
git reset --hard [commitID]
git commit
: Commit changes to the repository.Example:
1
git commit -m "Comment" test.java
git log
: View the commit log.
Common Commands for Remote Repositories
git remote
: View remote repositories.- Use
git remote -v
for detailed information.
- Use
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
git clone
: Clone a remote repository.Syntax:
1
git clone [URL]
git pull
: Pull updates from a remote repository.Syntax:
1
git pull [ShortName] [BranchName]
Example:
1
git pull origin master
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
View branches:
git branch
: List all local branches.git branch -r
: List all remote branches.git branch -a
: List all local and remote branches.
Create a branch:
1
git branch [BranchName]
Switch branches:
1
git checkout [BranchName]
For example,
git checkout b1
, orgit checkout master
Push a branch to a remote repository:
1
git push [RemoteName] [BranchName]
For example,
git push origin b1
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:
- Manually resolve the conflict by editing the file at the indicated conflict markers.
- 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]
- 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 |
- 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.
List all tags:
1
git tag
Create a tag:
1
git tag [TagName]
For example,
git tag v0.1
Push a tag to a remote repository:
1
2
3
4
5
6
7
8git 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