Technology Sharing

Basic operations of git and gitee

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

Table of contents

Common git commands

1. Initialize the workspace (under a certain file path)

2. View the code file status of the current workspace

3. Submit the code files in the workspace to the temporary storage area

4. Submit the code files in the temporary storage area to the local warehouse

5. Compare the difference between the working area and the temporary storage area files

6. Comparison of differences between the staging area and the local warehouse

7. Comparison of differences between workspace and local warehouse

8. Version rollback (change the code (file content) of the workspace, staging area, and local repository)

9. View the commit log

10. Generate branches

11. View all branches

12. Switch branches

13. Merge branches

14. Delete a branch

Use Git to connect to gitee

1. Connect

2. Pull the code from the repository

3. Push

4. View the connected remote warehouse

5. Delete the connection to the remote warehouse

6. Clone and create a new folder without git init initialization

Push the code on idea to the gitee remote repository

Step 1: Create a new repository on gitee

Step 2: Create a new project in idea and let the project generate a git local repository

Step 3: Write code and add it to the staging area and commit it to the local repository

Step 4: Remotely connect to the warehouse

​Edit

Clone a project from a remote repository

​Edit Notes on Pulling Projects

1. Need to modify the address of Maven

2. Need to modify the SDK version

​Edit 3. Modify the coded version

Use idea to switch branches

We want to create a new branch in the gitee repository now

​Edit Then switch the branch of this remote warehouse in idea

​Edit Writing new code

Then add and submit, and finally push to the remote warehouse


Git is divided into work area, temporary storage area, local warehouse, local warehouse and temporary storage area are collectively called version library

Common git commands

Click Git Bash Here to enter the command line window

1. Initialize the workspace (under a certain file path)

git init;

2. View the code file status of the current workspace

git status; 

3. Submit the code files in the workspace to the temporary storage area

git add filename

git add ./ --> Submit all files in the current directory to the temporary storage area

How to know that the file is submitted to the temporary storage area

Using git status

Green means it has been successfully submitted to the staging area, and red means it is still in the working area

4. Submit the code files in the temporary storage area to the local warehouse

git commit -m "commit information"

If it is the first submission

You need to submit your email and username

git config --global user.email "[email protected]"
 git config --global user.name "Your Name"

5. Compare the difference between the working area and the temporary storage area files

git diff read.txt(filename)

Green content is different content

6. Comparison of differences between the staging area and the local warehouse

git diff --cached read.txt

No content displayed means the content is the same

7. Comparison of differences between workspace and local warehouse

git diff HEAD read.txt

8. Version rollback(Change the code (file content) in the workspace, staging area, and local repository)

1. Roll back to the previous version

        git reset --hard HEAD^

2. Roll back to the specified version

git reset --hard version number

9. View the commit log

git reflog / git log

The red box indicates the current version number. If we want to roll back to the previous version

Roll back to the specified version, using the version number

10. Generate branches

git branch branch name

11. View all branches

git branch

12. Switch branches

git checkout branch name

Add new content to read.txt of the h1 branch and submit it to the local repository. When you switch back to the master branch, you can find that the newly added content is not displayed. This is because the h1 branch has not been merged into the master branch.

13. Merge branches

If you want to merge other branches into the main branch, you need to switch to the main branch first.

Note: If you want to merge a branch into the main branch, you need to commit the branch content to the local repository first.

git merge branch name

 

14. Delete a branch

git branch -d branch name


Use Git to connect to gitee

1. Connect

git remote add origin (connection name) https://gitee.com/-ss_0/hhh1.git (warehouse address)

2. Pull the code from the repository

git pull origin master --allow-unrelated-histories (first time)

git pull subsequent pull

3. Push

git push -u origin master (first time)

git push Subsequent push

Error:
1. ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://gitee.com/luosu-ss_0/hhh1.git'
Type: git pull --rebase origin master

2.fatal: unable to access 'https://gitee.com/luosu-ss_0/hhh1.git/': Could not resolve host: gitee.com

Cause: Network problem

4. View the connected remote warehouse

git remote -v

5. Delete the connection to the remote warehouse

git remote rm origin

6. Clone and create a new folder without git init initialization

git clone repository address

Note: The difference between pulling and cloning

They all get code from remote repositories

difference:

1.git clone: ​​When there is no local version library, the entire version library is cloned from the remote server to the local, which is a process of creating a local version library from scratch.

2.git pull: If there is a local version library, get the latest commit data (if any) from the remote server and merge it to the local

Push the code on idea to the gitee remote repository

Step 1: Create a new repository on gitee

 

Step 2: Create a new project in idea and let the project generate a git local repository

Step 3: Write code and add it to the staging area and commit it to the local repository

 

Step 4: Remotely connect to the warehouse

 

Note: Pull the files from the remote repository first

Enter in the idea terminal

git pull origin master --allow-unrelated-histories 

Then you can push it

Push Success

 

Clone a project from a remote repository

Cloning success

Notes on pulling projects

1. Need to modify the address of Maven

2. Need to modify the SDK version

3. Modify the coding version

Note: If the push fails, pull the code from the remote repository first.

Use idea to switch branches

We want to create a new branch in the gitee repository now

Then switch the branch of this remote warehouse on idea

Writing new code

Then add and submit, and finally push to the remote warehouse

 

We can find that the contents of the master branch and the dev1 branch are different. This is because the contents of the dev1 branch have not been merged into the master branch.

So let's switch to the master branch in idea.

Merger Success

 

Then you can push it