search
HomeDevelopment ToolsgitWhat are the commonly used operating commands in git? Summary of common operation commands

What are the commonly used operating commands in git? This article summarizes some commonly used operating commands in git. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

git start

Global configuration: configure user name and e-mail address

$ git config --global user.name"Your Name"
$ git config --global user.email"email@example.com"

git init: Initialize the git warehouse and generate a .git file in the directory

git init

git add File name: Modify and add the file To the warehouse

git add readme.txt //修改单个文件
git add . // 将所有修改的文件添加到暂存区

git commit -m 'Instructions': Submit the file to the warehouse

$ git commit -m "wrote a readme file"

git status: View the status of the current warehouse, Master the status of the workspace

git diff readme.txt: View the modified content of the file

Version rollback

git log: View history

git log --pretty=oneline: Only view commit.id (version number) and description

git reset --hard HEAD^: Roll back to the previous version HEAD represents the current version, the previous one is HEAD^, and the next one hundred is HEAD~100

git reset --hard commit.id: Return to the version corresponding to the specified version number

git reset --hard 1049a

git reflog: used to record each of your commands for determination Which version to return to

A few concepts

Working Directory: Perform git on the computer Operating directory

Repository: The .git file in the workspace is the repository. The most important thing in the git repository is called stage (or index) In the staging area, there is also the first branch master that Git automatically created for us, and a pointer to master called HEAD.

When adding a file to the Git repository, it is executed in two steps:

The first step is to use git addAdd the file, which actually adds the file modification to the temporary storage area;

The second step is to use git commit to submit the changes, In fact, all the contents of the staging area are submitted to the current branch.

Undo modifications

Undoing is divided into three situations:

The first is readme.txt It has not been placed in the temporary storage area since the modification. Now, undoing the modification will return to the same state as the repository; (no add) ---> git checkout -- file

The second is that after readme.txt has been added to the temporary storage area, it has been modified. Now, undo the modification and return to the addition. The state after reaching the staging area. (no commit) ---> git reset head file

The third way is that readme.txt has been committed, just use version rollback ---> git reset --hard head^

1. git checkout -- readme.txt: Undo all modifications to the specified file in the workspace

##Note: The -- in the command is very important. Without --, it becomes the "switch to another branch" command

2. git reset: You can either roll back the version or roll back the modifications in the temporary storage area to the workspace

In the second case, we You can use the git reset command to withdraw the workspace

git reset head readme.txt // head表示当前版本

After withdrawing the workspace, use the git checkout command to withdraw from the workspace

Delete the file

git rm file: Delete the file

from the repository. If you delete it accidentally, you can use the version of the repository. 'one click recovery'

git checkout -- test.txt

远程仓库

git remote add origin github仓库地址:将本地仓库与远程仓库关联

git push -u origin master: 由于远程库是空的,我们第一次推送master分支时,加上了-u参数,Git不但会把本地的master分支内容推送的远程新的master分支,还会把本地的master分支和远程的master分支关联起来,在以后的推送或者拉取时就可以简化命令。

git push origin master: 将本地master分支的修改推送到远程仓库

git clone github仓库地址:将远程仓库克隆到本地

分支管理

git branch dev:创建dev分支

git checkout dev :切换dev分支

git checkout -b dev:创建dev分支,并切换到dev分支。 -b参数表示创建并切换到dev分支

git branch: 查看所有分支, *表示当前分支

git merge dev: 合并指定分支到当前分支 。 结果中Fast-forward信息表示‘快进模式’ -->直接把master指向dev的当前提交,合并速度非常快

git merge --no-ff -m '描述内容' dev :--no-ff参数表示禁用Fast-forward,使用普通模式

git branch -d dev: 删除dev分支

git branch -D dev: 如果分支还没合并,使用-d无法删除,使用-D强制删除

git log --graph: 可以查看分支合并图。

git log --graph  --pretty=oneline --abbrev-commit  :--pertty=oneline 查看简短信息   --abbrev-commit: 查看commit缩写<span class="comment"><br></span>

场景:修复bug时创建分支

git stash: 将当前工作现场存储起来

git stash list :查看存储的工作现场列表

git stash pop:恢复工作现场并删除stash的内容

git remote: 查看远程仓库的信息

git remote -v: 查看origin的地址

git checkout -b dev origin/dev:在本地创建和远程分支对应的分支

git pull:抓取最新的远程提交

git branch --set-upstream-to=origin/dev dev: 建立本地分支和远程分支的关联

git rebse: 把本地未push的分叉提交历史整理成直线

标签管理

git tag : 用于创建一个新标签,默认指向head,也可以指向commi.id

git tag: 用于查看所有标签

git tag -a   -m  '标签信息' :为标签指定信息

git tag -d : 删除本地标签

git push origin :推送本地标签到远程

git push origin --tags: 推送全部未推送的本地标签到远程

git push origin :refs/tags/:删除一个远程标签

自定义git

git config --global alias.'自定义简写指令' '被简写的指令'

git config --global alias.st statusgit config --global alias.co checkout

命令行命令

cat readme.txt: 查看文件内容

rm file 删除文件

vi file: linux里的vi编辑器 

(1)通过i键进入插入模式,可以修改文件 

(2)通过Esc键进入命令模式   输入':wq!' -->保存+退出vi    输入':q!' -->不保存退出

The above is the detailed content of What are the commonly used operating commands in git? Summary of common operation commands. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:博客园. If there is any infringement, please contact admin@php.cn delete
Git: The Version Control System, GitHub: The Hosting PlatformGit: The Version Control System, GitHub: The Hosting PlatformApr 22, 2025 am 12:02 AM

Git is a distributed version control system developed by Linus Torvaz in 2005, and GitHub is a Git-based code hosting platform founded in 2008. Git supports branching and merges through snapshot management files, and GitHub provides pull requests, problem tracking and code review functions to facilitate team collaboration.

Git and GitHub: A Comparative AnalysisGit and GitHub: A Comparative AnalysisApr 21, 2025 am 12:10 AM

Git and GitHub are key tools in modern software development. Git is a distributed version control system, and GitHub is a Git-based code hosting platform. Git's core features include version control and branch management, while GitHub provides collaboration and project management tools. When using Git, developers can track file changes and work together; when using GitHub, teams can collaborate through PullRequests and Issues.

GitHub: An Introduction to the Code Hosting PlatformGitHub: An Introduction to the Code Hosting PlatformApr 20, 2025 am 12:10 AM

GitHubiscrucialforsoftwaredevelopmentduetoitscomprehensiveecosystemforcodemanagementandcollaboration.Itoffersversioncontrol,communitysupport,andtoolslikeGitHubActionsandPages.Startbymasteringbasicslikecreatingarepository,usingbranches,andautomatingwo

Git and GitHub: Essential Tools for DevelopersGit and GitHub: Essential Tools for DevelopersApr 19, 2025 am 12:17 AM

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: Their Relationship ExplainedGit and GitHub: Their Relationship ExplainedApr 18, 2025 am 12:03 AM

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.

What do you need to set after downloading GitWhat do you need to set after downloading GitApr 17, 2025 pm 04:57 PM

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

What to do if the git download is not activeWhat to do if the git download is not activeApr 17, 2025 pm 04:54 PM

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

Why is git downloading so slowWhy is git downloading so slowApr 17, 2025 pm 04:51 PM

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.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools