Home > Development Tools > git > Summary of common Git commands

Summary of common Git commands

藏色散人
Release: 2019-06-17 15:09:40
Original
5147 people have browsed it

Summary of common Git commands

Common git commands are:

git init

Create a new repo locally and enter In a project directory, executing git init will initialize a repo and create a .git folder under the current folder.

git clone

Get the remote Git repo corresponding to a url and create a local copy.

The general format is git clone [url].

The cloned repo will start with the last slash of the url. Name it and create a folder. If you want to specify a specific name, you can specify it with git clone [url] newname.

git status

Query the status of the repo.

git status -s: -s means short, the output mark of -s will have two columns, the first column is for the staging area, and the second column is for the working directory In terms of.

git log

show commit history of a branch.

git log --oneline --number : Each log only displays one line, showing number.

git log --oneline --graph: can graphically represent the branch merge history.

git log branchname can display a specific branch log.

git log --oneline branch1 ^branch2, you can view the commits in branch 1 but not in branch 2. ^ means to exclude this branch (you may need to add quotation marks to ^branch2 under Windows).

git log --decorate will display tag information.

git log --author=[author name] can specify the author's submission history.

git log -- since --before --until --after Filter logs based on commit time.

--no-merges can exclude merge commits.

git log --grep Filter based on commit information log: git log --grep=keywords

By default, git log --grep --author is an OR relationship, that is, it will be returned if one of them is met. If you want them to be an AND relationship, you can Add the --all-match option.

git log -S: filter by introduced diff.

For example: git log -SmethodName (note that there is no equal sign between S and the following words separated).

git log -p: show patch introduced at each commit.

Each submission is a snapshot, and Git will calculate the diff of each submission as A patch is shown to you.

Another method is git show [SHA].

git log --stat: show diffstat of changes introduced at each commit.

It is also used to see the relative information of changes. --stat is simpler than the output of -p.

git add

Before committing, Git has a staging area (staging area), where newly added files can be placed or new changes can be added. The changes submitted when committing are the last changes added to the staging area, not the ones on our disk Change.

git add .

will recursively add all files in the current working directory.

git diff

git diff without parameters:

show diff of unstaged changes.

This command compares the difference between the current file in the working directory and the snapshot of the staging area. That is, the changes that have not been temporarily saved after modification.

To see the difference between the files that have been temporarily saved and the snapshot of the last submission, you can use:

git diff --cached command.

show diff of staged changes.

(Git 1.6.1 and later also allows the use of git diff --staged, the effect is Same).

git diff HEAD

show diff of all staged or unstated changes.

That is, compare the woking directory with the last commit All changes during the period.

If you want to see what has been changed since a certain version, you can use:

git diff [version tag]

Like the log command, diff can also be added with the --stat parameter to simplify the output.

git diff [branchA] [branchB] can be used to compare two branches.

It will actually return a patch from A to B, which is not the result we want.

Generally, the result we want is what are the changes after the two branches are separated, and what are the changes? Command:

git diff [branchA]...[branchB] is given.

Actually it is: git diff $(git merge-base [branchA] [branchB]) [branchB] The result.

git commit

Submit the changes that have been added.

git commit -m "the commit message"

git commit -a will first add the changes to all tracked files, and then Submit (a bit like a svn submission, no need to temporarily save it first). For files without track, you still need to git add.

git commit --amend to supplement the submission. The same parent as the current submission node will be used The node makes a new submission, and the old submission will be canceled.

git reset

undo changes and commits.

The HEAD keyword here refers to the latest commit at the end of the current branch. That is, the latest version on the branch in the repository.

git reset HEAD: unstage files from index and reset pointer to HEAD

This command is used to take out the files that were accidentally added from the staged state. It can be operated on a certain file alone: ​​git reset HEAD - - filename, this - - can also be omitted.

git reset --soft

move HEAD to specific commit reference, index and staging are untouched.

git reset --hard

unstage files AND undo any changes in the working directory since last commit.

Use git reset --hard HEAD to reset, that is, after the last commit, all staged changes and changes in the working directory will disappear and be restored to the state of the last commit.

HEAD here can be written as SHA-1 of any commit.

Git reset without soft and hard parameters actually uses the default parameter mixed.

Summary:

git reset --mixed id changes the HEAD of git (that is, the commit record has changed), but the file has not changed (that is, the working tree has not changed) ). Cancel the contents of commit and add.

git reset --soft id. In fact, after git reset --mixed id, do git add again. That is, cancel the contents of commit.

git reset --hard id. Changes the git HEAD and the files.

Sort by change scope as follows:

soft (commit) < mixed ( commit add) < hard (commit add local working)

git revert

Reverse and undo the commit. Just change the wrong commit (commit ) name (reference) can be passed to the command as a parameter.

git revert HEAD: Undo the most recent submission.

git revert will create a new reverse submission, which can be passed Parameter -n to tell Git not to commit yet.

git rm

git rm file: Remove the file from the staging area and also move it Remove the working directory.

git rm --cached: Remove files from the staging area, but leave them in the working directory.

git rm --cached is functionally equivalent to git reset HEAD , clear the cache area, but leave the working directory tree unchanged.

git clean

git clean removes no track from the working directory File.

The usual parameter is git clean -df:

-d means to remove the directory at the same time, -f means force, because in the git configuration file, clean.requireForce=true , if -f is not added, clean will refuse to execute.

git mv

git rm - - cached orig; mv orig new; git add new

git stash

Push the current changes into a stack.

git stash will put the current changes into a stack. All changes in the directory and index (but not including untracked files) are pushed into a stack, leaving you with a clean working state, that is, at the last latest submission.

git stash list will display The list of this stack.

git stash apply: Take out the previous item in stash (stash@{0}) and apply it to the current working directory.

You can also specify other projects , such as git stash apply stash@{1}.

If you want to delete the item in stash while applying it, you can use git stash pop

to delete stash Projects in:

git stash drop: Delete the previous one, or you can specify parameters to delete a specified project.

git stash clear: Delete all projects.

git branch

git branch can be used to list branches, create branches and delete branches.

git branch -v can see the end of each branch One commit.

git branch: List all local branches, the current branch will be marked with an asterisk.

git branch (branchname): Create a new branch (when you use this When creating a branch, the branch is based on your last submission).

git branch -d (branchname): Delete a branch.

Delete remote branch:

git push (remote-name):(branch-name): delete a remote branch .

This is because the complete command form is:

git push remote-name local-branch:remote-branch

And the local-branch part here is empty, It means deleting remote-branch

git checkout

git checkout (branchname)

Switch to a branch.

git checkout -b (branchname): Create and switch to a new branch.

This command is the result of combining git branch newbranch and git checkout newbranch.

Checkout has another function: replacing local changes:

git checkout --

This command will replace the files in your working directory with the latest contents in HEAD. Changes and new files that have been added to the staging area will not be Will be affected.

Note: git checkout filename will delete all changes in the file that have not been temporarily saved and committed. This operation is irreversible.

git merge

Merge a branch into the current branch.

git merge [alias]/[branch]

Merge the remote branch into the current branch.

If a conflict occurs and needs to be modified manually, you can use git mergetool.

When resolving the conflict, you can use git diff. After the conflict is resolved, use git add to add it, which means The conflict has been resolved.

git tag

tag a point in history as import.

will be on a commit To create a permanent bookmark, you usually add a tag after releasing a release version or shipping something.

For example: git tag v1.0

git tag -a v1.0, -a The parameters will allow you to add some information, that is, make an annotated tag.

When you run the git tag -a command, Git will open an editor for you to enter the tag information.

We can use commit SHA to tag a past submission:

git tag -a v0.9 XXXX

When pushing, it is not If you want to include tags, you can add the --tags parameter when pushing.

When fetching, tags that can be reached by branch HEAD are automatically fetched, tags that aren't reachable from branch heads will be skipped. If you want to ensure that all tags are included, you need to add the --tags option.

git remote

list, add and delete remote repository aliases.

Because there is no need to use the complete URL every time, Git creates an alias for each remote repo URL, and then uses git remote to manage the list.

git remote: List remote aliases.

If you clone a project, Git will automatically add the original url, and the alias is called: origin.

git remote - v: You can see the actual url corresponding to each alias.

git remote add [alias] [url]: Add a new remote repo.

git remote rm [alias]: Delete one Existing remote alias.

git remote rename [old-alias] [new-alias]: Rename.

git remote set-url [alias] [url]: Update url. Yes Add the -push and fetch parameters to set different access addresses for the same alias.

git fetch

download new branches and data from a remote repository.

You can use git fetch [alias] to get a certain remote repo, or you can use git fetch --all to get all repo

fetch will get all the ones you don’t have locally Data, all removed branches can be called remote branches, they are the same as local branches (you can view diff, log, etc., and can also be merged to other branches), but Git does not allow you to checkout them.

