Home >Development Tools >git >Detailed introduction to the use of git

Detailed introduction to the use of git

coldplay.xixi
coldplay.xixiforward
2021-04-28 17:07:073445browse

Detailed introduction to the use of git

1、Git概念

1.1. Git库中由三部

Git 仓库就是那个.git 目录,其中存放的是我们所提交的文档索引内容,Git 可基于文档索引内容对其所管理的文档进行内容追踪,从而实现文档的版本控制。.git目录位于工作目

1) 工作目录:用户本地的

2) Index(索引):将工作目录下所有文件(包含子目录)生成快照,存放到一个临时的存储区域,Git 称该区域为

3) 仓库:将索引通过commit命令提交至仓库中,每一次提交都意味着版本在进行一次

推荐(免费):git

1.2. 使用Git时的

1.2.1. Git初始

1) 配置使用git仓库的人

git config --global user.name "Your Name Comes He

2) 配置使用git仓库的人员em

git config --global user.email you@yourdomain.example.

1.2.2. Git文档忽

工作目录中有一些文件是不希望接受Git 管理的,譬如程序编译时生成的中间文件等等。Git 提供了文档忽略机制,可以将工作目录中不希望接受Git 管理的文档信息写到同一目录下的.gitignore 文

例如:工作目录下有个zh目录,如果不想把它加入到Git管理中,则

echo “zh” > .gitign

git ad

有关gitignore 文件的诸多细节知识可阅读其使用手册:man gitign

1.3. Git与Repo

Git操作一般对应一个仓库,而Repo操作一般对应一个项目,即一个项目会由若干仓库

例如,在操作整个Recket项目时使用Repo,而操作其中的某个仓库时使用Git。在包含隐藏目录.git的目录下执行git

2. Gi

Git help 获取git基

(如果要知道某个特定命令的使用方法,例如:使用Git help clone,来获取git clone的使用

3. Git本地

3.1. Git i

或者使用git init-

创建一个空的Git库。在当前目录中产生一个.git 的子目录。以后,所有的文件变化信息都会保存到这个目录下,而不像CVS那样,会在每个目录和子目录下都创建一个CVS

在.git目录下有一个config文件,可以修改其中的配置

3.2. Git 

将当前工作目录中更改或者新增的文件加入到Git的索引中,加入到Git的索引中就表示记入了版本历史中,这也是提交之前所需要执行的

可以递归添加,即如果后面跟的是一个目录作为参数,则会递归添加整个目录中的所有子目录和文件。

git add dir1 ( 添加dir1这个目录,目录下的所有文件都被加

Git add f1 f2 ( 添加f1,f2

git add . ( 添加当前目录下的所有文件和子目

3.3. Git

从当前的工作目录中和索引中删除

可以递归删除,即如果后面跟的是一个目录做为参数,则会递归删除整个目录中的所有子目录和文件。

git rm –r * (Enter a directory and execute this statement to delete all files and sub-directories in the directory.

git rm f1 (Delete file f1, including this file in the local directory and index. File

git rm --ached f1 (Deleting file f1 will not delete the local directory files, only delete the file records in the index; remove the files that have been git added to the cache, so that they will not be deleted when committing. Submitting this file is suitable for situations where you have added a lot of files at once, but want to exclude a few of them

3.4. Git com

Submit changes to the current working directory

Directly call the git commit command and you will be prompted to fill in the comments. Fill in the commit comments on the command line as follows: git commit -m "Initial commit of gittutor repostory". Note that, unlike CVS, the git commit comments must not be empty. , otherwise it will be submitted

git commit also has a -a parameter, which can forcibly submit changes that are not identified by git add, but it is not recommended to use this

Every time Submit, git will create a unique commit identification code for the global code. The user can restore to any commit through the git reset command

git commit –-amend –m “message” (in a commit id Continuously modify the submitted

3.5. Git sta

Check the status of the repository. You can know which files have changed, which files have not been added to the git repository, etc. It is recommended that each time This command must be used to confirm the library before committing.

The most common misoperation is to modify a file and directly call the commit operation without calling git add to notify the git library that the file has changed, thus causing the file to change. There is no real submission. At this time, if the developer thinks that the file has been submitted and continues to modify or even delete the file, then the modified content will not be managed through the version. If you use git status to check it every time before submitting, You can find this kind of error. Therefore, if you call the git status command, you must pay special attention to those files with the prompt "Changed but not updated:". These files have changed compared with the last commit, but have not Use the

3.6. Git

identified by git add to view the historical log, including each version change. Each version change corresponds to a commit

Git log

-1 means to display only one commit. If you want to display 5 commits, use -5. If not specified, git log will start from the commit and go back.

Git log --stat –summary (display the details of each version

In the project log information, the header of each log line (that is, the string of characters) is the name for the version update submission. We can understand this name as the project version number. The project version number should be unique and is automatically generated by Git by default to mark a certain update of the project. .If we use the project version number as a parameter of the git-show command, we can view the update details of the project version.

1) Git

2) Git

Actually, the above command is not a real version number customization, it just creates a tag object. This is used when releasing the project version to the outside world.

3.7. Git me

put the server The downloaded code is merged with the local code. Or branch

For example: Currently on the master branch, if you want to merge the branch dev into master, then git merge

Note: git merge nov/eclair_eocket (it is to merge the server git Merge the eclair_eocket branch of the library into the local branch

git rebase nov/eclair_eocket (maps the eclair_eocket branch of the server git library to a local temporary branch, and then merges the changes on the local branch into this temporary branch, Then use this temporary branch to initialize the local

3.8. Git d

Compare the local code with the code in the index, or compare the code in the index with the code in the local warehouse

1) Git d

Compare the working directory and

in the Index

2) Git diff - - cac

Compare the index with the local warehouse

3.9. Git check

3.9.1. Switch

1) Create a new branch and switch to the

Git checkout –b new

2) Switch to an already established local branch local_bra

Git checkout local_bra

(After using cat .git/HEAD, refs:refs/heads/ local_bran

3 is displayed) Switch to a branch on the server remote_bra

Git checkout remote_bra

(Remote branch remote_branch can be switched to a commit through git branch –r

4)

Git checkout commit

(after using cat .git/HEAD , display commit_

5) Switch to a

Git checkout

(after using cat .git/HEAD, display t


Note: Except for 1) and 2), the other three are just switched to a temporary (no branch) state (this head is detached). At this time, you can use git branch to see that it is in (no branch) On, cat .git/HEAD sees pointing to the corresponding commit id. This (no branch) only exists temporarily and is not a truly established branch. If 2) is executed at this time, this (no branch) will disappear automatically; if 1) is executed, a new branch new branch will be created and this (no branch) will be attached to this new branch. At this time, cat .git/ refs/heads/new_branch You can see that it points to the commit just now


3.9.2. Initialize with an existing branch


Execute the following command, while switching to an established local branch or a remote branch or a commit id or a tag, create a new branch new_branch and hang it on this new branch


1) Switch to an already established local branch local_branch, and use this branch to initialize a new branch new_bran


git checkout –b new_branch local_bra


2) Switch to a remote branch remote_branch, and use this branch to initialize a new branch new_bran


Git checkout –b new_branch remote_bra


3) Switch to a commit id and create a new branch new_bra


Git checkout –b new_branch commit


4) Switch to a certain tag and create a new branch new_bra


Git checkout –b new_branch


3.9.3. Also


For example “git checkout app/model/ user.rb" will update the user.rb file back from the last submitted version, and all the contents of the uncommitted working directory will be




3.10. Git-ls


Check what

is in the current git library

3.11. Git


Rename a file, directory or


For example: Git mv helloworld.c helloworld1.c (rename the file helloworld.c to helloworld1


3.12. Git bra


3.12.1.


#The cost of creating a branch in the git repository is almost zero, so there is no need to be stingy about creating a few more branches. When The first time you execute git init, the system will create a branch named "master". Other branches are created manually


Here are some common branches


Create a personal working branch of your own to avoid too much interference with the main branch master and to facilitate communication with others


When doing high-risk work, create an experimental


#When merging other people's work, it is best to create a temporary branch to merge into , after the merge is completed, "fetch" to your own


to add, delete, check, etc.


## Note: Branch information is generally in the .git/refs/ directory, where the heads directory is the local branch, remotes is the branch on the corresponding server, and the tags are


##3.12.2 . Check


git branch lists all branches in the local git library. Among the listed branches, if there is * before the branch name, it means that this branch is the current


git branch -r lists all


of the server git library (you can continue to use the command "git checkout -b local Branch name server branch name" to get the code text of a certain branch on the server


3.12.3. Check which


cat .git/H


3.12.4. Create a


1) git branch


Although the branch is created, the current working branch will not be switched to the newly created branch. Therefore, the command "git checkout branch name" is also needed to


2) git checkout –b


Not only creates a branch, but also switches the current working branch to the branch


3.12.5. Switch to a branch: git checkout


Switch to the main branch: git checkout mas


3.12.6. Delete


git branch –D


Note: After deletion, all changes that occurred on the branch cannot be restored. Force deletion of this


3.12.7. Compare files on two branches


git diff master branch name ( Compare the master branch to another branch's

##3.12.8. View the branch

git-show-branch ( View the commit comments and

of the current branch

git-show-branch -all (View commit comments and information of all branches)


* [dev]


! [master]



* [dev]


* [dev^]


* [dev~2]


* [master]


In the above example, the two lines above "--" indicate that there are two branches dev and master, and the last submitted log on the dev branch is "d2", the master branch The last committed log was "m2". The lines under "--" represent the history of branch evolution, where dev represents the last commit that occurred on the dev branch, and dev^ represents the penultimate commit that occurred on the dev branch. dev~2 represents the third to last operation that occurred on the dev branch


3.12.9. View the operations of the current branch


git whatchan


3.12.10. 合




git merge "Comment" The merged target branch is merged


If there is a conflict in the merge, git will have


For example: git checkout master (switch to master


git merge HEAD dev~2 (merge master branch and dev~2 branch) or: git merge master de




##git pull The merged target branch is merged


For example: git checkout master (switch to master


##git pull . dev~2 (merge the current branch and dev~2


3.13. Git reb


is generally used when merging the latest content of the server to the local, for example: in version C When I get the content from the server to the local and modify the local content, I want to submit the locally modified content to the server; but I find that the version on the server has changed to G. At this time, I need to perform Git rebase first to rebase the server. The latest version on is merged into the local one.


It will be clearer to explain with the following two pictures. After the rebase command is executed, the branch point is actually moved from C to G , so that the branch also has the



##3.14. Git

In addition to resetting some abandoned R&D code, the reversal and recovery of the library also plays an important role. For example, we clone a code library remotely. After local development, it is ready to be submitted back to the remote. However, when the local code base is developed, there are functional commits, and there are commits for backup purposes, etc. In short, there are a lot of useless logs in the commit logs, and we do not want to keep these logs. When submitting back to the remote, it is also submitted to the library. Therefore, git res

The concept of git reset is more complicated. Its command form: git reset [- -mixed | --soft | --hard] [

of the command

--mixed This is the default option. For example, git reset [--mixed] dev^ (for the definition of dev^, please refer to 2.6.5). Its function is only to reset the branch status to dev1^, but it does not change the contents of any working files. That is, all file changes from dev1^ to dev1 are retained, but all commit logs between dev1^ and dev1 are cleared, and the changed file contents are not identified by git add, if you want to re-commit , you also need to do a git add on the changed files. In this way, after committing, you will get a very clean commit record. (Reverting back the


in the index and warehouse--soft is equivalent to doing git reset –mixed, and then doing git add on the changed files. If you use If you select this option, you can commit directly. (If you roll back the


--hard command in the warehouse, all information will be rolled back, including the file content. Generally, it is only used when resetting abandoned code. After execution, the file content cannot be restored. (The


## in the working directory, index and warehouse are rolled back.



#Switch to the used split


git reset HEAD^ roll back to the first


git reset HEAD~2 Roll back the second


If you want to roll back the files in the working directory, use git reset - - hard HEAD^ Go back to the first


git reset - - hard HEAD~2 Go back to the second


You can also use the following


to completely roll back the current working directory to the specified version number. Assume that as shown below, we have five submitted versions from A to G, among which The version number of C is bbaf6fb5060b4875b18ff9ff637ce118256d6f20. We executed 'git reset bbaf6fb5060b4875b18ff9ff637ce118256d6f20'. Then there were only three submissions A-C left.




##3.15. Git r


#Restore a certain modification to the version, for example: git revert commit_id (commit_id is generated when the code is committed) A unique word representing


For example: (in 3.6) git revert dfb02e6e4f2f7b573337763e5c0013802e392818 (Perform this operation to restore the last commit's


3.16. Git con


#Use this command to add and change various Git settings, such as "git config branch.master.remote origin" Set the master's remote repository to an alias called origin version


3.17. Git s


displays the differences in objects


3.18. Git


Create, list, delete, or verify a tag object (signed with GPG


You can tag a specific version, so you don’t need to remember the complex version number hash value string. For example, you can use "git tag revert_version bbaf6fb5060b4875b18ff9ff637ce118256d6f20" To mark the version you restored, then when you want to view the version in the future, you can use the revert_version tag name instead of the hash




4. Git server operation command (similar to


4.1. Git cl


Retrieve the code of the server's warehouse to the directory created locally (with the server


Get the remote git library through git clone Afterwards, the developer information in .git/config will not be cloned together. Developer information still needs to be added to the .git/config file of the local library. In addition, developers also need to add .gitignore

themselves.

The remote git library obtained through git clone only contains the current working branch of the remote git library. If you want to obtain other branch information, you need to use "git branch -r" to view it. If If you need to get other remote branch codes, you can use the command "git checkout -b local branch name remote branch name", where the remote branch name is "git branch -r" and the branch name listed is usually such as " origin/branch name". If the local branch name already exists, you do not need "-b"






##4.2. Git


Get the code from the server's repository, and Local code merge. (Interact with the server and download the latest code from the server, which is equivalent to: Git fetch Git mer


from other repositories (either remote or It can be local) to update the code locally, for example: "git pull origin master" is to update the code of the origin repository to the local master


git pull You can get the contents of a certain branch from any git library. Usage


##git pull username@ipaddr: remote repository name remote branch name local branch name. This command Get the remote branch name from the remote git library to a local branch of the local git library. If you do not write the local branch name, it will be pulled to the local current

# by default. ##It should be noted that git pull can also be used to merge branches. It has the same effect as git merge. Therefore, if your local branch already has content, git pull will merge these files. If there are conflicts,













##4


Update the local commit code to the remote repository, such as "git push origin" will update the local code to the remote version named orgin


git push and git pull are exactly the opposite, they are to submit the contents of a local branch to a remote branch. Usage: git pushusername@ipaddr: remote repository name local branch name remote branch name. This command pushes a local branch of the local git library to a remote branch of the remote git library


It should be noted that git push does not seem to automatically merge files . Therefore, if a conflict occurs during git push, it will be forcibly overwritten by the content of the file pushed later without any prompt. This is very dangerous when developing collaboratively





##4.4. Git


Download the code from the server's repository. (Interacting with the server, downloading the latest


from the server is equivalent to getting the latest version from the remote to the local, it will not automatically merge, and is safer than Git pull


Use this method to get the


For example: If you use git checkout nov/eclair_rocket (nov/eclair_rocket is the branch name), it is to obtain the code downloaded from the server when the git fetch command was last used; if you use git fetch first, and then use git checkout nov/eclair_rocket, you will first obtain the latest update information from the server, and then download it from the server. Download the latest code...) h:.




##:. . :. ) l:. . )e mutual). . g. w. g))t本:record record record record;:))):]. t. :e)) Branch: 2). Branch: branch d record. 2012-22:) History) Farewell. Name branch r name. Name, name branch D on. . . branch. . . ;;:. Described h). v. es. codeghdhh. h. . branch. )gg)dd)hh)hh name has upper branch t. d. f. f))v. e. wg:). 1. g. . s). . . . t))):. m))):. d. . . t command) order lp. . Compare e.e:. Make ml" name setting item...;.Into...)h:.




:..:.)l:..)e mutual)..g.w.g))t本:record的record;:))):].t.:e )) Branch: 2). Branch: branch d record. 2012-22:) History) alias. Name branch r name. Name, name branch D on... Branch...;;:. Description h).v. es. Code ghdhh. h.. branch.) gg) dd) hh) hh name upper branch t. d. f. f)) v. e.wg:). 1. g.. s). ..t))):.m))):.d..t command) Let lp..Compared with e.e:.Make ml" name setting item. . ;. become. . . ) h:.




##:. . :. ) l:. . )e mutual). . g. w. g))t本:record record record record;:))):]. t. :e)) Branch: 2). Branch: branch d record. 2012-22:) History) Farewell. Name branch r name. Name, name branch D on. . . branch. . . ;;:. Described h). v. es. codeghdhh. h. . branch. )gg)dd)hh)hh name has upper branch t. d. f. f))v. e. wg:). 1. g. . s). . . . t))):. m))):. d. . . t command) order lp. . Compare e.e:. Make ml" name setting item...;.Into...)h:.




:..:.)l:..)e mutual)..g.w.g))t本:record的record;:))):].t.:e )) Branch: 2). Branch: branch d record. 2012-22:) History) alias. Name branch r name. Name, name branch D on... Branch...;;:. Description h).v. es. Code ghdhh. h.. branch.) gg) dd) hh) hh name upper branch t. d. f. f)) v. e.wg:). 1. g.. s). ..t))):.m))):.d..t command) Let lp..Compared with e.e:.Make ml" name setting item. . ;. become. . . ) h:.



##:. . :. ) l:. . )e mutual). . g. w. g))t本:record record record record;:))):]. t. :e)) Branch: 2). Branch: branch d record. 2012-22:) History) Farewell. Name branch r name. Name, name branch D on. . . branch. . . ;;:. Described h). v. es. codeghdhh. h. . branch. )gg)dd)hh)hh name has upper branch t. d. f. f))v. e. wg:). 1. g. . s). . . . t))):. m))):. d. . . t command) order lp. . Compare e.e:. Make ml" name setting item...;.Into...)h:.



:。。:。)l:。。)e互)。。g。w。g))t本:录录录录;:))):]。t。:e))支:2)。支:支d录。2012-22:)史)别。名支r名。名,名支D上。。。支。。。;;:。述h)。v。es。码ghdhh。h。。支。)gg)dd)hh)hh名上支t。d。f。f))v。e。wg:)。1。g。。s)。。。。t))):。m))):。d。。。t命令)令lp。。较e.e:。制ml"名置项。。;。成。。。)h:。




