Git Utility Commands Summary

Source: Internet
Author: User
Tags diff using git

初始化A directory

git init

添加本地修改的文件

add .

Which. Represents adding the current directory under all modifications & additions to the file, if you want to add the specified directory or file separately. Replace it with a directory or file address.

提交更新

commit -m ‘注释‘

The submission here is submitted to the local repository.

删除文件

git rm file/dir

Using the shell directly rm file after the version will also need to use the git rm file command to delete files from the repository, where if the directory is deleted, you need to add an-R parameter

查看提交历史

log

git logcommand to view the commit history of all versions and the version number and comments for each commit, and each commit will have a unique version number.

Use the git log --graph ability to submit records more visually in a ' graph ' way, and if the display is a little more concise, you can add a –oneline parameter, and you can merge each commit into one line

HEAD

The head is a special pointer to git, which points to the latest commit of the current branch, which is a very important concept. head^ represents the last commit version of the current branch, head^^ represents the previous version, or it can be represented with Head~n, and N represents the previous version

    • If you modify something locally and do not want it, you can use the command git reset --hard HEAD to revert to the last post-commit version.
    • If you want to restore the first version, you can use the commandgit reset --hard HEAD^^
    • If you want to restore to the specified version based on the version number, you can use git reset --hard COMMIT_VERSION. the command, commit_version represents the version number

git restWhen you use the command to retire a version and then use git log the command to view the commit record, the records after the current version are not found. If you want to restore to a later version of the current version, you can use git reflog the view git every action record, from the inside to find the version good, and then re-use the git reset command can be restored

暂存区

It can also be called ' working directory/working directory ' all changes after the last commit are saved in ' staging area ', you can git status view all the changes in the current staging area by command, when using Git add/rm and other commands, it is actually the operation of staging area when using git commitcommand, the contents of the staging area are submitted once to the current branch.

比对文件 git diff

file

You can git diff see the difference between a file and the contents of this file in the current head version.

can also be git diff COMMIT_VERSION compared to a version of the current staging area file content, if you do not specify Commit_version, the default is head

You can also git diff COMMIT_VERSION file distinguish between a version of a specified file and the current one.

You can also git diff COMMIT_VERSION_1 COMMIT_VERSION_2 compare the differences before two versions. Also in the following can add a file path, view the difference before the specified file

撤销修改

gitcheckout--file

Note, be sure to use – if not used – to switch branches

If you want to modify a file before committing it, you can use the command to revoke the contents of the file to the content of the git ckeckout -- file head version after the last commit, if the file has not yet been added. If the file has been add, directly using checkout, the file will be back to the contents of the Add, if the last add when the content also needs to be revoked, you can first use the git reset file command to revoke the file from the staging area, the file will go back to the state before the Add, The checkout is then revoked. When you use the Reset command to operate a staging area file, the staging area file is removed staging area instead of the version that is retired

恢复被删除的文件

gitcheckout--file

Note, be sure – otherwise the checkout will represent the switch branch. Use this method to recover a file if the deleted file is only a local RM or commit, and if the file has been submitted, then you need to use the git reset command to recover.

添加一个远程仓库

add origin remoteRespPath

Where origin is casually defined, is to give the remote warehouse to take the individual name, Remoteresppath is the address of the remote warehouse, you can add multiple remote warehouses, to ensure that the remote warehouse alias can be different

查看远程仓库信息

查看原创仓库名称列表  remote查看原创仓库详细信息  remote -v

push代码到远程仓库

push remoteRespName localBranch:remoteBrancheg:push origin master:master

Where origin is the alias that was taken before the remote repository was added, Localbranch is the name of the local branch, and Remotebranch is the name git push origin master:master of the remote repository branch. The master branch of the local repository is push to the remote repository on the master branch of origin

It's a good idea to pull before each push. If a simple conflict is automatically merged, if there is a serious conflict, after resolving the conflict and then push, if the other people push the content after the last pull, then this push will be an error.

从远程仓库push代码到本地仓库

git pull remoteRespName brancheg: git pull origin master

Where Remoterespname represents the warehouse alias, branch is the branch name, which will git pull origin master pull the master branch code of the remote repository origin to the local

clone

clone remoteResp

REMOTERESP represents the address of the remote repository, in addition to initializing a project locally, and then pushing to the remote repository, you can also git clone clone an existing project from the remote repository to local by command, and when the clone project from the remote repository is local, the default is on the Master branch. and will automatically add a remote Repository named Origin, which is the address of the remote repository at clone.

By default, only the master branch is from the remote repository clone, and if you want to switch to a different branch, you can use the git checkout -b newBranchName origin/newBranchName command to checkout a remote branch after cloning.

In addition to checkout the remote branch directly, you can create a local branch locally git checkout -b branch01 , and after that, the pull operation will be error-free because the local branch does not have a branch associated with a remote repository, and the next use git branch --set-upstream-to=origin/branch01 branch01 To be able to speak the locally created BRANCH01 branch is associated with the Branch01 branch under origin of the remote repository, and pull operations are now available.

创建分支

git branch newBranchNameeg : git branch laibao

The above command will create a Laibao branch, after you create the branch, you are still in the current branch, if you want to switch to a new branch can be used to switch Branch command

切换分支

git checkout branchNameeg : git checkout laibao

The above command will switch to the Laibao branch, then you can modify the content on the new branch, then commit locally, and then git push origin laibao:laibao push the local Laibao branch to the remote repository by command

For the above two commands can actually be replaced with a command git checkout -b newBranchName , which means to create a new branch, and switch to this branch above

删除分支

push remoteRespName --delete

合并分支

<br/>首先切换到a分支  <br/>然后合并  <br/>还可以通过-m参数指定合并备注<br/><br/>如果合并冲突了,会提示合并冲突,接下来打开冲突的文件,解决冲突,然后commit。就合并完成了。<br/>合并分支时一般加上 --no-ff参数,表示不使用 Fast Forward模式,如果不加这个参数,默认使用的就是Fast Forward模式,导致合并后删除原来的分支后,看不出主分支曾经合并过

存档

list恢复存档git stash apply 存档id删除存档git stash drop 存档id上面的恢复和删除可以用下面的命令合并操作git stash pop 存档id

When working in the current branch for a period of time temporarily need to switch to another branch, but the contents of the current branch cannot be submitted, this time can be used to use the command to log stash archive the current working directory, and then checkout to other branches, and then checkout back to the current branch, and then use log stash listcommand to view the archive list and then use log stash pop 存档id it to develop the previous content.

If the above is not archived first, and does not commit, directly checkout to other branches, there will be a problem, either conflict, or the current changes will be taken to the branch after the checkout.

忽略指定文件/目录

You can add a. gitignore file under the root directory that specifies the files and directories to ignore
Such as:

.idea/*target.settings/*.classpath.project.DS_StorelogsMakefile*.iml*/log/**.dat

别名 alias

If some of the commands are too long, it is cumbersome to write, you can alias some operations, such as git commit can be used instead of the git ci better, then you can set the alias for the commit, set the method for git config --global alias.ci "commit" , then all can be used git ci instead of git commit , if added- Global indicates that the current user is valid, the configured alias is saved in the ~/.gitconfig file, and if no –global parameter is present, it is only valid for the current warehouse and is saved in the./.git/config file. You can edit the configuration file directly to add aliases.

Add some useful aliases:

git config--Global alias. unstage' Reset HEAD '//"Git unstage xx.txt" equivalent to "git Reset HEAD xx.txt" commandgit config--Global alias, c+' Commit 'git config--Global alias. BR' branch 'git config--Global alias, C-' Checkout 'git config--Global alias. LOGX' Log--color--graph--pretty=format: '%cred%h%creset-%c(yellow)%d%creset %s %cgreen(%CR)%c(Bold blue) <%an>%creset'--abbrev-commit '//This is the Ox x, after the configuration, you can try the "git logx" command

Git Utility Commands Summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.