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
github是什么github是什么Mar 24, 2023 pm 05:46 PM

​GitHub是一个面向开源及私有软件项目的托管平台,可以让开发者们在这里托管自己的代码,并进行版本控制。GitHub主打的是开源项目与协作,通过这个平台上的开源项目,开发者们可以查看其他开发者的项目源代码,并进行交流和学习。

git中push -u是什么意思git中push -u是什么意思Jul 01, 2022 am 10:36 AM

在git中,“push -u”的意思是将本地的分支版本上传到远程合并,并且记录push到远程分支的默认值;当添加“-u”参数时,表示下次继续push的这个远端分支的时候推送命令就可以简写成“git push”。

如何在GitLab上进行第一次登录并更改密码如何在GitLab上进行第一次登录并更改密码Mar 24, 2023 pm 05:46 PM

GitLab是一种基于Web的Git版本控制库管理软件,旨在帮助开发团队更好地协同工作,提高工作效率。当您第一次登录GitLab时,系统会提示您要更改初始密码以确保账户安全。本文将为大家介绍如何在GitLab上进行第一次登录并更改密码。

git的pack文件有什么用git的pack文件有什么用Jun 30, 2022 pm 05:41 PM

在git中,pack文件可以有效的使用磁盘缓存,并且为常用命令读取最近引用的对象提供访问模式;git会将多个指定的对象打包成一个成为包文件(packfile)的二进制文件,用于节省空间和提高效率。

git中pull失败了怎么办git中pull失败了怎么办Jun 30, 2022 pm 04:47 PM

git中pull失败的解决方法:1、利用“git reset --hard”强制覆盖掉自己的本地修改;2、利用“git stash”推送一个新的储藏,拉取之后利用“git stash pop”将修改保存到暂存区;3、若依然出现问题,则将文件保存到暂存区并提交注释即可。

git分支能改名字吗git分支能改名字吗Jun 16, 2022 pm 05:55 PM

git分支能改名字。改名方法:1、利用git中的branch命令修改本地分支的名称,语法为“git branch -m 旧名字 新名字”;2、利用“git push origin 新名字”命令,在删除远程分支之后将改名后的本地分支推送到远程;3、利用IDEA直接操作修改分支名称即可。

git怎么删除某个分支git怎么删除某个分支Jun 24, 2022 am 11:11 AM

git删除某个分支的方法:1、利用“git branch --delete dev”命令删除本地分支;2、利用“git push origin --delete branch”命令删除远程分支;3、利用“git branch --delete --remotes”命令删除追踪分支。

用三行代码使你的git提交记录变干净用三行代码使你的git提交记录变干净Feb 28, 2023 pm 04:19 PM

本篇文章给大家带来了关于git的相关知识,其中主要跟大家聊一聊怎么让你的git记录保持整洁,感兴趣的朋友下面一起来看一下吧,希望对大家有帮助。

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools