Common operations and commands of git

git flowchart

insert image description here

git operation

git new branch

1. Switch to the base branch, such as the trunk

git checkout master

2. Create and switch to a new branch

git checkout -b martin

git branch can see that it is already on the panda branch

3. Update the branch code and submit

git add *

git commit -m "init martin"
# 推分支代码
git push origin martin

git commit code

1. View branch

git branch

2. Switch to the branch where the code is located

git checkout martin

3. View the modified files

git status

4. Pull the code on the branch

# 在提交代码之前需要先拉取代码,目的是为了保证你在提交文件后不回与别人提交的代码产生冲突,冲掉别人提交的代码。
git pull

5. Save the current working state to the git stack and restore it when needed

git stash

If an error is reported when pulling the code, it may be because the local code conflicts with the pulled code. At this time, the local code needs to be temporarily stored in the stack, and then git pull again. Order: after git stash, your workspace status is clear

6. Pop out the latest content in the stack and apply it to the current branch, and delete the records in the heap

# 将暂存在栈上的文件重新移到工作区中
git stash pop

7. Submit the code to the local git cache

git add . 或者 git add xxx
# 命令:git add 文件名1 文件名2 …

(1) If you check the current status with git status and find that all the files you have modified must be submitted, then you can use git add directly to add all your content to the local git cache

(2) If you check the current status with git status and find that there are some files that you don't want to submit, then use git add xxx to submit some files to the local git cache

8. Push the code to the local git repository: git commit

git commit 文件名 -m “提交代码备注”

9. Submit the local code to the remote warehouse

git push

10. If git does not submit the specified file,
check the modified files first.

git status

For example: vue.config.js, src/model/table.vue...
Requirement : Do not submit vue.config.js
command every time you submit code : git update-index --assume-unchanged vue.config.js
Example : git update-index --assume-unchanged.idea/workspace.xml

Before submitting, we use the command: git status to check whether there is such a file, if not, it means success, this habit must be maintained.

If you want to submit the file later, run the command:
git update-index --no-assume-unchanged vue.config.js

When we forget which files have been ignored, we can run the following command to find out:

git ls-files -v | grep '^h '
and then cancel the flag. If you need to cancel all flags without submitting files, you can run the following command:

git ls-files -v | grep ‘^h’ | awk ‘{print $2}’ |xargs git update-index --no-assume-u

The git sub branch code is submitted and merged into the master main branch

1. Submit part of the code modified by the sub branch

# 将所有代码提交
git add .
# 编写提交备注
git commit -m "修改bug"
# 提交代码至远程分支
git push origin dev

2. Switch the master branch

# 切换分支
git checkout master 
# 如果多人开发建议执行如下命令,拉取最新的代码
git pull origin master

3. merge branch code

git merge dev
# merge完成后可执行如下命令,查看是否有冲突
git status

Solve the problem of .idea workspace.xml appearing every time the code is submitted

Cause of the problem:
The reason is that Git ignores. When Git synchronizes the code, the premise of setting the local ignore file is to ensure that there is no such file to be ignored in the remote warehouse of Git. When the file is included on the remote side, the ignore set locally will no longer work.
Solution
1. Add the .idea/workspace.xml file in the local .gitignore file.
2. If the local file has been submitted to the remote end, the file submitted by the remote end needs to be deleted. The deletion command is:
git rm -r --cached .idea
3. Push code

Then use the git status command to view deleted files
insert image description here

When cutting branches, it prompts Please move or remove them before you switch branches.

insert image description here

It probably means that the untracked working tree files of the following files will be checked out and overwritten. You need to kill them when switching branches.
Solution

git clean -dfx

# git clean是把你当前工作的分支所有东西保持和远程的分支一致,就是说你远程是怎么样的,执行这个命令后你本地的就是怎么样的,没有进入版本的文件会直接扔掉

Push the local project to the new warehouse

# 链接远程仓库
git remote add origin git地址
# 查看链接是否成功
git remote -v
# 提交代码
git add .
git commit -m "vue"
# 推送代码并创建远程分支
git push --set-upstream origin main

Native code lost and retrieved

insert image description here
Select the file to be retrieved, click "Show History", find the code change of the corresponding version, and restore it

Guess you like

Origin blog.csdn.net/weixin_43587784/article/details/129178152