Git-Git common commands, common operations, creating mirrors & switching new warehouses, first clone, stash backup, viewing and switching users, modifying default branches, whether to keep local modifications, etc.

Git is often used in daily development, but sometimes we still forget it or encounter some strange problems. Use this record to remind yourself~updated from time to time~

1. First clone of new environment

Clone the repository and its modules for the first time

git clone --recursive [仓库URL]

The warehouse pulls the module for the first time

git submodule update --init --recursive

Update submodule

git submodule update --recursive --remote

2. Do you want to keep local modifications?

Save changes

git stash #封存修改
git pull origin master
git stash pop #把修改还原

Abandon changes == rollback

git reset --hard 
git pull origin master

3. Submit code

Add all files in the current directory to the staging area

git add .

Submit the staging area to the local warehouse

git commit -m "本次提交代码概要简述"

Check the current status of the project

git status

Pull the content on the dev branch to local

git pull origin dev

push code

git push

4. stash backup

Back up the content of the current workspace and read the relevant content from the most recent submission to ensure that the workspace is consistent with the content of the last submission. At the same time, save the current workspace content to the Git stack

git stash

Pull the current branch code on the server

git pull

Read the most recently saved content from the Git stack and restore workspace-related content. At the same time, the user may perform multiple stash operations, and it is necessary to ensure that the last stash is fetched first, so a stack (first in, last out) is used to manage it; pop takes the content on the top of the stack and restores it.

git stash pop

Displays all backups in the Git stack. You can use this list to decide where to restore from.

git stash list

Clear the Git stack

git stash clear

5. View and switch users

View username, github account name

git config user.name

Check your mailbox

git config user.email

Switch user

git config --global user.name “xxx”

Switch mailbox

git config --global user.email “xxx”

6. Create mirror & switch to new warehouse

Clone as image

git clone --mirror [仓库URL]

Go to the mirror folder

cd [repo folder]

Delete the associated origin remote library

git remote rm origin

After creating a new warehouse , update it to the new remote warehouse address

git remote add origin [新的URL] 

Push image

git push origin --all或git push origin --mirror

Push tags

git push --tags

Confirm that the branches and tags of the project on gitlab are consistent with the original ones.
After ensuring that the local backup is complete, you can clean up the local redundant remote branches.

git fetch -p

7. Modify the default branch

(1) After creating a new project on GitLab, enter the project and click Repository->Branches on the left

You can see that there is only one protected default branch, main, which cannot be deleted here.

Then create a new branch master: click New branch in the upper right corner, enter the branch name master, and Create branch.

(2) Click Settings->Repository on the left

Click on Default branch, select Default branch as master, and Save Changes.

At this point, the default branch is master.

(3) At this time, you can also return to the settings of (1) and delete the branch main.

Ps.Problem solved

Merge made by the ‘ort’ strategy.

Check the commit record and find the commit before the remote local conflict.
You can press "Q" to exit the git log state.

git log

Roll back to a certain version, determined based on comminID. Only the commit information was rolled back

git reset --soft [commitID]
git pull

fatal: Cannot do soft reset with paths.

HEAD & index & working copy are simultaneously reset to the commit you want to reset to. If executed, your local modifications may be lost.

git reset --hard

Recover git reset —hard misoperation

Find the commitId to be returned

 git reflog

and then execute again

 git reset —hard commitId

Reference:
https://www.mimastech.com/2017/11/19/how-to-movemigrate-a-full-git-repository-from-github-to-gitlab-self-hosted-instance/
https:// qa.1r1g.com/sf/ask/480571171/
https://blog.csdn.net/csdnlijingran/article/details/96425712

Guess you like

Origin blog.csdn.net/weixin_44436677/article/details/131020283