Home > Article > Development Tools > Completely master the basic operations of git under Linux and Windows
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"
github homepage: https://github.com/Taot-chen
Linux
Environmentsudo apt-get install git
git config --global user.name "你的git账号用户名"git config --global user.email "你的邮箱"
git config --global credential.helper cachegit config --global credential.helper 'cache --timeout=3600'
ssh-keygen -t rsa -C "你的邮箱"# 之后一路回车cd ~/.ssh cat id_rsa.pub# 再在Terminal中复制密钥,添加到github的settings的SSH公钥中,完成免密码登录关联 # 验证ssh通信情况,提示连接成功即可ssh -T git@github.com# 其他操作就和为windows的几乎一致
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地址"
git config http.postBuffer 52428800 //(根据文件大小随便调整)
Windows
EnvironmentStore the version number in the database to Distinguish between record version changes.
Have a server dedicated to storing version revisions, and can easily locate related records with the help of version records.
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.
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
git config --gobal user.name "your user name" # 配置用户名git config --gobal user.email "your email" #配置邮箱git config --list # 查看所有配置
.git
is not considered a workspace, but is the git version library Initialize a local warehouse without any Empty repository for files.
git init
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 # 查看完整的提交记录
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
Submit: git add/git commit
Undo:
Remove from the staging area:
git restore --staged git02.txt # 从暂存区移除文件git02.txtgit reset HEAD git02.txt # 取消关于文件git02.txt上一次的操作
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指向的版本之后的版本),即可以显示用户的每一次操作的记录
git ls-files # 查看本地仓库的文件目录git rm filename # 删除文件filename # 另一种删除方法:现在工作区删除文件,之后再提交操作即可
git clone "项目地址"(github地址) # 下载github项目(可以不登陆)
# 首先需要在gitbash中生成一个keyssh-keygen -t rsa -C "github邮箱" # 找到生成的公钥,打开后复制,之后再去github中添加`SSH and GPG keys` # 验证有没有添加成功ssh -T git@github.com# 出现您以被成功认证即可(即此时已经将ssh绑定了github) # 下载项目git clone "项目地址" (ssh地址)
# 在github新建一个仓库# 将本地项目提交到本地仓库 # 将本地仓库绑定github上面的远程仓库git remote add origin "github仓库地址" # 将其推到远程仓库的主干上(远程仓库中包含本地仓库的所有提交记录)git push -u origin master # 以后的更新推送,只需要在本地提交完成之后,直接如下命令git push
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.
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 branch分支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
# 当分支和主干的同一文件的同一行不同的时候,合并分支就会出现冲突 # 根据具体的需要修改,使之相同即可
# 两个用户对同一个文件的同一行进行了不同的操作 # 解决方法:在推送之期拉一下远程仓库,在本地根据具体的需求解决完冲突之后再推送
标签操作基本命令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 | 删除一个远程标签 |
Configure
->Settings
->搜索git->在Path to Git executable中添加git的安装路径(一直到git.exe)->test
->出现版本号,即表示成功->添加github
或 File
->Other Settings
->Setting for New Projects
->Git/Git Hub
项目提交到本地仓库->创建远程仓库->绑定远程仓库->推送到远程仓库
推荐学习:《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!