1. Introduction of Git
Git is an open source distributed version control system designed for agile and efficient handling of any project, large or small
Git has four zones: workspace, staging, local repository, and remote repository
- -Vanessa: Hey, you’re writing code in a Workspace
- Index: Temporary storage area for temporary changes
- Repository: a local Repository that holds all version data, with HEAD pointing to the latest version
- We can set up Remote repositories on some sites (GitHub, Gitee, etc.) to host our code
The working process
Git works like this:
1. Write in the workspace and modify the code
2. Run the git add command to add the files to the staging area
3. Run the git commit command to save the files in the staging area to the local repository. A Git commit record is created
4. Run the git push command to push the version of the local repository to the remote repository
Therefore, Git managed files generally have three states: Modified, staged, and Committed
2. Basic commands
1. Workspace command
-
Initialize the warehouse
git init Copy the code
-
Add File Tracking
You can save a file to the staging area by using git add
git add <fileName> Copy the code
You can also use the git add. command to save all files in your workspace to staging
git add . Copy the code
-
View workspace file status
Git status allows you to view the status of files in the current workspace
-
Cancel the submission
You can undo files stored in the staging area by using the git reset command
Git reset <fileName> // Git resetCopy the code
2. Staging area command
-
Submit files from the staging area to the local repository
git commit -m <commitLog> Copy the code
-
Undo the last commit
// Undo the commit to the staging area git reset --soft HEAD^Copy the code
-
Version rollback
git reset --hard <logName> Copy the code
3. Run the local warehouse command
-
View all remote warehouses
git remote -v Copy the code
-
Add a remote repository
git remote add <remoteName> <remoteUrl> Copy the code
-
Delete a remote repository
git remote rm <remoteName> Copy the code
-
Modify a remote repository name
git remote rename <oldName> <newName> Copy the code
-
Push local repository code to remote repository
git push <remoteNmae> <branchName> Copy the code
You can run the -f(–force) command to force the push
git push -f <remoteNmae> <branchName> Copy the code
You can add the -u command to track a branch by default
git push -u <remoteNmae> <branchName> Copy the code
-
Clone a repository locally
git clone <remoteUrl> Copy the code
-
Update remote repository code to local repository
git fetch Copy the code
-
Update the remote repository code to the local repository and merge it into the workspace
git pull(git fetch + git merge) Copy the code
3. Branch commands
-
Viewing local branches
git branch Copy the code
-
Viewing remote branches
git branch -r Copy the code
-
View all branches
git branch -a Copy the code
-
Create a new branch
git branch <branchName> Copy the code
-
Rename branches
git branch -m oldName newName Copy the code
-
Switch to the specified branch
git checkout <branchName> Copy the code
-
Create and switch to the branch
git checkout -b <branchName> Copy the code
-
Delete a local branch (cannot be under this branch)
git branch -d <branchName> Copy the code
-
Deleting a Remote Branch
git push -d <remoteName> <branchName> Copy the code
-
Merging branches
git merge <branchName> Copy the code
-
Branch rebase
git rebase <branchName> Copy the code
1. Branch to base
Branch rebasing can change the starting point of the current branch to the end point of the target branch. It is generally used for merging code from the same branch
- rebase
git rebase <branchName>
Copy the code
- Rebase while pulling remote code (note in command
/
)
git pull --rebase <remoteName>/<branchName>(git fetch + git rebase <remoteName>/<branchName>)
Copy the code
Conflict resolution
- Abandons the merge, reverts to the state before the rebase operation, and previous commits are not discarded
git rebase --abort
Copy the code
- A. Discarded commits That caused conflict
git rebase --skip
Copy the code
-
Resolve the conflict
If a conflict occurs while pulling code, run git add and git commit to commit the conflicting files, and then run git rebase –continue to merge the branches
git rebase --continue
Copy the code