git pull

fetch from a remote repo and try to merge into the current branch.

pull == fetch merge FETCH_HEAD

Git pull will first execute git fetch, then execute git merge, and merge the head of the fetched branch into the current branch. This merge operation will generate a new commit.

If you use the --rebase parameter, it Will execute git rebase to replace the original git merge.

git rebase

--rebase will not generate merged submissions. It will temporarily save all local submissions as patches and place them in the ".git/rebase" directory, and then update the current branch to the latest branch tip. Finally, apply the saved patch to the branch.

During the rebase process, conflicts may occur. Git will stop rebase and let you resolve the conflict. After resolving the conflict, use git add to update the content. Then there is no need to execute commit, just:

git rebase --continue will continue to make the remaining patches.

git rebase --abort will terminate rebase, and the current branch will return to rebase Previous state.

git push

push your new branches and data to a remote repository.

git push [ alias] [branch]

will merge the current branch to the [branch] branch on alias. If the branch already exists, it will be updated. If it does not exist, the branch will be added.

If multiple people push code to the same remote repo, Git will first run git log on the branch you are trying to push to check whether the current tip of the branch on the server can be seen in its history. If it cannot be seen in the local history If you go to the server's tip, it means that the local code is not the latest. Git will reject your push and let you fetch and merge first, and then push. This ensures that everyone's changes will be taken into account.

git reflog

Git reflog is a command to manage reflog. Reflog is a mechanism used by git to record reference changes, such as recording branch changes or Changes in HEAD references.

When git reflog does not specify a reference, the reflog of HEAD is listed by default.

HEAD@{0} represents the current value of HEAD, and HEAD@{3} represents The value of HEAD before three changes.

Git will record the changes in the reflog file corresponding to HEAD. The path is .git/logs/HEAD. The reflog files of the branches are placed in .git/logs/ In the subdirectories under the refs directory.

Special symbols:

^ represents the parent submission. When a submission has When there are multiple parent submissions, you can indicate the number of parent submissions by following ^: ^ is equivalent to ^1.

~ is equivalent to consecutive ^ .

git init

Create a new repo locally, enter a project directory, and execute git init. A repo will be initialized and created in the current folder. git folder.

git clone

Get the remote Git repo corresponding to a url and create a local copy.

The general format is git clone [url].

The cloned repo will be named after the last slash in the url. Create a folder. If you want to specify a specific name, you can git clone [url ] newname specified.

git status

Query the status of the repo.

git status -s: -s means short , the output mark of -s will have two columns, the first column is for the staging area, and the second column is for the working directory.

git log

Show commit history of a branch.

git log --oneline --number: Each log only displays one line, display number.

git log -- oneline --graph: can graphically display the branch merge history.

git log branchname can display the log of a specific branch.

git log --oneline branch1 ^branch2, you can view the branch 1, but not in branch 2. ^ means excluding this branch (you may need to add quotation marks to ^branch2 under Windows).

git log --decorate will display the tag information.

git log --author=[author name] You can specify the author's submission history.

git log --since --before --until --after Filter logs based on submission time.

--no-merges can exclude merge commits.

git log --grep Filter logs based on commit information: git log --grep=keywords

By default, git log - -grep --author is an OR relationship, that is, if one of them is met, it will be returned. If you want them to be an AND relationship, you can add the --all-match option.

git log -S: filter by introduced diff.

For example: git log -SmethodName (note that there is no equal sign between S and the following words).

git log -p: show patch introduced at each commit.

Each submission is a snapshot. Git will calculate the diff of each submission and display it to you as a patch.

Another method is git show [SHA ].

git log --stat: show diffstat of changes introduced at each commit.

It is also used to see the relative information of the changes. The output of --stat is simpler than -p Some.

git add

     在提交之前,Git有一个暂存区(staging area),可以放入新添加的文件或者加入新的改动. commit时提交的改动是上一次加入到staging area中的改动,而不是我们disk上的改动.

     git add .

     会递归地添加当前工作目录中的所有文件.

 

git diff

     不加参数的git diff:

     show diff of unstaged changes.

     此命令比较的是工作目录中当前文件和暂存区域快照之间的差异,也就是修改之后还没有暂存起来的变化内容.

 

     若要看已经暂存起来的文件和上次提交时的快照之间的差异,可以用:

     git diff --cached 命令.

     show diff of staged changes.

     (Git 1.6.1 及更高版本还允许使用 git diff --staged,效果是相同的).

 

     git diff HEAD

     show diff of all staged or unstated changes.

     也即比较woking directory和上次提交之间所有的改动.

 

     如果想看自从某个版本之后都改动了什么,可以用:

     git diff [version tag]

     跟log命令一样,diff也可以加上--stat参数来简化输出.

 

     git diff [branchA] [branchB]可以用来比较两个分支.

     它实际上会返回一个由A到B的patch,不是我们想要的结果.

     一般我们想要的结果是两个分支分开以后各自的改动都是什么,是由命令:

     git diff [branchA]…[branchB]给出的.

     实际上它是:git diff $(git merge-base [branchA] [branchB]) [branchB]的结果.

 

 

git commit

Submit the changes that have been added.

git commit -m "the commit message"

git commit -a will first add the changes to all tracked files, and then Submit (a bit like a svn submission, no need to temporarily save it first). For files without track, you still need to git add.

git commit --amend to supplement the submission. The same parent as the current submission node will be used The node makes a new submission, and the old submission will be canceled.

git reset

undo changes and commits.

The HEAD keyword here refers to the latest commit at the end of the current branch. That is, the latest version on the branch in the repository.

git reset HEAD: unstage files from index and reset pointer to HEAD

This command is used to take out the files that were accidentally added from the staged state. It can be operated on a certain file alone: ​​git reset HEAD - - filename, this - - can also be omitted.

git reset --soft

move HEAD to specific commit reference, index and staging are untouched.

git reset --hard

unstage files AND undo any changes in the working directory since last commit.

Use git reset --hard HEAD to reset, that is, after the last commit, all staged changes and changes in the working directory will disappear and be restored to the state of the last commit.

HEAD here can be written as SHA-1 of any commit.

Git reset without soft and hard parameters actually uses the default parameter mixed.

Summary:

git reset --mixed id changes the HEAD of git (that is, the commit record has changed), but the file has not changed (that is, the working tree has not changed) ). Cancel the contents of commit and add.

git reset --soft id. In fact, after git reset --mixed id, do git add again. That is, cancel the contents of commit.

git reset --hard id. Changes the git HEAD and the files.

Sort by change scope as follows:

soft (commit) < mixed ( commit add) < hard (commit add local working)

git revert

Reverse and undo the commit. Just change the wrong commit (commit ) name (reference) can be passed to the command as a parameter.

git revert HEAD: Undo the most recent submission.

git revert will create a new reverse submission, which can be passed Parameter -n to tell Git not to commit yet.

git rm

git rm file: Remove the file from the staging area and also move it Remove the working directory.

git rm --cached: Remove files from the staging area, but leave them in the working directory.

git rm --cached is functionally equivalent to git reset HEAD , clear the cache area, but leave the working directory tree unchanged.

git clean

git clean removes no track from the working directory File.

The usual parameter is git clean -df:

-d means to remove the directory at the same time, -f means force, because in the git configuration file, clean.requireForce=true , if -f is not added, clean will refuse to execute.

git mv

git rm - - cached orig; mv orig new; git add new

git stash

Push the current changes into a stack.

git stash will put the current changes into a stack. All changes in the directory and index (but not including untracked files) are pushed into a stack, leaving you with a clean working state, that is, at the last latest submission.

git stash list will display The list of this stack.

git stash apply: Take out the previous item in stash (stash@{0}) and apply it to the current working directory.

You can also specify other projects , such as git stash apply stash@{1}.

If you want to delete the item in stash while applying it, you can use git stash pop

to delete stash Projects in:

git stash drop: Delete the previous one, or you can specify parameters to delete a specified project.

git stash clear: Delete all projects.

git branch

git branch can be used to list branches, create branches and delete branches.

git branch -v can see the end of each branch One commit.

git branch: List all local branches, the current branch will be marked with an asterisk.

git branch (branchname): Create a new branch (when you use this When creating a branch, the branch is based on your last submission).

git branch -d (branchname): Delete a branch.

Delete remote branch:

git push (remote-name):(branch-name): delete a remote branch .

This is because the complete command form is:

git push remote-name local-branch:remote-branch

And the local-branch part here is empty, It means deleting remote-branch

git checkout

git checkout (branchname)

Switch to a branch.

git checkout -b (branchname): Create and switch to a new branch.

This command is the result of combining git branch newbranch and git checkout newbranch.

Checkout has another function: replacing local changes:

git checkout --

This command will replace the files in your working directory with the latest contents in HEAD. Changes and new files that have been added to the staging area will not be Will be affected.

Note: git checkout filename will delete all changes in the file that have not been temporarily saved and committed. This operation is irreversible.

git merge

Merge a branch into the current branch.

git merge [alias]/[branch]

Merge the remote branch into the current branch.

If a conflict occurs and needs to be modified manually, you can use git mergetool.

When resolving the conflict, you can use git diff. After the conflict is resolved, use git add to add it, which means The conflict has been resolved.

git tag

tag a point in history as import.

will be on a commit To create a permanent bookmark, you usually add a tag after releasing a release version or shipping something.

For example: git tag v1.0

git tag -a v1.0, -a The parameters will allow you to add some information, that is, make an annotated tag.

When you run the git tag -a command, Git will open an editor for you to enter the tag information.

We can use commit SHA to tag a past submission:

git tag -a v0.9 XXXX

When pushing, it is not If you want to include tags, you can add the --tags parameter when pushing.

When fetching, tags that can be reached by branch HEAD are automatically fetched, tags that aren't reachable from branch heads will be skipped. If you want to ensure that all tags are included, you need to add the --tags option.

git remote

list, add and delete remote repository aliases.

Because there is no need to use the complete URL every time, Git creates an alias for each remote repo URL, and then uses git remote to manage the list.

git remote: List remote aliases.

If you clone a project, Git will automatically add the original url, and the alias is called: origin.

git remote - v: You can see the actual url corresponding to each alias.

git remote add [alias] [url]: Add a new remote repo.

git remote rm [alias]: Delete one Existing remote alias.

git remote rename [old-alias] [new-alias]: Rename.

git remote set-url [alias] [url]: Update url. Yes Add the -push and fetch parameters to set different access addresses for the same alias.

git fetch

download new branches and data from a remote repository.

You can use git fetch [alias] to get a certain remote repo, or you can use git fetch --all to get all repo

fetch will get all the ones you don’t have locally Data, all removed branches can be called remote branches, they are the same as local branches (you can view diff, log, etc., and can also be merged to other branches), but Git does not allow you to checkout them.

git pull

fetch from a remote repo and try to merge into the current branch.

pull == fetch merge FETCH_HEAD

Git pull will first execute git fetch, then execute git merge, and merge the head of the fetched branch into the current branch. This merge operation will generate a new commit.

If you use the --rebase parameter, it Will execute git rebase to replace the original git merge.

git rebase

     --rebase不会产生合并的提交,它会将本地的所有提交临时保存为补丁(patch),放在”.git/rebase”目录中,然后将当前分支更新到最新的分支尖端,最后把保存的补丁应用到分支上.

     rebase的过程中,也许会出现冲突,Git会停止rebase并让你解决冲突,在解决完冲突之后,用git add去更新这些内容,然后无需执行commit,只需要:

     git rebase --continue就会继续打余下的补丁.

     git rebase --abort将会终止rebase,当前分支将会回到rebase之前的状态.

 

git push

     push your new branches and data to a remote repository.

     git push [alias] [branch]

     将会把当前分支merge到alias上的[branch]分支.如果分支已经存在,将会更新,如果不存在,将会添加这个分支.

     如果有多个人向同一个remote repo push代码, Git会首先在你试图push的分支上运行git log,检查它的历史中是否能看到server上的branch现在的tip,如果本地历史中不能看到server的tip,说明本地的代码不是最新的,Git会拒绝你的push,让你先fetch,merge,之后再push,这样就保证了所有人的改动都会被考虑进来.

 

git reflog

     git reflog是对reflog进行管理的命令,reflog是git用来记录引用变化的一种机制,比如记录分支的变化或者是HEAD引用的变化.

     当git reflog不指定引用的时候,默认列出HEAD的reflog.

     HEAD@{0}代表HEAD当前的值,HEAD@{3}代表HEAD在3次变化之前的值.

     git会将变化记录到HEAD对应的reflog文件中,其路径为.git/logs/HEAD, 分支的reflog文件都放在.git/logs/refs目录下的子目录中.

 

 

特殊符号:

     ^代表父提交,当一个提交有多个父提交时,可以通过在^后面跟上一个数字,表示第几个父提交: ^相当于^1.

     ~相当于连续的个^.

更多Git相关技术文章,请访问Git使用教程栏目!

The above is the detailed content of Summary of common Git commands. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template