Tip: After the article is written, the table of contents can be automatically generated. For how to generate it, please refer to the help document on the right.
Article directory
Preface
Git is an open source distributed version control system that can effectively and quickly handle version management of projects from small to large.
1. Git working mechanism and hosting center
1.1 Working mechanism
Historical version→local library
↑
git commmit
↑
Temporary storage→temporary storage area
↑
git add
↑
Write code → workspace
1.2 Hosting center
The code hosting center is a remote code warehouse based on the network server. Generally, we simply call it the remote library.
- local area network
GitLab
- internet
GitHub (foreign website)
Gitee code cloud (domestic website)
2. Git common commands
命令名称 |
作用 |
---|---|
git config --global user.name 用户名 |
设置用户签名 |
git config --global user.email 邮箱 |
设置用户签名 |
git config --global core.quotepath false |
设置显示中文文件 |
git init |
初始化本地库 |
git status |
查看本地库状态 |
git add 文件名 |
添加到暂存区 |
git commit -m "日志信息" 文件名 |
提交到本地库 |
git reflog 或 git log(更详细) |
查看历史记录 |
git reset --hard 版本号 |
版本穿梭 |
2.1 Set user signature
admin@DESKTOP-E8GCEEI MINGW64 ~
$ git --version //查看版本号
git version 2.35.1.windows.2
admin@DESKTOP-E8GCEEI MINGW64 ~
$ git config --global user.name tang //设置用户签名
admin@DESKTOP-E8GCEEI MINGW64 ~
$ git config --global user.email 2834323411@qq.com //设置用户签名
admin@DESKTOP-E8GCEEI MINGW64 ~
$ git config --global core.quotepath false //设置显示中文文件
Note: Setting the user signature here has nothing to do with the account you will use to log in to GitHub (or other code hosting centers) in the future.
- Check whether the setting is successful
Find the .gitconfig file in C:\Users\admin and open it
2.2 Initialize local library
admin@DESKTOP-E8GCEEI MINGW64 ~/Desktop/my demo/git_demo
$ git init
After initialization, a hidden file .git will be automatically generated in the current folder.
2.3 View local library status
admin@DESKTOP-E8GCEEI MINGW64 ~/Desktop/my demo/git_demo (master)
$ git status
On branch master
nothing to commit, working tree clean
2.4 Add to cache
admin@DESKTOP-E8GCEEI MINGW64 ~/Desktop/my demo/git_demo (master|MERGING)
$ git add hello.txt
2.5 Submit to local library
git commit -m "日志信息" 文件名
You can also see the difference through its git status ,
and you can also see the version update by looking at the log.
admin@DESKTOP-E8GCEEI MINGW64 ~/Desktop/my demo/git_demo (master|MERGING)
$ git commit -m "merge"
[master f556a5b] merge
2.6 Historical versions
git reflog
View version information
git log
View version details
admin@DESKTOP-1SHNHA2 MINGW64 /e/Git (master)
$ git reflog
3f47d64 (HEAD -> master, origin/master, git/master) HEAD@{
0}: commit: git笔记1.1
29410ad HEAD@{
1}: commit: git笔记1.0
f73d1ab HEAD@{
2}: commit: 完善版
df5acc9 HEAD@{
3}: reset: moving to df5acc9
20952cf HEAD@{
4}: reset: moving to 2095
df5acc9 HEAD@{
5}: commit: git笔记1.0
d930448 HEAD@{
6}: commit: 进行了第四次修改
24de0b6 HEAD@{
7}: commit: 第二次修改有误,返回上次版本
435af33 HEAD@{
8}: commit: 进行了第二次修改
20952cf HEAD@{
9}: commit (initial): 进行了一次修改
admin@DESKTOP-1SHNHA2 MINGW64 /e/Git (master)
$ git log
commit 3f47d64f23916a165bc218a4bf47ccbe5540bcf5 (HEAD -> master, origin/master, git/master)
Author: tang <2834323411@qq.com>
Date: Sat Sep 3 13:10:14 2022 +0800
git笔记1.1
commit 29410ad24169242a841511e3299560203b4cccec
Author: tang <2834323411@qq.com>
Date: Sat Sep 3 12:47:05 2022 +0800
git笔记1.0
2.6.1 Version shuttle
git reset --hard version number
admin@DESKTOP-1SHNHA2 MINGW64 /e/应用21301唐辉阳/Git (master)
$ git reflog
bf3c4a7 (HEAD -> master) HEAD@{
0}: commit: git笔记1.0
f73d1ab HEAD@{
1}: reset: moving to HEAD^
29410ad HEAD@{
2}: reset: moving to 29410ad
3f47d64 (origin/master, git/master) HEAD@{
3}: commit: 唐辉阳的git笔记
29410ad HEAD@{
4}: commit: git笔记
f73d1ab HEAD@{
5}: commit: 完善版
df5acc9 HEAD@{
6}: reset: moving to df5acc9
20952cf HEAD@{
7}: reset: moving to 2095
df5acc9 HEAD@{
8}: commit: git笔记
d930448 HEAD@{
9}: commit: 进行了第四次修改
24de0b6 HEAD@{
10}: commit: 第二次修改有误,返回上次版本
435af33 HEAD@{
11}: commit: 进行了第二次修改
20952cf HEAD@{
12}: commit (initial): 进行了一次修改
admin@DESKTOP-1SHNHA2 MINGW64 /e/应用21301唐辉阳/Git (master)
$ git reset --hard 3f47
HEAD is now at 3f47d64 唐辉阳的git笔记
admin@DESKTOP-1SHNHA2 MINGW64 /e/应用21301唐辉阳/Git (master)
$ git reset --hard bf3c
HEAD is now at bf3c4a7 git笔记1.0
2.7 Git common command cheat sheet
3. Git branch operations
In the version control process, multiple tasks are promoted at the same time. For each task, we can create a separate branch for each task. Using branches means that programmers can separate their work from the main development line. When developing their own branches, it will not affect the operation of the main line branches.
advantage
- Promote the development of multiple functions in parallel at the same time to improve development efficiency
- During the development process of each branch, if the development of one branch fails, it will not have any impact on other branches. Just delete the failed branch and start again.
Common branch commands
Command name | effect |
---|---|
git branch branch name | Create a branch |
git branch -d branch name | delete branch |
git branch -v | View branches |
git branch -r | View remote library branch |
git checkout branch name | switch branch |
git merge branch name | Merge the specified branch into the current branch |
Branch merge
When there is a code conflict, change the file first, and then use the command (without the file name)
4. GitHub operations
GitHub URL
- Create a remote warehouse
- Enter warehouse name
Remote warehouse command
Command name | effect |
---|---|
git remote -v | View all current remote address aliases |
git remote add alias remote address | Give an alias |
git push alias branch | Push content on the local branch to the remote repository |
git diff |
View conflict files |
git clone remote address | Clone the contents of the remote repository to the local |
git pull remote library address alias remote branch name | Pull down the latest content of the branch from the remote warehouse and merge it directly with the current local branch. |
View and create aliases
admin@DESKTOP-E8GCEEI MINGW64 ~/Desktop/my demo/git_demo (master)
$ git remote -v //查看别名
admin@DESKTOP-E8GCEEI MINGW64 ~/Desktop/my demo/git_demo (master)
$ git remote add git-demo https://github.com/Tang17261828134/git_demo.git
//创建别名
admin@DESKTOP-E8GCEEI MINGW64 ~/Desktop/my demo/git_demo (master)
$ git remote -v
git-demo https://github.com/Tang17261828134/git_demo.git (fetch)
git-demo https://github.com/Tang17261828134/git_demo.git (push)
admin@DESKTOP-E8GCEEI MINGW64 ~/Desktop/my demo/git_demo (master)
$ git remote remove git-demo //删除别名
After you create an alias,
you can upload files through the alias.
To collaborate within the team
, you can invite the account through the manager access settings in GitHub.
Then the invitee can agree by copying the link.
The invited person can clone the clone code and then edit and upload the model to change the code.
Cross-team cooperation
. Fork the code and make it your own. After modification, send it through a pull request.
If successful, it will become your modification.
SSH password-free login
- Enter Git Bash here
- input the command:
ssh-keygen -t rsa
- Find id_rsa.pub in C:\Users\admin\.ssh and copy all the contents inside.
- Log in to the Gitee website, click Settings 1 > SSH Public Key 1 > Paste the content you just copied in Add Public Key.
Note: If you use ssh to log in without a password, the link behind the alias can only be http, not https.
5. IDEA integrates Git
5.1 Configure Git to ignore files
First set up a ignore file
that has nothing to do with the actual function of the project and does not participate in the deployment and operation on the server.
Set a git.ignore
content file as
# Compiled class file
*.class
# Log file
*.log
# BlueJ files
*.ctxt
# Mobile Tools for Java (J2ME)
.mtj.tmp/
# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar
# virtual machine crash logs, see
http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
.classpath
.project
.settings
target
.idea
*.iml
References in .gitconfig files ignore configuration files
[user]
name = manong
email = yanjiuseng
[core]
excludesfile = C:/目录/git.ignore
5.2 Locating Git programs
5.3 Initialize local library
5.4 Add to staging area
Files that have been successfully added are green , and files that have not been added to the cache are red.
5.5 Submit to local library
5.6 Switch version
5.6.1 View version information
5.6.2 Switch version
5.7 Create a branch
There are two ways to create a branch
5.8 Switch branches
5.9 Merge branches
5.10 Conflict resolution
Summarize
This time I learned a lot by learning git and learned a lot of knowledge that I didn’t know. Git is a distributed management tool that maintains data integrity at all times through fingerprint strings. It is concerned with the overall changes in file data and does not save the difference data before and after changes; Git saves relevant projects on the local disk. For historical updates, most operations only require access to local file resources and do not require the Internet. Of course, you can use GitHbub to host the code for remote development, which is convenient when the team is relatively dispersed (this reflects the advantages of Git distribution); developers only need to clone the project locally and carry out corresponding development. Then push and upload it to GitHub.