This article brings you relevant knowledge about Git, which mainly introduces the basic operations of git under Linux and Windows. I hope it will be helpful to everyone.
Recommended study: "Git Tutorial"
git study notes - basic operations of git under Linux and Windows
github homepage: https://github.com/Taot-chen
1. Linux
Environment
1. Install git
sudo apt-get install git
2. Set git account information
git config --global user.name "你的git账号用户名"git config --global user.email "你的邮箱"
3. Set up the credential helper to help us save our code in memory within a certain period of time. The second line sets the timeout
git config --global credential.helper cachegit config --global credential.helper 'cache --timeout=3600'
4. Obtain the ssh key and associate it with the remote GIT
ssh-keygen -t rsa -C "你的邮箱"# 之后一路回车cd ~/.ssh cat id_rsa.pub# 再在Terminal中复制密钥,添加到github的settings的SSH公钥中,完成免密码登录关联 # 验证ssh通信情况,提示连接成功即可ssh -T git@github.com# 其他操作就和为windows的几乎一致
5. Push common commands
git init # 初始化本地仓库git config --list # 可以查看你的git配置信息 # 提交文件到本地仓库git add abc.cppgit commit -m "first commit" //-m 用于指定本次提交的描述信息 # 提交到repositorygit remote add origin "github仓库ssh地址" //关联仓库git push origin master #master 是分支名 # 以后若提交到相同仓库,提交到本地仓库之后,直接git push即可# 克隆项目git clone "项目的ssh地址"
6. Increase the cache when push error is reported
git config http.postBuffer 52428800 //(根据文件大小随便调整)
2. Windows
Environment
1. Version control; backup and modification
1) Local version control system
Store the version number in the database to Distinguish between record version changes.
2) Centralized Version Control System (CVCS)
Have a server dedicated to storing version revisions, and can easily locate related records with the help of version records.
3) Distributed Version Control System (DVCS)
The client not only extracts the latest version of the file snapshot, but also mirrors the original code repository locally for collaboration anywhere. In case of server failure, any mirrored local warehouse can be used to recover afterwards.
2. Installation and configuration of git under Windows
1) Installation
Download and install the corresponding version from the git official website, and find Git-> in the menu Git Bash
, if the command line window appears, the installation is successful.
-
View version:
git --version
2) Configure user name and email
git config --gobal user.name "your user name" # 配置用户名git config --gobal user.email "your email" #配置邮箱git config --list # 查看所有配置
3. Three statuses of git files and Working mode
1) Three states
- Committed: The data has been safely saved in the local database
- Modified: Modified file, but has not been saved to the database
- Staged (staged): The current version of a modified file is marked to be included in the next submitted snapshot
2) Three work areas
- Workspace: Local project directory
- Staging area: Take a snapshot of the modified file and add it to the staging area
- git repository: the hidden directory of the workspace
.git
is not considered a workspace, but is the git version library
3) git workflow
- Modify some files in the workspace;
- Take a snapshot of the modified files and add them to the temporary storage area
- Submit the update and save the snapshot in the temporary storage area Permanently stored in the git repository
Pull: git repository->Local workspace
Submit:Local workspace->Temporary Storage area->git warehouse
4. Create a version library and submit the file
1) Initialize the local warehouse
Initialize a local warehouse without any Empty repository for files.
git init
2) Create a new folder git01.txt and add it to the staging area
git add # 将文件添加到暂存区git add . # 提交当前目录的全部文件git status # 查看文件的状态git commit # 将暂存区的文件提交到本地仓库git log # 查看完整的提交日志信息git diff HEAD --file # 查看文件法file历次提交的区别
For example:
In Git Bash
中
git init # 创建空仓库git add git01.txt # 将文件git01.txt添加到缓存区git commit -m '第一次提交' #提交文件到本地仓库,单引号内的内容是本次提交的注释,必须要有git status # 查看暂存区文件状态git log # 查看完整的提交记录
5. File modification and submission modification
You can modify the file directly in the workspace, then add it to the staging area and submit it to the local warehouse
Note: Must be added to the temporary storage area before submission
1) Submission and withdrawal of files in the temporary storage area
Submit: git add/git commit
-
Undo:
Remove from the staging area:git restore --staged git02.txt # 从暂存区移除文件git02.txtgit reset HEAD git02.txt # 取消关于文件git02.txt上一次的操作
2) Version rollback
Simplified display of submission records:
git log --pretty=oneline
At this time, the HEAD pointer points to the last submitted record by default. Version rollback is the version that the HEAD pointer wants to roll back to.
git reset --hard HEAD^ # 回退一个版本git reset --hard HEAD^^ # 回退两个版本git reset --hard HEAD~n # 回退n个版本git reset --hard "版本识别码” # 回退或者前进到版本识别码所在的版本git reflog # 显示所有的提交记录(包括HEAD指向的版本之后的版本),即可以显示用户的每一次操作的记录
3) File deletion
git ls-files # 查看本地仓库的文件目录git rm filename # 删除文件filename # 另一种删除方法:现在工作区删除文件,之后再提交操作即可
6, remote warehouse
1)github
git clone "项目地址"(github地址) # 下载github项目(可以不登陆)
2)SSH download (requires login)
# 首先需要在gitbash中生成一个keyssh-keygen -t rsa -C "github邮箱" # 找到生成的公钥,打开后复制,之后再去github中添加`SSH and GPG keys` # 验证有没有添加成功ssh -T git@github.com# 出现您以被成功认证即可(即此时已经将ssh绑定了github) # 下载项目git clone "项目地址" (ssh地址)
3) Push the local project (local warehouse) to the remote warehouse
# 在github新建一个仓库# 将本地项目提交到本地仓库 # 将本地仓库绑定github上面的远程仓库git remote add origin "github仓库地址" # 将其推到远程仓库的主干上(远程仓库中包含本地仓库的所有提交记录)git push -u origin master # 以后的更新推送,只需要在本地提交完成之后,直接如下命令git push
7, git branch operation
The trunk is a project that is already online, and any operation in the branch will not affect the trunk function. After the branch is complete and correct, merge it into the trunk.
1) Local branch operations
Commonly used basic commands
命令 | 描述 |
---|---|
git checkout branch | 切换到指定分支 |
git checkout -b new_branch | 新建分支并切换到新建分支 |
git branch -d branch | 删除指定分支 |
git branch | 查看所有分支,并且* 标记当前所在分支 |
git merge branch | 合并分支 |
git branch -m / -M oldbranch newbranch | 重命名分支,如果new_branch名字分支已经存在,则需要使用-M强制重命名 |
- 切换到指定分支:git checkout branch
- 新建分支并切换到新建分支:git checkout -b new_branch
- 删除指定分支:git branch -d branch
- 查看所有分支,并且
*
标记当前所在分支:git branch - 合并分支:git merge branch
- 重命名分支,如果new_branch名字分支已经存在,则需要使用-M强制重命名:git branch -m | -M oldbranch newbranch
注:
只能在主干分支上来合并分支,不可反过来。(虽然git不会报错,但是这样是不可以的)
分支所具有的内容,就是创建分支的时候主干所具有的内容。
2)远程分支操作
分支push和pull
相关命令
命令 | 描述 |
---|---|
git branch -a | 查看本地与远程分支 |
git push origin branch_name | 推送本地分支到远程 |
git push origin :remote_branch | 删除远程分支(本地分支还保留) |
git checkout -b local_branch origin/remote_branch | 拉取远程指定分支并在本地创建分支 |
获取远程分支的最新状态
git fetch
图表的方式显示操作记录
git log --graph --pretty=oneline
3)本地分支冲突解决
# 当分支和主干的同一文件的同一行不同的时候,合并分支就会出现冲突 # 根据具体的需要修改,使之相同即可
4)多人协同操作冲突
# 两个用户对同一个文件的同一行进行了不同的操作 # 解决方法:在推送之期拉一下远程仓库,在本地根据具体的需求解决完冲突之后再推送
9、标签管理
标签操作基本命令git tag
命令 | 描述 |
---|---|
git tag tag_name | 新建标签,默认为HEAD |
git tag -a tag_name -m ‘xxx’ | 添加标签并指定标签描述信息 |
git tag | 查看所有标签 |
git tag -d tag_name | 删除一个本地标签 |
git push origin tag_name | 推送本地标签到远程 |
git push origin --tags | 推送全部未推送过的本地标签到远程 |
git push origin :refs/tags/tag_name | 删除一个远程标签 |
10、Idea下git基本操作
1)环境集成配置
Configure
->Settings
->搜索git->在Path to Git executable中添加git的安装路径(一直到git.exe)->test
->出现版本号,即表示成功->添加github
或 File
->Other Settings
->Setting for New Projects
->Git/Git Hub
2)推送项目到远程仓库
项目提交到本地仓库->创建远程仓库->绑定远程仓库->推送到远程仓库
3)分支操作
4)gitignore插件
5)冲突及其解决
推荐学习:《Git学习教程》
The above is the detailed content of Completely master the basic operations of git under Linux and Windows. For more information, please follow other related articles on the PHP Chinese website!

