Home >Development Tools >git >The Git usage skills summarized by Daniel are so practical

The Git usage skills summarized by Daniel are so practical

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼forward
2019-10-21 17:59:203542browse

The Git usage skills summarized by Daniel are so practical

What is Git

Official words: Git is a free and open source distributed version control system designed to quickly and efficiently handle tasks ranging from small to large Everything from large-scale projects.

It can automatically help me record every file change, and also allows colleagues to collaborate on editing, so I don’t have to manage a bunch of similar files myself, and I don’t need to pass files around. If you want to see a certain change, you only need to take a look in the software.

Why you should learn Git

Be asked in interviews. Can handle interviews.

Many companies use Git to handle projects during development. If you don’t learn it now, you will definitely have to learn it later.

In my opinion, Git is something that all programmers today must master, and it must be used to develop projects with colleagues in the future. Being proficient in Git commands can improve the efficiency of development.

Install Git

Windows

Download directly from the official website. After the download is complete, right-click on a file and if there is Git Bash Here, the installation will be successful. After installation, you also need to enter on the command line:

$git config --global user.name"你的名字"
$git config --global user.email"你的邮箱"

global means global. All Git repositories on this machine will use this configuration. Allow individual repositories to use other names and email addresses.

Mac

Mac can also be installed like Windows by following the steps above.

You can also install Xcode directly from the AppStore. Xcode integrates Git, but it is not installed by default. You need to run Xcode, select the menu "Xcode"->"Preferences", and find "Downloads" in the pop-up window. Select "Command Line Tools" and click "Install" to complete the installation.

Warehouse

The Git usage skills summarized by Daniel are so practical

The local warehouse is for the remote warehouse. Local warehouse = workspace version area.

The workspace is a collection of files on the disk.

The version area (version library) is the .git file.

Repository = staging area (stage) branch (master) pointer Head.

Take the git command I use most frequently, which is submitting to github, as an example.

git init Originally the local warehouse only contained the workspace, which is the most common working state. At this time, git init means that a .git file is created in the local area and the version area is established.

git add . means submitting all files in the workspace to the temporary storage area in the version area.

Of course you can also add them to the staging area one by one through git add ./xxx/.

git commit -m "xxx" Submits all the files in the temporary storage area to the warehouse area, and the temporary storage area is empty.

git remote add origin https://github.com/name/name_cangku.git connects the local warehouse with the remote warehouse.

git push -u origin master Submit the files in the warehouse area to the remote warehouse.

Once submitted, if you do not make any modifications to the workspace, then the workspace is "clean". There will be a message like nothing to commit, working tree clean.

Related recommendations: "php Getting Started Tutorial"

Submit to GitHub

When I was not familiar with git commands before, I To submit a project to GitHub, you pull the files directly from the web page and submit them. A little shameful.

The Git usage skills summarized by Daniel are so practical

git init. Initialization means turning this file into a warehouse that can be managed by Git. After initialization, open the hidden file and you can see that there is a .git file.

git add . A dot after it indicates that all files will be submitted to the temporary storage area.

git add ./readme.md/ means submitting the readme.md file under this file to the temporary storage area.

git commit -m "Do you want to comment on something?" git commit means to submit all the files in the temporary storage area to the local warehouse. -m followed by a comment.

git remote add origin https://github.com/name/name_cangku.git means connecting your local warehouse to the remote warehouse on GitHub. You only need to connect once, and you don't need to use this command when submitting in the future. name is your github name, name_cangku is your warehouse name. Be careful not to miss the .git at the end. Because this is how I came along, and I took many detours. As for how to create a new warehouse on GitHub, there are many tutorials on the Internet, so I won’t go into details here.

git push -u origin master Submits the local warehouse to the remote warehouse. (Last step) Refresh your remote repository to see the files you submitted.

The last thing mentioned is that before git commit -m "", you can repeat git add to the staging area. But git commit will submit all the files you previously stored in the staging area to the local warehouse at once.

Version Backtracking and Forward

提交一个文件,有时候我们会提交很多次,在提交历史中,这样就产生了不同的版本。每次提交,Git会把他们串成一条时间线。如何回溯到我们提交的上一个版本,用git reset --hard + 版本号即可。版本号可以用git log来查看,每一次的版本都会产生不一样的版本号。

回溯之后,git log查看一下发现离我们最近的那个版本已经不见了。但是我还想要前进到最近的版本应该如何?只要git reset --hard + 版本号就行。退一步来讲,虽然我们可以通过git reset --hard + 版本号,靠记住版本号来可以在不同的版本之间来回穿梭。

但是,有时候把版本号弄丢了怎么办?git reflog帮你记录了每一次的命令,这样就可以找到版本号了,这样你又可以通过git reset来版本穿梭了。

撤销

场景1:在工作区时,你修改了一个东西,你想撤销修改,git checkout -- file。廖雪峰老师指出撤销修改就回到和版本库一模一样的状态,即用版本库里的版本替换工作区的版本。

场景2:你修改了一个内容,并且已经git add到暂存区了。想撤销怎么办?回溯版本,git reset --hard + 版本号,再git checkout -- file,替换工作区的版本。

场景3:你修改了一个内容,并且已经git commit到了master。跟场景2一样,版本回溯,再进行撤销。

删除

如果你git add一个文件到暂存区,然后在工作区又把文件删除了,Git会知道你删除了文件。如果你要把版本库里的文件删除,git rm 并且git commit -m "xxx".

如果你误删了工作区的文件,怎么办?使用撤销命令,git checkout --就可以。这再次证明了撤销命令其实就是用版本库里的版本替换工作区的版本,无论工作区是修改还是删除,都可以“一键还原”。

分支

分支,就像平行宇宙,你创建了一个属于你自己的分支,别人看不到,还继续在原来的分支上正常工作,而你在自己的分支上干活,想提交就提交,直到开发完毕后,再一次性合并到原来的分支上,这样,既安全,又不影响别人工作。用 Git 和 Github 提高效率的 10 个技巧!这篇也推荐看下。

创建与合并分支

The Git usage skills summarized by Daniel are so practical

在没有其他分支插进来时,只有一个master主分支。每次你git push -u origin master 提交就是增加一条时间轴,master也会跟着移动。

The Git usage skills summarized by Daniel are so practical

创建一个other的分支,通过other提交,虽然时间轴向前走了,但是主分支master还在原来的位置。

The Git usage skills summarized by Daniel are so practical

理论分析完,看一下命令怎么写。

创建分支other,切换到other分支。

gitbranch other
gitcheckout other

查看当前所有分支

gitbranch
* othermaster

当前的分支会有一个*

用other提交

gitadd ./xxx/
git commit -m"xxx"

other分支完成,切换回master

git checkoutmaster

此时,master分支上并没有other的文件,因为分支还没有合并。

合并分支

gitmergeother

合并完成之后,就可以在master分支上查看到文件了。

删除other分支

gitbranch -d other

我由此想到,在以后工作中,应该是一个开放小组共同开发一个项目,组长会创建很多分支,每一个分支可以交给一个人去开发某一个功能,一个小组共同开发而且不会相互干扰。谁的功能完成了,可以由组长合并一下完成了的分支。哦,完美!

解决合并分支问题

The Git usage skills summarized by Daniel are so practical

Suppose there is such a situation, the branch other has been committed, but at this time the pointer points back to the master, and the master is not merged, but submitted by git add / commit. In this way, a conflict occurs, and the content of the master file of the main branch is different from the content of other branches. Can't merge! So, modify the contents of the file to make it consistent.

git add git commit commit.

The branches are merged.

The Git usage skills summarized by Daniel are so practical

git log --graph View the branch merge graph