:。。:。)l:。。)e互)。。g。w。g))t本:录录录录;:))):]。t。:e))支:2)。支:支d录。2012-22:)史)别。名支r名。名,名支D上。。。支。。。;;:。述h)。v。es。码ghdhh。h。。支。)gg)dd)hh)hh名上支t。d。f。f))v。e。wg:)。1。g。。s)。。。。t))):。m))):。d。。。t命令)令lp。。较e.e:。制ml"名置项。。;。成。。。)h:。




:。。:。)l:。。)e互)。。g。w。g))t本:录录录录;:))):]。t。:e))支:2)。支:支d录。2012-22:)史)别。名支r名。名,名支D上。。。支。。。;;:。述h)。v。es。码ghdhh。h。。支。)gg)dd)hh)hh名上支t。d。f。f))v。e。wg:)。1。g。。s)。。。。t))):。m))):。d。。。t命令)令lp。。较e.e:。制ml"名置项。。;。成。。。)h:。




:。。:。)l:。。)e互)。。g。w。g))t本:录录录录;:))):]。t。:e))支:2)。支:支d录。2012-22:)史)别。名支r名。名,名支D上。。。支。。。;;:。述h)。v。es。码ghdhh。h。。支。)gg)dd)hh)hh名上支t。d。f。f))v。e。wg:)。1。g。。s)。。。。t))):。m))):。d。。。t命令)令lp。。较e.e:。制ml"名置项。。;。成Git

1.1. Git库中由三部分组

Git 仓库就是那个.git 目录,其中存放的是我们所提交的文档索引内容,Git 可基于文档索引内容对其所管理的文档进行内容追踪,从而实现文档的版本控制。.git目录位于工作目录内

1) 工作目录:用户本地的目录

2) Index(索引):将工作目录下所有文件(包含子目录)生成快照,存放到一个临时的存储区域,Git 称该区域为索引

3) 仓库:将索引通过commit命令提交至仓库中,每一次提交都意味着版本在进行一次更新


1.2. 使用Git时的初始化事

1.2.1. Git初始化配

1) 配置使用git仓库的人员姓

git config --global user.name "Your Name Comes Here

2) 配置使用git仓库的人员emai

git config --global user.email you@yourdomain.example.co

1.2.2. Git文档忽略机

工作目录中有一些文件是不希望接受Git 管理的,譬如程序编译时生成的中间文件等等。Git 提供了文档忽略机制,可以将工作目录中不希望接受Git 管理的文档信息写到同一目录下的.gitignore 文件中

例如:工作目录下有个zh目录,如果不想把它加入到Git管理中,则执行

echo “zh” > .gitignor

git add 

有关gitignore 文件的诸多细节知识可阅读其使用手册:man gitignor

1.3. Comparison between Git and Repo

Git operations generally correspond to one warehouse, while Repo operations generally correspond to one project, that is, a project will be composed of several warehouses

For example, when operating the entire Repo is used when working on the Recket project, and Git is used when operating one of the warehouses. Perform git operations in the directory containing the hidden directory .git


2. Git he

Git help Get basic git commands

( If you want to know how to use a specific command, for example: use Git help clone to get how to use git clone


3. Basic Git local operations

3.1. Git ini

Or use git init-db

to create an empty Git library. Create a .git subdirectory in the current directory. After that, all file change information will be saved in this directory, unlike CVS, which will create a CVS directory in each directory and subdirectory

There is a config file in the .git directory, and the configuration information in it can be modified

3.2. Git ad

Add the changed or newly added files in the current working directory to the Git index. Adding them to the Git index means that they are recorded in the version history. This is also a commit. The step

that needs to be performed before can be added recursively, that is, if it is followed by a directory as a parameter, all subdirectories and files in the entire directory will be added recursively. For example,

git add dir1 (Add the directory dir1, all files in the directory are added

Git add f1 f2 (Add f1, f2 files

git add. (Add all files and subdirectories in the current directory

3.3. Git r

Delete files from the current working directory and index

Can be deleted recursively, that is, if it is followed by a directory as a parameter, then All subdirectories and files in the entire directory will be deleted recursively. For example,

git rm –r * (enter a directory and execute this statement, all files and subdirectories in the directory will be deleted

git rm f1 (Delete file f1, including the file record in the local directory and index

git rm --ached f1 (Delete file f1, the local directory file will not be deleted, only the file in the index will be deleted Record; remove the file that has been git added to the cache, so that the file will not be submitted when committing. It is suitable for situations where a lot of files are added at once, but you want to exclude a few of them.

3.4. Git commit

Submit the modifications to the current working directory

Directly call the git commit command and you will be prompted to fill in the comments. Fill in the commit comments on the command line as follows: git commit -m " Initial commit of gittutor repostory". Note that unlike CVS, the git submission comment must not be empty, otherwise the submission will fail.

git commit also has a -a parameter, which can forcefully submit changes that are not identified by git add. , but it is not recommended to use this method

Every time a submission is made, git will create a unique commit identification code for the global code. The user can restore the code to any submission through the git reset command

git commit –-amend –m “message” (Continuously modify the submitted content on a commit id

3.5. Git statu

View the status of the repository. You can know which files Changes have occurred, which files have not been added to the git library, etc. It is recommended to confirm the library status through this command before each commit

The most common misoperation is to modify a file and directly call the commit operation without calling git add to notify the git library that the file has changed, resulting in the file not being actually submitted. At this time, if the developer thinks that the file has been submitted and continues to modify or even delete the file, then the modified content will not be version managed. If you use git status to check every time before committing, you can find this error. Therefore, if you call the git status command, you must pay special attention to those files with a prompt of "Changed but not updated:". These files are all files that have changed compared with the last commit, but are not identified by git add

3.6. Git lo

View the historical log, including each version change. Each version change corresponds to a commit id

Git log -

-1 means that only one commit is displayed. If you want to display 5 commits, use -5. If not specified, git log will display from the commit onwards

Git log --stat –summary (display detailed changes of each version

In the project log information, each log The first line of (that is, the string of characters) is the name for the version update submission. We can understand this name as the project version number. The project version number should be unique and is automatically generated by Git by default to mark a certain part of the project. An update. If we use the project version number as a parameter of the git-show command, we can view the update details of the project version. For example

1) Git lo


2) Git sho


In fact, the above command does not really customize the version number, it just creates a tag object. This is in progress It is more useful when the project version is released to the outside world

3.7. Git merg

Merges the code downloaded from the server with the local code. Or perform branch merging

For example: Currently on the master branch, if you want to merge the branch dev into master, then git merge de

Note: git merge nov/eclair_eocket (is to merge Merge the eclair_eocket branch of the server git library into the local branch

git rebase nov/eclair_eocket (maps the eclair_eocket branch of the server git library to a local temporary branch, and then merges the changes on the local branch into this Temporary branch, and then use this temporary branch to initialize the local branch

3.8. Git dif

Compare the local code with the code in the index, or compare the code in the index with the local warehouse Compare the code in

1) Git dif

Compare the code in the working directory and Index

2) Git diff - - cache

Compare index And the code in the local warehouse

3.9. Git checkou

3.9.1. Switch to the branch

1) Create a new branch and switch to the branch

Git checkout –b new branch

2) Switch to an already established local branch local_branc

Git checkout local_branc

(after using cat .git/HEAD , display refs:refs/heads/ local_branch

3) Switch to a branch on the server remote_branc

Git checkout remote_branc

(The remote branch remote_branch can be accessed through git branch – r List

4) Switch to a commit i

Git checkout commit_i

(after using cat .git/HEAD, commit_id

5 is displayed ) Switch to a certain ta

Git checkout ta

(after using cat .git/HEAD, tag

is displayed

Note: Except for 1) and 2), the other three are just switched to a temporary (no branch) state (this head is detached). At this time, you can use git branch to see that it is on (no branch). cat .git/HEAD sees that it points to the corresponding commit id. This (no branch) only exists temporarily and is not a truly established branch. If 2) is executed at this time, this (no branch) will disappear automatically; if 1) is executed, a new branch new branch will be created and this (no branch) will be attached to this new branch. At this time, cat .git/ refs/heads/new_branch You can see that it has pointed to the commit id just now

3.9.2. Initialize the new branch with the existing branch

Execute the following command, and switch to an already established branch At the same time as the local branch or a remote branch or a commit id or a tag, create a new branch new_branch and hang it on this new branch

1) Switch to an already established local branch local_branch , and use this branch to initialize a new branch new_branch

git checkout –b new_branch local_branc

2) Switch to a remote branch remote_branch, and use this branch to initialize a new branch new_branch

Git checkout –b new_branch remote_branc

3) Switch to a commit id and create a new branch new_branc

Git checkout –b new_branch commit_i

4) Switch Go to a certain tag and create a new branch new_branc

Git checkout –b new_branch ta

3.9.3. Restore the code

For example, “git checkout app/model/user. rb" will update the user.rb file back from the last submitted version, and all the contents in the uncommitted working directory will be overwritten


3.10. Git -ls-fil

View the files in the current git library

3.11. Git m

Rename a file, directory or link

For example : Git mv helloworld.c helloworld1.c (Rename the file helloworld.c to helloworld1.c

3.12. Git branc

3.12.1. Total

in git version The cost of creating a branch in the library is almost zero, so don't be stingy about creating a few more branches. When git init is executed for the first time, the system will create a branch named "master". Other branches are created manually

Here are some common branch strategies

Create your own personal working branch to avoid too much interference with the main branch master, and to facilitate communication with Communicate and collaborate with others

When doing high-risk work, create an experimental branch

When merging other people’s work, it is best to create a temporary branch for merging, and the merge is completed Then "fetch" to your own branch

Perform operations such as adding, deleting, and checking branches

Note: Branch information is generally in the .git/refs/ directory, among which the heads directory is Local branches, remotes are branches on the corresponding server, tags are tags

3.12.2. View branches

git branch lists all branches in the local git library. Among the listed branches, if there is * before the branch name, it means that this branch is the current branch

git branch –r lists all branches of the server git library

(You can continue to use the command " git checkout -b local branch name server branch name" to get the code file of a branch on the server)

3.12.3. Check which branch you are currently on

cat .git/HEA

3.12.4. Create a branch

1) git branch branch

Although the branch is created, the current working branch will not be switched to the newly created branch. Therefore, You also need the command "git checkout branch name" to switch

2) git checkout –b branch

Not only created a branch, but also switched the current working branch to the branch

3.12.5. Switch to a branch: git checkout Branch

Switch to the main branch: git checkout maste

3.12.6. Delete branch

git branch –D branch

Note: After deletion, all changes that occurred in the branch cannot be recovered. Force deletion of this branch

3.12.7. Compare the files on the two branches

git diff master branch name (compare the difference between the master branch and another branch

3.12.8. View the branch history

git-show-branch (View the commit comments and information of the current branch

git-show-branch -all (View the commit comments and information of all branches) For example

* [dev] d

! [master] m

-

* [dev] d

* [dev^] d

* [dev~2] d

* [master] m

In the above example, the two lines above "--" indicate that there are two branches dev and master, and the last submitted log on the dev branch is "d2", and the last submitted log on the master branch is "m2". The lines under "--" represent the history of branch evolution, where dev indicates that it occurred in The last commit on the dev branch, dev^ represents the second to last commit that occurred on the dev branch. dev~2 represents the third to last commit that occurred on the dev branch

3.12.9. View the current Branch operation notes

git whatchange

3.12.10. Merge points

Method 1

git merge “Comment” The source of the merged target branch

If there is a conflict in the merge, git will prompt

For example: git checkout master (switch to the master branch

git merge HEAD dev~2 (merge the master branch and dev ~2 branches) or: git merge master dev~

Method 2

git pull merged target branch merged source branch

For example: git checkout master (switch to master Branch

git pull . dev~2 (merge the current branch and dev~2 branch

3.13. Git rebas

is generally used when merging the latest content of the server to the local, For example: in version C, the content is obtained from the server to the local, and the local content is modified. At this time, I want to submit the locally modified content to the server; but I find that the version on the server has changed to G. At this time, I need to execute it first. Git rebase merges the latest version on the server into the local one. For example,

will be explained more clearly with the following two pictures. After the rebase command is executed, the branch point is actually moved from C to G, so that the branch It also has the function from C to G


##3.14. Git rese

The reversal and recovery of the library is not only used for some abandoned R&D codes. In addition to reset, there is another important function. For example, we clone a code base remotely, develop it locally, and prepare to submit it back to the remote. However, when the local code base is developed, there are functional commits, commits for backup purposes, etc. In short, there are a lot of useless logs in the commit log, and we don't want to submit these logs to the library when they are submitted back to the remote. Therefore, it is necessary to use git reset

The concept of git reset is relatively complicated. Its command form: git reset [--mixed | --soft | --hard] [

Command options

--mixed This is the default option. For example, git reset [--mixed] dev^ (for the definition of dev^, please refer to 2.6.5). Its function is only to reset the branch status to dev1^, but it does not change the contents of any working files. That is, all file changes from dev1^ to dev1 are retained, but all commit logs between dev1^ and dev1 are cleared, and the changed file contents are not identified by git add, if you want to re-commit , you also need to do a git add on the changed files. In this way, after committing, you will get a very clean commit record. (Rolling back the contents of the index and warehouse

--soft is equivalent to doing git reset –mixed, and then doing git add on the changed files. If you use this option, you can commit directly (Rolling back the contents of the warehouse

--hard This command will cause all information to be rolled back, including file content. Generally, it is only used when resetting abandoned code. After execution, The file contents cannot be restored. (The contents in the working directory, index and warehouse are rolled back

For example

Switch to the branch used

git reset HEAD^ Roll back the first record

git reset HEAD~2 Roll back the second record

If you want to roll back the files in the working directory, use git reset - - hard HEAD^ Roll back the first record

git reset - - hard HEAD~2 Roll back the second record

You can also use the following method

to completely restore the current working directory Scroll to the specified version number. Assume as shown below that we have five versions submitted by A-G. The version number of C is bbaf6fb5060b4875b18ff9ff637ce118256d6f20. We executed 'git reset bbaf6fb5060b4875b18ff9ff637ce118256d6f20', then only the three submitted versions A-C are left.


3.15. Git rever

Restore a certain modification to the version, for example: git revert commit_id (where commit_id is a unique representation generated when committing the code String

For example: (in 3.6) git revert dfb02e6e4f2f7b573337763e5c0013802e392818 (Perform this operation to restore the last commit operation

3.16. Git confi

Use this command to re- Add and change various Git settings, such as "git config branch.master.remote origin" to set the master's remote repository as an alias called origin repository

3.17. Git sho

Display different types of objects

3.18. Git ta

Create, list, delete or verify a tag object (signed with GPG)

You can convert a specific Tag the version so that you don’t need to remember the complex version number hash value string. For example, you can use "git tag revert_version bbaf6fb5060b4875b18ff9ff637ce118256d6f20" to mark the version you restored. Then when you want to view the version in the future, just You can use the revert_version tag name instead of the hash value


4. Git server operation command (contact the server

4.1. Git clon

Retrieve the code of the server's warehouse to a locally created directory (interacting with the server)

After obtaining the remote git library through git clone, the developer information in .git/config will not be cloned together. . You still need to add developer information to the .git/config file of the local library. In addition, developers also need to add the . gitignore file themselves.

The remote git library obtained through git clone only contains the current working branch of the remote git library. If you want to get other branch information, you need to use "git branch -r" to view it. If you need to get other remote branch codes, you can use the command "git checkout -b local branch name remote branch name", where the remote branch The branch name listed in "git branch -r" is usually something like "origin/branch name". If the local branch name already exists, the "-b" parameter is not required

For example


4.2. Git pul

Get the code from the server's warehouse and merge it with the local code. (Interacting with the server and downloading the latest code from the server is equivalent to: Git fetch Git merge

Updates the code to the local from other repositories (either remote or local), for example: "git pull origin master" is to update the code of the origin repository to the local master branch

git pull can obtain the contents of a certain branch from any git library. The usage is as follows

git pull username@ipaddr: remote repository name remote branch name local branch name. This command will get the remote branch name of the remote git library to a local branch of the local git library. Among them, if the local branch is not written name, the default is to pull to the local current branch

It should be noted that git pull can also be used to merge branches. It has the same effect as git merge. Therefore, if your local branch already has content, git pull These files will be merged and an alarm will be issued if there is a conflict

For example






##4.3. Git push

Update the local commit code to the remote repository. For example, "git push origin" will update the local commit code to the remote repository. The code is updated to the remote repository named orgin

git push and git pull are exactly the opposite, they are to submit the contents of a local branch to a remote branch. Usage: git pushusername@ipaddr :Remote repository name local branch name remote branch name. This command will push a local branch of the local git library to the remote branch name of the remote git library

It should be noted that git push It seems that files will not be automatically merged. Therefore, if a conflict occurs during git push, it will be forcibly overwritten by the content of the file after the push, and there will be no prompt. This is very dangerous during cooperative development

For example


4.4. Git fetc

Download the code from the server's repository. (Interact with the server and download the latest code from the server

Equivalent to getting the latest version from the remote to the local, it will not automatically merge, and is safer than Git pull

Use this method to get updates on the server

For example: if you use git checkout nov /eclair_rocket (nov/eclair_rocket is the branch name on the server), is to obtain the code downloaded from the server when the git fetch command was last used; if git fetch is used first, and then git checkout nov/eclair_rocket is used, the code is downloaded from the server first Get the latest update information from the server, and then download the latest code from the server. . . ) h:. . .



:. . :. ) l:. . )e mutual). . g. w. g))t本:record record record record;:))):]. t. :e)) Branch: 2). Branch: branch d record. 2012-22:) History) Farewell. Name branch r name. Name, name branch D on. . . branch. . . ;;:. Described h). v. es. codeghdhh. h. . branch. )gg)dd)hh)hh name has upper branch t. d. f. f))v. e. wg:). 1. g. . s). . . . t))):. m))):. d. . . t command) order lp. . Compare e.e:. Make ml" name setting item...;. Become a concept....................Update All codes fetc, for example, the name of the task in the library. 3. Git push, for example, the alarm branch is as follows: branch ge pul, for example, the parameter file is exchanged on the server (the value is exchanged) ta type ho library fi operator string ever version method. Note each Each record, for example, content content content option h>etrese function such as as branch branch source division method two v~ branch prompt source division method together divided ge record commit m d dd

- m d例如信息支历区别的区分支分支除分te分支支上分支切换分支个分EA分支件)分支分支看分标签操作分支分支协作策略创建 总nc.c链接 m文件-fil覆盖原代tanc_incncchncch支上新分idagtataid_i i列出ncncchncnc分支分支到分ou代码he代码if比较if分支支上de合并rg比较有用 sholo例如变化显示 -idlo文件状态tu内容代码方式失败内容mi况.记录目录例如文件 r录 文件入 例如一步ad信息目录dbni操作基本方法本命t he操作组成的比ord or执行件中略机coaire员姓化配初始化事更新索引目录录内分组Git概念

1.1. Git库中由三部分组

Git 仓库就是那个.git 目录,其中存放的是我们所提交的文档索引内容,Git 可基于文档索引内容对其所管理的文档进行内容追踪,从而实现文档的版本控制。.git目录位于工作目录内

1) 工作目录:用户本地的目录

2) Index(索引):将工作目录下所有文件(包含子目录)生成快照,存放到一个临时的存储区域,Git 称该区域为索引

3) 仓库:将索引通过commit命令提交至仓库中,每一次提交都意味着版本在进行一次更新


1.2. 使用Git时的初始化事

1.2.1. Git初始化配

1) 配置使用git仓库的人员姓

git config --global user.name "Your Name Comes Here

2) 配置使用git仓库的人员emai

git config --global user.email you@yourdomain.example.co

1.2.2. Git文档忽略机

工作目录中有一些文件是不希望接受Git 管理的,譬如程序编译时生成的中间文件等等。Git 提供了文档忽略机制,可以将工作目录中不希望接受Git 管理的文档信息写到同一目录下的.gitignore 文件中

例如:工作目录下有个zh目录,如果不想把它加入到Git管理中,则执行

echo “zh” > .gitignor

git add 

有关gitignore 文件的诸多细节知识可阅读其使用手册:man gitignor

1.3. Comparison between Git and Repo

Git operations generally correspond to one warehouse, while Repo operations generally correspond to one project, that is, a project will be composed of several warehouses

For example, when operating the entire Repo is used when working on the Recket project, and Git is used when operating one of the warehouses. Perform git operations in the directory containing the hidden directory .git


2. Git he

Git help Get basic git commands

( If you want to know how to use a specific command, for example: use Git help clone to get how to use git clone


3. Basic Git local operations

3.1. Git ini

Or use git init-db

to create an empty Git library. Create a .git subdirectory in the current directory. After that, all file change information will be saved in this directory, unlike CVS, which will create a CVS directory in each directory and subdirectory

There is a config file in the .git directory, and the configuration information in it can be modified

3.2. Git ad

Add the changed or newly added files in the current working directory to the Git index. Adding them to the Git index means that they are recorded in the version history. This is also a commit. The step

that needs to be performed before can be added recursively, that is, if it is followed by a directory as a parameter, all subdirectories and files in the entire directory will be added recursively. For example,

git add dir1 (Add the directory dir1, all files in the directory are added

Git add f1 f2 (Add f1, f2 files

git add. (Add all files and subdirectories in the current directory

3.3. Git r

Delete files from the current working directory and index

Can be deleted recursively, that is, if it is followed by a directory as a parameter, then All subdirectories and files in the entire directory will be deleted recursively. For example,

git rm –r * (enter a directory and execute this statement, all files and subdirectories in the directory will be deleted

git rm f1 (Delete file f1, including the file record in the local directory and index

git rm --ached f1 (Delete file f1, the local directory file will not be deleted, only the file in the index will be deleted Record; remove the file that has been git added to the cache, so that the file will not be submitted when committing. It is suitable for situations where a lot of files are added at once, but you want to exclude a few of them.

3.4. Git commit

Submit the modifications to the current working directory

Directly call the git commit command and you will be prompted to fill in the comments. Fill in the commit comments on the command line as follows: git commit -m " Initial commit of gittutor repostory". Note that unlike CVS, the git submission comment must not be empty, otherwise the submission will fail.

git commit also has a -a parameter, which can forcefully submit changes that are not identified by git add. , but it is not recommended to use this method

Every time a submission is made, git will create a unique commit identification code for the global code. The user can restore the code to any submission through the git reset command

git commit –-amend –m “message” (Continuously modify the submitted content on a commit id

3.5. Git statu

View the status of the repository. You can know which files Changes have occurred, which files have not been added to the git library, etc. It is recommended to confirm the library status through this command before each commit

The most common misoperation is to modify a file and directly call the commit operation without calling git add to notify the git library that the file has changed, resulting in the file not being actually submitted. At this time, if the developer thinks that the file has been submitted and continues to modify or even delete the file, then the modified content will not be version managed. If you use git status to check every time before committing, you can find this error. Therefore, if you call the git status command, you must pay special attention to those files with a prompt of "Changed but not updated:". These files are all files that have changed compared with the last commit, but are not identified by git add

3.6. Git lo

View the historical log, including each version change. Each version change corresponds to a commit id

Git log -

-1 means that only one commit is displayed. If you want to display 5 commits, use -5. If not specified, git log will display from the commit onwards

Git log --stat –summary (display detailed changes of each version

In the project log information, each log The first line of (that is, the string of characters) is the name for the version update submission. We can understand this name as the project version number. The project version number should be unique and is automatically generated by Git by default to mark a certain part of the project. An update. If we use the project version number as a parameter of the git-show command, we can view the update details of the project version. For example

1) Git lo


2) Git sho


In fact, the above command does not really customize the version number, it just creates a tag object. This is in progress It is more useful when the project version is released to the outside world

3.7. Git merg

Merges the code downloaded from the server with the local code. Or perform branch merging

For example: Currently on the master branch, if you want to merge the branch dev into master, then git merge de

Note: git merge nov/eclair_eocket (is to merge Merge the eclair_eocket branch of the server git library into the local branch

git rebase nov/eclair_eocket (maps the eclair_eocket branch of the server git library to a local temporary branch, and then merges the changes on the local branch into this Temporary branch, and then use this temporary branch to initialize the local branch

3.8. Git dif

Compare the local code with the code in the index, or compare the code in the index with the local warehouse Compare the code in

1) Git dif

Compare the code in the working directory and Index

2) Git diff - - cache

Compare index And the code in the local warehouse

3.9. Git checkou

3.9.1. Switch to the branch

1) Create a new branch and switch to the branch

Git checkout –b new branch

2) Switch to an already established local branch local_branc

Git checkout local_branc

(after using cat .git/HEAD , display refs:refs/heads/ local_branch

3) Switch to a branch on the server remote_branc

Git checkout remote_branc

(The remote branch remote_branch can be accessed through git branch – r List

4) Switch to a commit i

Git checkout commit_i

(after using cat .git/HEAD, commit_id

5 is displayed ) Switch to a certain ta

Git checkout ta

(after using cat .git/HEAD, tag

is displayed

Note: Except for 1) and 2), the other three are just switched to a temporary (no branch) state (this head is detached). At this time, you can use git branch to see that it is on (no branch). cat .git/HEAD sees that it points to the corresponding commit id. This (no branch) only exists temporarily and is not a truly established branch. If 2) is executed at this time, this (no branch) will disappear automatically; if 1) is executed, a new branch new branch will be created and this (no branch) will be attached to this new branch. At this time, cat .git/ refs/heads/new_branch You can see that it has pointed to the commit id just now

3.9.2. Initialize the new branch with the existing branch

Execute the following command, and switch to an already established branch At the same time as the local branch or a remote branch or a commit id or a tag, create a new branch new_branch and hang it on this new branch

1) Switch to an already established local branch local_branch , and use this branch to initialize a new branch new_branch

git checkout –b new_branch local_branc

2) Switch to a remote branch remote_branch, and use this branch to initialize a new branch new_branch

Git checkout –b new_branch remote_branc

3) Switch to a commit id and create a new branch new_branc

Git checkout –b new_branch commit_i

4) Switch Go to a certain tag and create a new branch new_branc

Git checkout –b new_branch ta

3.9.3. Restore the code

For example, “git checkout app/model/user. rb" will update the user.rb file back from the last submitted version, and all the contents in the uncommitted working directory will be overwritten


3.10. Git -ls-fil

View the files in the current git library

3.11. Git m

Rename a file, directory or link

For example : Git mv helloworld.c helloworld1.c (Rename the file helloworld.c to helloworld1.c

3.12. Git branc

3.12.1. Total

in git version The cost of creating a branch in the library is almost zero, so don't be stingy about creating a few more branches. When git init is executed for the first time, the system will create a branch named "master". Other branches are created manually

Here are some common branch strategies

Create your own personal working branch to avoid too much interference with the main branch master, and to facilitate communication with Communicate and collaborate with others

When doing high-risk work, create an experimental branch

When merging other people’s work, it is best to create a temporary branch for merging, and the merge is completed Then "fetch" to your own branch

Perform operations such as adding, deleting, and checking branches

Note: Branch information is generally in the .git/refs/ directory, among which the heads directory is Local branches, remotes are branches on the corresponding server, tags are tags

3.12.2. View branches

git branch lists all branches in the local git library. Among the listed branches, if there is * before the branch name, it means that this branch is the current branch

git branch –r lists all branches of the server git library

(You can continue to use the command " git checkout -b local branch name server branch name" to get the code file of a branch on the server)

3.12.3. Check which branch you are currently on

cat .git/HEA

3.12.4. Create a branch

1) git branch branch

Although the branch is created, the current working branch will not be switched to the newly created branch. Therefore, You also need the command "git checkout branch name" to switch

2) git checkout –b branch

Not only created a branch, but also switched the current working branch to the branch

3.12.5. Switch to a branch: git checkout Branch

Switch to the main branch: git checkout maste

3.12.6. Delete branch

git branch –D branch

Note: After deletion, all changes that occurred in the branch cannot be recovered. Force deletion of this branch

3.12.7. Compare the files on the two branches

git diff master branch name (compare the difference between the master branch and another branch

3.12.8. View the branch history

git-show-branch (View the commit comments and information of the current branch

git-show-branch -all (View the commit comments and information of all branches) For example

* [dev] d

! [master] m

-

* [dev] d

* [dev^] d

* [dev~2] d

* [master] m

In the above example, the two lines above "--" indicate that there are two branches dev and master, and the last submitted log on the dev branch is "d2", and the last submitted log on the master branch is "m2". The lines under "--" represent the history of branch evolution, where dev indicates that it occurred in The last commit on the dev branch, dev^ represents the second to last commit that occurred on the dev branch. dev~2 represents the third to last commit that occurred on the dev branch

3.12.9. View the current Branch operation notes

git whatchange

3.12.10. Merge points

Method 1

git merge “Comment” The source of the merged target branch

If there is a conflict in the merge, git will prompt

For example: git checkout master (switch to the master branch

git merge HEAD dev~2 (merge the master branch and dev ~2 branches) or: git merge master dev~

Method 2

git pull merged target branch merged source branch

For example: git checkout master (switch to master Branch

git pull . dev~2 (merge the current branch and dev~2 branch

3.13. Git rebas

is generally used when merging the latest content of the server to the local, For example: in version C, the content is obtained from the server to the local, and the local content is modified. At this time, I want to submit the locally modified content to the server; but I find that the version on the server has changed to G. At this time, I need to execute it first. Git rebase merges the latest version on the server into the local one. For example,

will be explained more clearly with the following two pictures. After the rebase command is executed, the branch point is actually moved from C to G, so that the branch It also has the function from C to G


##3.14. Git rese

The reversal and recovery of the library is not only used for some abandoned R&D codes. In addition to reset, there is another important function. For example, we clone a code base remotely, develop it locally, and prepare to submit it back to the remote. However, when the local code base is developed, there are functional commits, commits for backup purposes, etc. In short, there are a lot of useless logs in the commit log, and we don't want to submit these logs to the library when they are submitted back to the remote. Therefore, it is necessary to use git reset

The concept of git reset is relatively complicated. Its command form: git reset [--mixed | --soft | --hard] [

Command options

--mixed This is the default option. For example, git reset [--mixed] dev^ (for the definition of dev^, please refer to 2.6.5). Its function is only to reset the branch status to dev1^, but it does not change the contents of any working files. That is, all file changes from dev1^ to dev1 are retained, but all commit logs between dev1^ and dev1 are cleared, and the changed file contents are not identified by git add, if you want to re-commit , you also need to do a git add on the changed files. In this way, after committing, you will get a very clean commit record. (Rolling back the contents of the index and warehouse

--soft is equivalent to doing git reset –mixed, and then doing git add on the changed files. If you use this option, you can commit directly (Rolling back the contents of the warehouse

--hard This command will cause all information to be rolled back, including file content. Generally, it is only used when resetting abandoned code. After execution, The file contents cannot be restored. (The contents in the working directory, index and warehouse are rolled back

For example

Switch to the branch used

git reset HEAD^ Roll back the first record

git reset HEAD~2 Roll back the second record

If you want to roll back the files in the working directory, use git reset - - hard HEAD^ Roll back the first record

git reset - - hard HEAD~2 Roll back the second record

You can also use the following method

to completely restore the current working directory Scroll to the specified version number. Assume as shown below that we have five versions submitted by A-G. The version number of C is bbaf6fb5060b4875b18ff9ff637ce118256d6f20. We executed 'git reset bbaf6fb5060b4875b18ff9ff637ce118256d6f20', then only the three submitted versions A-C are left.


3.15. Git rever

Restore a certain modification to the version, for example: git revert commit_id (where commit_id is a unique representation generated when committing the code String

For example: (in 3.6) git revert dfb02e6e4f2f7b573337763e5c0013802e392818 (Perform this operation to restore the last commit operation

3.16. Git confi

Use this command to re- Add and change various Git settings, such as "git config branch.master.remote origin" to set the master's remote repository as an alias called origin repository

3.17. Git sho

Display different types of objects

3.18. Git ta

Create, list, delete or verify a tag object (signed with GPG)

You can convert a specific Tag the version so that you don’t need to remember the complex version number hash value string. For example, you can use "git tag revert_version bbaf6fb5060b4875b18ff9ff637ce118256d6f20" to mark the version you restored. Then when you want to view the version in the future, just You can use the revert_version tag name instead of the hash value


4. Git server operation command (contact the server

4.1. Git clon

Retrieve the code of the server's warehouse to a locally created directory (interacting with the server)

After obtaining the remote git library through git clone, the developer information in .git/config will not be cloned together. . You still need to add developer information to the .git/config file of the local library. In addition, developers also need to add the . gitignore file themselves.

The remote git library obtained through git clone only contains the current working branch of the remote git library. If you want to get other branch information, you need to use "git branch -r" to view it. If you need to get other remote branch codes, you can use the command "git checkout -b local branch name remote branch name", where the remote branch The branch name listed in "git branch -r" is usually something like "origin/branch name". If the local branch name already exists, the "-b" parameter is not required

For example


4.2. Git pul

从服务器的仓库中获取代码,和本地代码合并。(与服务器交互,从服务器上下载最新代码,等同于: Git fetch + Git merge

从其它的版本库(既可以是远程的也可以是本地的)将代码更新到本地,例如:“git pull origin master ”就是将origin这个版本库的代码更新到本地的master主分支

git pull可以从任意一个git库获取某个分支的内容。用法如下

git pull  username@ipaddr:远端repository名远端分支名 本地分支名。这条命令将从远端git库的远端分支名获取到本地git库的一个本地分支中。其中,如果不写本地分支名,则默认pull到本地当前分支

需要注意的是,git pull也可以用来合并分支。 和git merge的作用相同。 因此,如果你的本地分支已经有内容,则git pull会合并这些文件,如果有冲突会报警

例如






4.3. Git push

将本地commit的代码更新到远程版本库中,例如 “git push origin”就会将本地的代码更新到名为orgin的远程版本库中

git push和git pull正好想反,是将本地某个分支的内容提交到远端某个分支上。用法: git pushusername@ipaddr:远端repository名本地分支名 远端分支名。这条命令将本地git库的一个本地分支push到远端git库的远端分支名中

需要格外注意的是,git push好像不会自动合并文件。因此,如果git push时,发生了冲突,就会被后push的文件内容强行覆盖,而且没有什么提示。 这在合作开发时是很危险的事情

例如


4.4. Git fetc

从服务器的仓库中下载代码。(与服务器交互,从服务器上下载最新代码

相当于从远程获取最新版本到本地,不会自动merge,比Git pull更安全些

使用此方法来获取服务器上的更新

For example: If you use git checkout nov/eclair_rocket (nov/eclair_rocket is the branch name on the server), you will get the code downloaded from the server when you last used the git fetch command; if you use git fetch first, then use git Checkout nov/eclair_rocket first obtains the latest update information from the server, and then downloads the latest code from the server. . . ) h:. . .



:. . :. ) l:. . )e mutual). . g. w. g))t本:record record record record;:))):]. t. :e)) Branch: 2). Branch: branch d record. 2012-22:) History) Farewell. Name branch r name. Name, name branch D on. . . branch. . . ;;:. Described h). v. es. codeghdhh. h. . branch. )gg)dd)hh)hh name has upper branch t. d. f. f))v. e. wg:). 1. g. . s). . . . t))):. m))):. d. . . t command) order lp. . Compare e.e:. Make ml" name setting item ...;. into...) h:...



:...:.) l:...) e mutual)..g.w.g))tthis:record recordrecord;:))):].t.:e))branch:2).branch:branch drecord.2012-22: ) History) Differentiation. Name branch r name. Name, name branch D on... Branch...;;:. Description h). v. es. Code ghdhh. h.. Branch.) gg) dd) hh) hh name upper branch t.d.f.f))v.e.wg:).1.g..s)...t))):.m))):.d.. .t command) command lp..Compared with e.e:.make ml"name setting item. . ;. become. . . ) h:. . .



:. . :. ) l:. . )e mutual). . g. w. g))t本:record record record record;:))):]. t. :e)) Branch: 2). Branch: branch d record. 2012-22:) History) Farewell. Name branch r name. Name, name branch D on. . . branch. . . ;;:. Described h). v. es. codeghdhh. h. . branch. )gg)dd)hh)hh name has upper branch t. d. f. f))v. e. wg:). 1. g. . s). . . . t))):. m))):. d. . . t command) order lp. . Compare e.e:. Make ml" name setting item ...;. into...) h:...



:...:.) l:...) e mutual)..g.w.g))tthis:record recordrecord;:))):].t.:e))branch:2).branch:branch drecord.2012-22: ) History) Differentiation. Name branch r name. Name, name branch D on... Branch...;;:. Description h). v. es. Code ghdhh. h.. Branch.) gg) dd) hh) hh name upper branch t.d.f.f))v.e.wg:).1.g..s)...t))):.m))):.d.. .t command) command lp..Compared with e.e:.make ml"name setting item. . ;. become. . . ) h:. . .



:. . :. ) l:. . )e mutual). . g. w. g))t本:record record record record;:))):]. t. :e)) Branch: 2). Branch: branch d record. 2012-22:) History) Farewell. Name branch r name. Name, name branch D on. . . branch. . . ;;:. Described h). v. es. codeghdhh. h. . branch. )gg)dd)hh)hh name has upper branch t. d. f. f))v. e. wg:). 1. g. . s). . . . t))):. m))):. d. . . t command) order lp. . Compare e.e:. Make ml" name setting item ...;. into...) h:...



:...:.) l:...) e mutual)..g.w.g))tthis:record recordrecord;:))):].t.:e))branch:2).branch:branch drecord.2012-22: ) History) Differentiation. Name branch r name. Name, name branch D on... Branch...;;:. Description h). v. es. Code ghdhh. h.. Branch.) gg) dd) hh) hh name upper branch t.d.f.f))v.e.wg:).1.g..s)...t))):.m))):.d.. .t command) command lp..Compared with e.e:.make ml"name setting item. . ;. become. . . ) h:. . .



:. . :. ) l:. . )e mutual). . g. w. g))t本:record record record record;:))):]. t. :e)) Branch: 2). Branch: branch d record. 2012-22:) History) Farewell. Name branch r name. Name, name branch D on. . . branch. . . ;;:. Described h). v. es. codeghdhh. h. . branch. )gg)dd)hh)hh name has upper branch t. d. f. f))v. e. wg:). 1. g. . s). . . . t))):. m))):. d. . . t command) order lp. . Compare e.e:. Make ml" name setting item...;.Become...) h:...



:. . :. ) l:. . )e mutual). . g. w. g))t本:record record record record;:))):]. t. :e)) Branch: 2). Branch: branch d record. 2012-22:) History) Farewell. Name branch r name. Name, name branch D on. . . branch. . . ;;:. Described h). v. es. codeghdhh. h. . branch. )gg)dd)hh)hh name has upper branch t. d. f. f))v. e. wg:). 1. g. . s). . . . t))):. m))):. d. . . t command) order lp. . Compare e.e:. Make ml" name setting item...;.Become...) h:...



:。。:。)l:。。)e互)。。g。w。g))t本:录录录录;:))):]。t。:e))支:2)。支:支d录。2012-22:)史)别。名支r名。名,名支D上。。。支。。。;;:。述h)。v。es。码ghdhh。h。。支。)gg)dd)hh)hh名上支t。d。f。f))v。e。wg:)。1。g。。s)。。。。t))):。m))):。d。。。t命令)令lp。。较e.e:。制ml"名置项。。;。成Git概念
1.1. Git库中由三部分组成
Git 仓库就是那个.git 目录,其中存放的是我们所提交的文档索引内容,Git 可基于文档索引内容对其所管理的文档进行内容追踪,从而实现文档的版本控制。.git目录位于工作目录内。
1) 工作目录:用户本地的目录;
2) Index(索引):将工作目录下所有文件(包含子目录)生成快照,存放到一个临时的存储区域,Git 称该区域为索引。
3) 仓库:将索引通过commit命令提交至仓库中,每一次提交都意味着版本在进行一次更新。
Detailed introduction to the use of git
1.2. 使用Git时的初始化事项
1.2.1. Git初始化配置
1) 配置使用git仓库的人员姓名
git config --global user.name "Your Name Comes Here"
2) 配置使用git仓库的人员email
git config --global user.email you@yourdomain.example.com
1.2.2. Git文档忽略机制
工作目录中有一些文件是不希望接受Git 管理的,譬如程序编译时生成的中间文件等等。Git 提供了文档忽略机制,可以将工作目录中不希望接受Git 管理的文档信息写到同一目录下的.gitignore 文件中。
例如:工作目录下有个zh目录,如果不想把它加入到Git管理中,则执行:
echo “zh” > .gitignore
git add .
有关gitignore 文件的诸多细节知识可阅读其使用手册:man gitignore
1.3. Git与Repo的比较
Git操作一般对应一个仓库,而Repo操作一般对应一个项目,即一个项目会由若干仓库组成。
例如,在操作整个Recket项目时使用Repo,而操作其中的某个仓库时使用Git。在包含隐藏目录.git的目录下执行git操作。

2. Git help
Git help gets basic git commands
(If you want to know how to use a specific command, for example: use Git help clone to get how to use git clone)

3. Basic Git local operation commands
3.1. Git init
Or use git init-db.
Create an empty Git repository. Create a .git subdirectory in the current directory. In the future, all file change information will be saved in this directory, unlike CVS, which creates a CVS directory in each directory and subdirectory.
There is a config file in the .git directory, and the configuration information in it can be modified.
3.2. Git add
Add the changed or newly added files in the current working directory to the Git index. Adding them to the Git index means that they are recorded in the version history. This is also what needs to be executed before submission. step.
Can be added recursively, that is, if it is followed by a directory as a parameter, all subdirectories and files in the entire directory will be added recursively. For example:
git add dir1 (Add the directory dir1, all files in the directory are added)
Git add f1 f2 (Add f1, f2 files)
git add . (Add all files in the current directory files and subdirectories)
3.3. Git rm
Delete files from the current working directory and index.
Can be deleted recursively, that is, if it is followed by a directory as a parameter, all subdirectories and files in the entire directory will be deleted recursively. For example:
git rm –r * (Enter a directory and execute this statement to delete all files and subdirectories in the directory)
git rm f1 (Delete file f1, including the local directory and index. This file record)
git rm --ached f1 (Delete file f1, the local directory file will not be deleted, only the file record in the index will be deleted; the file that has been git added will be removed to the cache, so that it will not be deleted when committing. Submitting this file is suitable for situations where you have added a lot of files at once, but want to exclude a few of them.)
3.4. Git commit
Submit the modifications to the current working directory.
If you directly call the git commit command, you will be prompted to fill in the comments. Fill in the commit comments on the command line as follows: git commit -m "Initial commit of gittutor repostory". Note that unlike CVS, the git commit comment must not be empty, otherwise the commit will fail.
Git commit also has a -a parameter, which can forcibly submit changes that are not identified by git add, but this method is not recommended.
Every time a commit is made, git will create a unique commit identification code for the global code. Users can restore the code to any commit through the git reset command.
git commit –-amend –m “message” (continuously modify the submitted content on a commit id)
3.5. Git status
Check the status of the repository. You can know which files have changed, which files have not been added to the git library, etc. It is recommended to confirm the library status through this command before each commit.
The most common misoperation is to modify a file and directly call the commit operation without calling git add to notify the git library that the file has changed, resulting in that the file is not actually submitted. At this time, if the developer thinks that the file has been submitted and continues to modify or even delete the file, then the modified content will not be version managed. If you use git status to check every time before committing, you can find this error. Therefore, if you call the git status command, you must pay special attention to those files with a prompt of "Changed but not updated:". These files are all files that have changed compared with the last commit, but are not identified by git add.
3.6. Git log
View the historical log, including each version change. Each version change corresponds to a commit id.
Git log -1
-1 means to display only one commit. If you want to display 5 commits, use -5. If not specified, git log will be displayed from the commit onwards.
Git log --stat –summary (show detailed changes of each version)
In the project log information, the first line of each log (that is, the string of characters) is the name of the version update submission. We can understand this naming as the project version number. The project version number should be unique and is automatically generated by Git by default to indicate a certain update of the project. If we use the project version number as a parameter of the git-show command, we can view the update details of the project version. For example:
1) Git log
Detailed introduction to the use of git
2) Git show
Detailed introduction to the use of git
In fact, the above command does not really customize the version number, it just creates a It's just a tag object, which is more useful when releasing project versions to the outside world.
3.7. Git merge
Merge the code downloaded from the server with the local code. Or merge branches.
For example: Currently on the master branch, if you want to merge the branch dev into the master, then git merge dev
Note: git merge nov/eclair_eocket (is to merge the eclair_eocket branch of the server git library into the local branch (Up)
git rebase nov/eclair_eocket (maps the eclair_eocket branch of the server git library to a local temporary branch, then merges the changes on the local branch into this temporary branch, and then uses this temporary branch to initialize the local branch )
3.8. Git diff
Compare the local code with the code in the index, or compare the code in the index with the code in the local warehouse.
1) Git diff
Compare the code in the working directory and Index.
2) Git diff - - cached
Compare the code in the index and the local warehouse.
3.9. Git checkout
3.9.1. Switch to branch
1) Create a new branch and switch to this branch
Git checkout –b new branch name
2) Switch to An established local branch local_branch
Git checkout local_branch
(After using cat .git/HEAD, refs:refs/heads/ local_branch is displayed)
3) Switch to a branch remote_branch on the server
Git checkout remote_branch
(The remote branch remote_branch can be listed through git branch –r)
4) Switch to a certain commit id
Git checkout commit_id
(After using cat .git/HEAD, Display commit_id)
5) Switch to a certain tag
Git checkout tag
(After using cat .git/HEAD, the tag is displayed)
Note: Except for 1) and 2), the other three They just switch to a temporary (no branch) state (this head is detached). At this time, you can use git branch to see that it is on (no branch), and cat .git/HEAD will see that it points to the corresponding commit id. This (no branch) only exists temporarily and is not a truly established branch. If 2) is executed at this time, this (no branch) will disappear automatically; if 1) is executed, a new branch new branch will be created and this (no branch) will be attached to this new branch. At this time, cat .git/ refs/heads/new_branch You can see that it already points to the commit id just now.
3.9.2. Initialize a new branch with an existing branch
Execute the following command to create a new branch while switching to an established local branch or a remote branch or a commit id or a tag. New branch new_branch, and hang on this new branch.
1) Switch to an already established local branch local_branch, and use this branch to initialize a new branch new_branch.
git checkout –b new_branch local_branch
2) Switch to a remote branch remote_branch, and use this branch to initialize a new branch new_branch.
Git checkout –b new_branch remote_branch
3) Switch to a certain commit id and create a new branch new_branch
Git checkout –b new_branch commit_id
4) Switch to a certain tag and create a new branch new_branch
Git checkout –b new_branch tag
3.9.3. Restore code
For example, "git checkout app/model/user.rb" will update the user.rb file from the last submitted version When you come back, all contents in the uncommitted working directory will be overwritten.

3.10. Git-ls-files
Check what files are in the current git library.
3.11. Git mv
Rename a file, directory or link.
For example: Git mv helloworld.c helloworld1.c (rename the file helloworld.c to helloworld1.c)
3.12. Git branch
3.12.1. Overview
In the git repository The cost of creating a branch is almost zero, so don't be stingy about creating a few more branches. When git init is executed for the first time, the system will create a branch named "master". Other branches are created manually.
Here are some common branching strategies:
Create your own personal working branch to avoid too much interference with the main branch master and to facilitate communication and collaboration with others;
When doing high-risk projects When working, create an experimental branch;
When merging other people's work, it is best to create a temporary branch for merging, and then "fetch" to your own branch after the merging is completed.
Perform operations such as adding, deleting, and checking branches.
Note: Branch information is generally in the .git/refs/ directory, where the heads directory is the local branch, remotes is the branch on the corresponding server, and tags is the tag.
3.12.2. View branches
git branch lists all branches in the local git library. Among the listed branches, if there is * before the branch name, it means that this branch is the current branch.
git branch –r lists all branches of the server git library.
(You can continue to use the command "git checkout -b local branch name server branch name" to obtain the code file of a certain branch on the server).
3.12.3. Check which branch you are currently on
cat .git/HEAD
3.12.4. Create a branch
1) git branch branch name
Although the branch is created, it does not The current working branch will be switched to the newly created branch. Therefore, the command "git checkout branch name" is also needed to switch,
2) git checkout -b branch name
Not only creates the branch, but also the current working branch The branch is switched to this branch.
3.12.5. Switch to a branch: git checkout branch name
Switch to the master branch: git checkout master
3.12.6. Delete a branch
git branch –D branch name
Note : After deletion, all changes that occurred on this branch cannot be recovered. Force deletion of this branch.
3.12.7. Compare the differences between files on two branches
git diff master branch name (compare the difference between the master branch and another branch)
3.12.8. View branch history
git- show-branch (View the commit comments and information of the current branch)
git-show-branch -all (View the commit comments and information of all branches) For example:
* [dev] d2
! [master] m2
--
* [dev] d2
* [dev^] d1
* [dev~2] d0
* [master] m2
In the above example, " The two lines above "--" indicate that there are two branches, dev and master, and the last submitted log on the dev branch is "d2", and the last submitted log on the master branch is "m2".The lines under "--" represent the history of branch evolution, where dev represents the last commit that occurred on the dev branch, and dev^ represents the penultimate commit that occurred on the dev branch. dev~2 represents the third to last commit that occurred on the dev branch.
3.12.9. View the operation record of the current branch
git whatchanged
3.12.10. Merge branches
Method 1:
git merge “Comment” The merged target branch merged from the source branch
If there is a conflict in the merge, git will prompt you.
For example: git checkout master (switch to master branch)
git merge HEAD dev~2 (merge master branch and dev~2 branch) or: git merge master dev~2
Method two:
git pull merged target branch merged source branch
For example: git checkout master (switch to master branch)
git pull . dev~2 (merge the current branch and dev~2 branch)
3.13. Git rebase
Generally used when merging the latest content of the server to the local. For example: in version C, the content is obtained from the server to the local, and the local content is modified. At this time, I want to submit the locally modified content to the server; but it is found that the server The version on the server has changed to G. At this time, you need to perform Git rebase to merge the latest version on the server to the local one. For example:
It will be clearer to explain with the following two pictures. After the rebase command is executed, the branch point is actually moved from C to G, so that the branch also has the function from C to G.
Detailed introduction to the use of git[6]
3.14. Git reset
In addition to resetting some abandoned R&D code, the reversal and recovery of the library also plays an important role. For example, we clone a code base remotely, develop it locally, and prepare to submit it back to the remote. However, when the local code base is developed, there are functional commits, commits for backup purposes, etc. In short, there are a lot of useless logs in the commit log, and we don't want to submit these logs to the library when they are submitted back to the remote. Therefore, git reset is used.
The concept of git reset is relatively complex. Its command form: git reset [--mixed | --soft | --hard] []
Command options:
--mixed This is the default option. For example, git reset [--mixed] dev^ (for the definition of dev^, please refer to 2.6.5). Its function is only to reset the branch status to dev1^, but it does not change the contents of any working files. That is, all file changes from dev1^ to dev1 are retained, but all commit logs between dev1^ and dev1 are cleared, and the changed file contents are not identified by git add, if you want to re-commit , you also need to do a git add on the changed files. In this way, after committing, you will get a very clean commit record.(The contents in the index and warehouse are rolled back)
--soft is equivalent to doing git reset –mixed, and then doing git add on the changed files. If this option is used, you can commit directly. (The contents in the warehouse are rolled back)
The --hard command will cause all information to be rolled back, including file contents. It is generally only used when resetting obsolete code. After execution, the file contents cannot be restored. (The contents in the working directory, index and warehouse are rolled back)
For example:
Switch to the branch used;
git reset HEAD^ Roll back the first record
git reset HEAD~2 Roll back the second record
If you want to roll back the files in the working directory, use git reset - - hard HEAD^ to roll back the first record
git reset - - hard HEAD~2 roll back the first record Two records
You can also use the following method:
Completely roll back the current working directory to the specified version number. Assume as shown below, we have versions submitted five times from A to G, among which the version number of C is bbaf6fb5060b4875b18ff9ff637ce118256d6f20, We executed 'git reset bbaf6fb5060b4875b18ff9ff637ce118256d6f20' and the result was that only the three submitted versions A-C were left
Detailed introduction to the use of git[5]
3.15. Git revert
Restore a certain modification to the version, for example: git revert commit_id (Commit_id is a unique string generated when committing code)
For example: (in 3.6) git revert dfb02e6e4f2f7b573337763e5c0013802e392818 (Performing this operation will restore the last commit operation)
3.16. Git config
Use this command to add and change various settings of Git. For example, "git config branch.master.remote origin" will set the master's remote repository to an alias called origin repository.
3.17. Git show
Display different types of objects.
3.18. Git tag
Create, list, delete or verify a tag object (signed with GPG).
You can label a specific version, so you don’t need to remember the complex version number hash value string. For example, you can use "git tag revert_version bbaf6fb5060b4875b18ff9ff637ce118256d6f20" to mark the version you restored, then When you want to check the version in the future, you can use the revert_version tag name instead of the hash value.

4. Git server operation command (interact with the server)
4.1. Git clone
Take out the code of the server's warehouse into a locally created directory (interact with the server)
Get the remote end through git clone After git repository, the developer information in .git/config will not be cloned together. You still need to add developer information to the .git/config file of the local library. In addition, developers also need to add . gitignore files themselves.
The remote git library obtained through git clone only contains the current working branch of the remote git library. If you want to get other branch information, you need to use "git branch -r" to view it. If you need to get other remote branch codes, you can use the command "git checkout -b local branch name remote branch name", where the remote branch The branch name listed in "git branch -r" is usually something like "origin/branch name". If the local branch name already exists, the "-b" parameter is not required.
For example:
Detailed introduction to the use of git[5]
4.2. Git pull
Get the code from the server's repository and merge it with the local code. (Interact with the server and download the latest code from the server, which is equivalent to: Git fetch Git merge)
Update the code to the local from other repositories (either remote or local), for example: "git "pull origin master" is to update the code of the origin repository to the local master branch.
git pull can obtain the contents of a certain branch from any git library. The usage is as follows:
git pull username@ipaddr: remote repository name remote branch name local branch name . This command will get the remote branch name of the remote git repository to a local branch of the local git repository. Among them, if you do not write the local branch name, it will be pulled to the local current branch by default.
It should be noted that git pull can also be used to merge branches. It has the same function as git merge. Therefore, if your local branch already has content, git pull will merge these files and alert you if there are conflicts.
For example:
Detailed introduction to the use of git

Detailed introduction to the use of git

Detailed introduction to the use of git

Detailed introduction to the use of git
4.3. Git push
Update the local commit code to the remote repository. For example, "git push origin" will update the local code To the remote repository named orgin.
Git push and git pull are exactly the opposite, they are to submit the contents of a local branch to a remote branch. Usage: git pushusername@ipaddr: remote repository name local branch name remote branch name . This command pushes a local branch of the local git repository to the remote branch name of the remote git repository.
It should be noted that git push does not seem to automatically merge files. Therefore, if a conflict occurs during git push, it will be forcibly overwritten by the content of the file pushed later without any prompt. This is a very dangerous thing when developing collaboratively.
For example:
Detailed introduction to the use of git
4.4. Git fetch
Download code from the server's repository. (Interact with the server and download the latest code from the server)
It is equivalent to getting the latest version from the remote to the local. It will not automatically merge and is safer than Git pull.
Use this method to get updates on the server.
For example: If you use git checkout nov/eclair_rocket (nov/eclair_rocket is the branch name on the server), you will get the code downloaded from the server when you last used the git fetch command; if you use git fetch first, then use git Checkout nov/eclair_rocket first obtains the latest update information from the server, and then downloads the latest code from the server.

The above is the detailed content of Detailed introduction to the use of git. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete