Common git operations
Time: October 30, 2023
08:50:21
Prefix: git configuration
-
First run
Git
. When runningGit
for the first time, you need to set user information. -
Configure global users
-
Configure the global user – that is, the user name you submitted in
git
( is used to identify the user – no other meaning) -
#Set the submitted user git config --global user.name "XXX" #Set the submission email git config --global user.email "[email protected]"
-
Note: This is not the user and email address of the
git
code warehouse, it is only used for development identification.
-
-
View current global configuration
-
git config --global --list
-
-
Configure ssh (optional)
-
What is the use of
git
settingssh key
? -
One of the main functions of git setting ssh key is to achieve passwordless login. By generating a public key and a private key, the public key can be added to the git server, thereby eliminating the need to enter a password every time a git operation is performed. This can greatly improve operational efficiency and reduce the tedious password input process. For example, it is often used during automated deployment.
-
How to configure?
-
First generate the SSH public key, using
gitee
as an example-
First enter the
git bash
console -
# Check whether a secret key has been generated cd ~/.ssh # If the following appears -- No such file or directory, it means that the secret key has not been generated. bash: cd: /c/Users/19839/.ssh: No such file or directory
-
# Create ssh public key ssh-keygen -t rsa -C "[email protected]"
-
-
Just
enter
three times in a row -
# View the public key content cat ~/.ssh/id_rsa.pub
-
-
Add public key in
gitee
-
-
-
1
1. Details of basic git commands
-
Basic command details of
git
-
Pull code repository
-
# Pull remote warehouse git cloneXXX.git # View branches git branch -a
-
Note that if ssh is added above, you can use the ssh download method
- 1
-
1
-
-
Tips for uploading git code
-
Create a code branch in the remote
git
repository and initialize it -
git clone xx
Create a directory locally and pull the remote repository -
Copy the code to the current directory
-
Upload the code to the remote warehouse
-
Use
idea
to open the directory, andcommint + push
-
Use
git bash
-
# Pull code repository git clonexxx.git #Add the copied files, all files in the current directory, and transfer them to the temporary storage area git add . # Add explanation git commit -m "<message>" # Show remote warehouse git remote -- origin # Push to remote warehouse git push origin master
-
If it is the first upload, you will be asked to enter your account and password. I have added ssh here and there is no need for user verification.
-
Go to the remote warehouse and check it. There are files to be uploaded now
-
1
-
-
-
2. How to use git in idea
-
Basic operations
-
Directly use
idea
to pull the remote warehouse code -
idea
Visualgit
common instructions- Pull code = code update, update the latest code of the current branch
- Submit to local library (
commit
)- Every time there is a small functional change, you can
commit
first, and then add comments to explain it.
- Every time there is a small functional change, you can
- Push to remote library (
push
)- Submit multiple
commit
to the remote warehouse at the same time
- Submit multiple
- View historical information, you can view the commit information of the branch
-
3. How to merge merge
For example, merge the
dev
branch into themaster
branch
-
First submit the
dev
branchcommit
to the cache, otherwise direct switching will prompt that it has not been submitted, and thenpush
submit it to the remote warehouse -
checkout + update
- Use
checkout
to switch branches, and save both branches as the latestupdate
- Use
-
Switch to the branch you want to merge – at this time we want to push the
dev
branch to themaster
branch, then switch to themaster
branch at this time That’s it- There is a branch switching conflict message because not all branch changes have been submitted, only part of them have just been submitted.
-
Then directly
merge
-
Finally,
master
branchpush
4. What should I do if a conflict occurs in merge
?
If multiple people modify this code during the merge process, merge conflicts will occur.
- Many people modified this code during the
merge
merger process. - You can see on this page that there are three options: either accept your recently modified
Accept Yours
, or accept other people’s previousAccept Theirs
, ormerge
- In most cases, we neither accept our own nor other people’s, we have to
Merge
- As you can see in the picture, we have a conflict to be resolved at the moment, and idea is asking who should be retained at this moment
- explain
Accept Left:
Select this button to fully accept the left side (discard our changes)Accept Right:
Select this button to accept the right side in full screen (retain the changes of the current branch)- In the middle is the style diagram of the final branch
- After the conflict resolution is completed, you can
merge
and there will be a prompt. - This is a conflict that has not been resolved. For example, if there are many conflicts and some of them are not resolved, then
apply
will pop up a prompt box. At this time, we continuecontinue
mark>
5. Tips
- To understand and become familiar with the native
git
native instructions, you can view the console after visualizing the operation inidea