git branch -d other Delete the branch and the task ends.

Branch management strategy

git merge --no-ff other Disable Fast forward mode, because using Fast forward mode, branch history information will be lost after deleting a branch. Super detailed Git practical tutorial, even a fool can understand it at a glance!

BUG branch

Every bug in the work can be fixed through a new temporary branch. After repair, merge the branches, and then delete the temporary branch. But if you have a branch at work, your superior will ask you to fix the bugs in another branch.

You need to save the branch you are working on now, use git stash to "store" the current work site, and continue working after restoring it later. After you solve the bug, git checkout other and return to your own branch. Use git stash list to see where the work you just "stashed" has gone.

At this point you have to resume work:

git stash apply restores but does not delete the stash content, git stash drop deletes the stash content.

When git stash pop is restored, the stash content is also deleted.

At this time, if you use git stash list to view it, you can't see any stash content.

Summary: When fixing a bug, we will fix it by creating a new bug branch, then merge it, and finally delete it; when the work at hand is not completed, first git stash the work site and then repair it. After fixing the bug, git stash pop and return to the work site.

Delete branch

git branch -d branch may fail to be deleted because Git will protect branches that have not been merged.

git branch -D branch forcibly deletes and discards branches that have not been merged.

Multiple Collaboration

git remote View the information of the remote library, origin will be displayed, the default name of the remote warehouse is origin

git remote -v displays more detailed information

git push -u origin master pushes the master branch to the origin remote warehouse.

git push -u origin other Push other to the origin remote warehouse.

Get the branch

The Git usage skills summarized by Daniel are so practical

When the conflict in the above picture occurs,

git pull pulls the latest commit from Fetch it from the remote warehouse, merge it locally, and resolve conflicts. When performing git pull

If git pull also fails, you must also specify the link between branches. Git will remind you what to do in this step. Then git pull again.

The working mode of multi-person collaboration is usually as follows:

First, you can try to use git push origin

to push your own changes;

If the push fails , because the remote branch is newer than your local one, you need to use git pull to try to merge it first;

If there is a conflict in the merge, resolve the conflict and submit it locally;

There is no conflict or resolve it After the conflict, use git push origin

to push successfully!

If git pull prompts no tracking information, it means that the link relationship between the local branch and the remote branch has not been created. Use the command git branch --set-upstream-to origin/.

Rebase

git rebase "organizes" the forked commit history into a straight line, which looks more intuitive. The disadvantage is that the local forked commit has been modified. .

Finally perform git push -u origin master

The purpose of rebase is to make it easier for us to view changes in historical submissions, because forked submissions require three-way comparison.

tag management

For example, if an APP is to be launched online, a tag (tag) is usually added to the version library. In this way, the tagged version is determined. Whenever in the future, retrieving a tagged version means taking out the historical version at that tagged moment. Therefore, a tag is also a snapshot of the repository.

Although the Git tag is a snapshot of the repository, it is actually a pointer to a commit.

tag is actually a meaningful name that is easy to remember, and it is tied to a certain commit. For example, tag v2.1 refers to a historical version called v2.1

Create tag

Steps:

git branch to view the current branch, git checkout master switch to the master branch.

git tag Tag, default is HEAD. For example, git tag v1.0

The default tag is placed on the latest commit. If you want to label a previous commit, use git log to find the commit id of the historical commit.

If a commt id is du2n2d9, execute git tag v1.0 du2n2d9 to label this version v1.0.

git tag View all tags, you can know that the historical versions of tag

tags are not listed in chronological order, but in alphabetical order.

git show View tag information.

git tag -a -m "", creates a tag with a description. -a specifies the label name, and -m specifies the description text. Use show to view the instructions.

Operation tag

git tag -d v1.0 Delete tag. Because the created tags are only stored locally and will not be automatically pushed to the remote. Therefore, mistyped tags can be safely deleted locally.

git push origin Push a tag to the remote

git push origin --tags Push all local tags that have not been pushed to the remote at one time

If the tag Push to remote. git tag -d v1.0 first deletes the local tag v1.0. git push origin :refs/tags/v1.0Delete remote tag v1.0

Customize Git

git config --global color.ui true to let Git display The color will make the command output look more eye-catching

Ignore special files Create a .gitignore file and fill in the file names that need to be ignored. Git will automatically ignore these files. I have also encountered such a problem in my studies. For example, the node_modules file can be ignored.

Ignore file principle: Ignore files automatically generated by the operating system, such as thumbnails, etc.; ignore intermediate files, executable files, etc. generated by compilation. That is, if a file is automatically generated by another file, then There is no need to put automatically generated files into the repository, such as .class files generated by Java compilation; ignore your own configuration files with sensitive information, such as configuration files that store passwords.

Force submission of ignored files. git add -f

git check-ignore -v Check why Git ignores the file.

Assign aliases to Git commands. This is a bit awkward. When you want to enter git rebase in the future, you give it a "nickname" and call it git nb. In the future you can use git nb instead of git rebase.

Summary of commonly used Git commands

git config --global user.name "your name" lets all your Git repositories bind your name

git config --global user.email "your email" Let all your Git repositories bind your email

git init initialize your repository

git add . Put the workspace Submit all the files in the workspace to the temporary storage area

git add .// Submit the files in the workspace to the temporary storage area

git commit -m "xxx" Submit all the files in the temporary storage area to the warehouse area, and the temporary storage area is empty

git remote add origin https://github.com/name/name_cangku.git Connect the local warehouse to the remote warehouse

git push -u origin master Submit the main branch master in the warehouse area to the remote warehouse

git push -u origin Submit other branches to the remote warehouse

git status View the status of the current warehouse

git diff View the specific content of file modifications

git log Displays the commit history from the most recent to the farthest

git clone warehouse address Download the clone file

git reset --hard The version number goes back to the version. The version number follows the master when committing

git reflog displays the command history

git checkout - - Undo the command and replace the files in the workspace with the files in the repository. I feel like the ctrl z

git rm in the Git world deletes the files in the repository

git branch Views all current branches

git branch Create Branch

git checkout Switch to branch

git merge Merge branch

git branch -d Deleting a branch may fail because Git will protect branches that have not been merged

git branch -D Forcibly delete and discard branches that have not been merged

git log --graph View branch merge graph

git merge --no-ff Disable Fast forward mode when merging branches, because this mode will lose branch history information

git stash When other tasks are inserted, "store" the current work site, and continue working after recovery later

git stash list Check where the work you just "stored" has gone

git stash apply restores but does not delete the stash content

git stash drop deletes the stash content

git stash pop deletes the stash content while restoring

git remote view The information of the remote library will display origin. The default name of the remote warehouse is origin

git remote -v displays more detailed information

git pull grabs the latest submission from the remote warehouse. Merge locally, contrary to git push

git rebase "organizes" the forked commit history into a straight line, which looks more intuitive

git tag View all tags, you can know the historical version tag

git tag Tag, default is HEAD. For example git tag v1.0

git tag Tag the version number. The version number is the string of letters and numbers next to it when committing.

git show View tag information

git tag -a -m "" Create a tag with a description. -a specifies the tag name, -m specifies the description text

git tag -d deletes the tag

git push origin pushes a tag to the remote

git push origin --tags Push all local tags that have not been pushed to the remote at once

git push origin :refs/tags/ Delete the remote tag

git config --global color.ui true lets Git display colors, which will make the command output look more eye-catching

git add -f Force submission of ignored files

git check-ignore -v Check why Git ignores the file

gitbranch other

The above is the detailed content of The Git usage skills summarized by Daniel are so practical. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:jianshu.com. If there is any infringement, please contact admin@php.cn delete
Previous article:How to download githubNext article:How to download github