Git and GitHub are essential tools for modern developers. 1. Use Git for version control: create branches for parallel development, merge branches, and roll back errors. 2. Use GitHub for team collaboration: code review through PullRequest to resolve merge conflicts. 3. Practical tips and best practices: submit regularly, submit messages clearly, use .gitignore, and back up the code base regularly.

Git and GitHub are not the same thing: Git is a distributed version control system, and GitHub is an online platform based on Git. Git helps developers manage code versions and achieve collaboration through branching, merge and other functions; GitHub provides code hosting, review, problem management and social interaction functions, enhancing Git's collaboration capabilities.

After installing Git, in order to use more efficiently, the following settings are required: Set user information (name and mailbox) Select text editor Set external merge tool Generate SSH key settings Ignore file mode

Resolve: When Git download speed is slow, you can take the following steps: Check the network connection and try to switch the connection method. Optimize Git configuration: Increase the POST buffer size (git config --global http.postBuffer 524288000), and reduce the low-speed limit (git config --global http.lowSpeedLimit 1000). Use a Git proxy (such as git-proxy or git-lfs-proxy). Try using a different Git client (such as Sourcetree or Github Desktop). Check for fire protection

Causes of slow Git downloads include poor network connections, Git server problems, large files or large submissions, Git configuration issues, insufficient computer resources, and other factors such as malware. Workarounds include improving network connectivity, adjusting firewall settings, avoiding downloading unnecessary files or submissions, optimizing Git configuration, providing adequate computer resources, and scanning and removing malware.

How to update local Git code? Use git fetch to pull the latest changes from the remote repository. Merge remote changes to the local branch using git merge origin/<remote branch name>. Resolve conflicts arising from mergers. Use git commit -m "Merge branch <Remote branch name>" to submit merge changes and apply updates.

Steps to update git code: Check out code: git clone https://github.com/username/repo.git Get the latest changes: git fetch merge changes: git merge origin/master push changes (optional): git push origin master

You can delete a Git branch through the following steps: 1. Delete the local branch: Use the git branch -d <branch-name> command; 2. Delete the remote branch: Use the git push <remote-name> --delete <branch-name> command; 3. Protected branch: Use git config branch. <branch-name>.protected true to add the protection branch settings.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool