In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article is to share with you about how to use git. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
What is Git?
Official: Git is a free open source distributed version control system designed to handle everything from small to large projects quickly and efficiently.
It automatically records every file change and allows colleagues to collaborate so that you don't have to manage a bunch of similar files or pass them around. If you want to see a change, just take a peek at it in the software.
Why learn Git?
The interview will be asked. Can handle the interview.
Many company developers use Git to handle projects. If you don't learn now, you will certainly learn more in the future.
In my opinion, Git should be mastered by all programmers nowadays, and must be used in joint development projects with colleagues in the future. Proficient in Git commands can improve the efficiency of development.
Related recommendations:
How to use 1.Git (picture and text + video)
The difference between 2.Git and Github (picture and text + video)
Install Git
Windows
Download it directly on the official website. After the download is complete, you can install it successfully if you right-click any file if there is a Git Bash Here. After installation, enter on the command line:
$git config-- global user.name "your name" $git config-- global user.email "your email"
Global stands for global, and all Git repositories on this machine will use this configuration. Allow a single warehouse to use other names and mailboxes.
Mac
Mac can also be installed like Windows by following the steps above.
You can also install Xcode,Xcode integrated Git directly from AppStore, but there is no installation by default. You need to run Xcode, choose "Xcode"-> "Preferences", find "Downloads" in the pop-up window, choose "Command Line Tools", and click "Install" to complete the installation.
Warehouse
The local warehouse is for the remote warehouse. Local warehouse = work area + version area.
A workspace is a collection of files on disk.
The version area (version library) is the .git file.
Version library = stage + master + pointer Head.
Take the git command that I use most frequently, that is, submit to github as an example.
Git init originally contains only the workspace in the local warehouse, which is the most common working state. At this point, git init indicates that a .git file has been created in the local area, and the version area has been created.
Git add. Indicates that all files in the workspace are submitted to the temporary storage area in the version area.
Of course, you can also add it to the temporary storage area in batches through git add. / xxx/.
Git commit-m "xxx" submits all files from 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 submits the files from the warehouse area to the remote warehouse.
Once submitted, if you haven't made any changes to the workspace, the workspace is "clean". There will be a message like nothing to commit, working tree clean.
Submit to GitHub
When I was not familiar with the git command in the past, I submitted projects to github by pulling files directly from the web page and submitting them. A little shameful.
Git init. Initialization, which means turning this file into a repository that can be managed by Git. Open the hidden file after initialization and you can see that there is a .git file.
Git add. The latter dot indicates that all the files will be submitted to the staging area.
Git add. / readme.md/ means to submit the readme.md file under this file to the staging area.
Git commit-m "what do you want to comment on" git commit means to submit all files in the staging area to the local warehouse. -m followed by comments.
Git remote add origin https://github.com/name/name_cangku.git means to connect your local warehouse with a remote warehouse on GitHub. You only need to connect once, and you don't have to thank this command when you submit it later. Name is your github name and name_cangku is your warehouse name. Be careful not to leave out the .git. Because that's how I walked in front of me, and I took a lot of detours. As for how to build a new warehouse on GitHub, there are many tutorials online, and I won't repeat them here.
Git push-u origin master submits the local warehouse to the remote warehouse. (last step) refresh your remote warehouse and you can see the file you submitted.
Finally, before git commit-m ", you can repeat git add to the staging area. However, git commit will submit all the files you previously stored in the temporary storage area to the local warehouse at once.
Backtracking and advance of the version
To submit a document, sometimes we will submit it many times, in the submission history, which results in different versions. Each time they are submitted, Git strands them into a timeline. How to go back to the last version we submitted, use git reset-- hard + version number. The version number can be viewed using git log, and each version produces a different version number.
After looking back, git log looked at it and found that the version closest to us was gone. But what do I want to move on to the latest version? Just git reset-- hard + version number is fine. To say the least, although we can use the git reset-hard + version number, we can travel back and forth between different versions by remembering the version number.
But what if you lose your version number sometimes? Git reflog records every command for you, so you can find the version number, so you can shuttle versions through git reset again.
Revocation
Scenario 1: when you are in the workspace, you change something and you want to undo the change, git checkout-- file. Teacher Liao Xuefeng pointed out that if you undo the changes, you will return to exactly the same state as the version library, that is, replacing the version of the workspace with the version in the version library.
Scenario 2: you have modified a content and have already git add to the staging area. What if you want to undo? Retrace the version, git reset-- hard + version number, and then git checkout-- file, replace the version of the workspace.
Scenario 3: you have modified a content and have already git commit to master. As in scenario 2, the version is backdated and undone.
Delete
If you git add a file to the staging area and then delete the file in the workspace, Git will know that you deleted the file. If you want to delete files from the version library, git rm and git commit-m "xxx".
What if you delete the files in the workspace by mistake? Use the undo command, git checkout-- that's fine. This proves once again that the undo command actually replaces the version of the workspace with the version in the version library, and can be "restored with one click" regardless of whether the workspace is modified or deleted.
Branch
Branch, like a parallel universe, you create your own branch, others can not see, but also continue to work on the original branch, and you work on your own branch, you want to submit, until the development is completed, and then merge into the original branch at one time, so that it is not only safe, but also does not affect the work of others. 10 tips for improving efficiency with Git and Github! This one is also recommended.
Create and merge branches
When no other branches are plugged in, there is only one master primary branch. Every time you git push-u origin master a submission, you add a timeline, and master moves along with it.
Create a branch of other and submit it through other. Although the timeline has moved forward, the main branch master is still in its original position.
After the theoretical analysis, take a look at how to write the command.
Create a branch other and switch to the other branch.
Gitbranch othergitcheckout other
View all current branches
Gitbranch* othermaster
The current branch will have a *
Submit with other
Gitadd. / xxx/git commit-m "xxx"
Other branch completed, switch back to master
Git checkoutmaster
At this point, there is no other file on the master branch because the branches have not been merged.
Merge branches
Gitmergeother
After the merge is complete, you can view the files on the master branch.
Delete other Branch
Gitbranch-d other
It occurred to me that in the future work, an open team should jointly develop a project, and the team leader will create many branches, each of which can be handed over to a person to develop a certain function, and a team will develop together without interfering with each other. Whose function is completed, the completed branch can be merged by the team leader. Oh, perfect!
Solve the problem of merging branches
Suppose there is a case where the branch other is already commit, but when the pointer points back to master, and master does not merge, git add / commit commits. In this way, there is a conflict, and the content of the main branch master file is different from that of the other branch. It can't be merged! So, modify the contents of the file to make it consistent.
Git add git commit submission.
The branches merged.
Git log-- graph View Branch merge Diagram
Git branch-d other deletes the branch and the task ends.
Branch management strategy
Git merge-- no-ff other disables Fast forward mode because using Fast forward mode, branch history information is lost when a branch is deleted. Ultra-detailed Git practical tutorials, a fool will take a look!
BUG branch
At work, each bug can be repaired by a new temporary branch, which, after repair, is merged, and then the temporary branch is deleted. But if you have a branch at work, your supervisor wants you to change the BUG of another branch.
You need to save the branch that is working now, git stash, "save" the current work site, and continue to work later when it is restored. When you solve BUG, git checkout other goes back to its branch. Use git stash list to find out where the job you just "saved" went.
At this point, you need to get back to work:
Git stash apply restores but does not delete stash content, while git stash drop deletes stash content.
Delete the stash content when git stash pop restores it.
At this point, looking at it with git stash list, you can't see any stash content.
Summary: when repairing bug, we will repair 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, then repair the bug, then git stash pop, and return to the work site.
Delete Branch
It is possible to delete git branch-d + branches because Git protects branches that have not been merged.
Git branch-D + branches are forcibly deleted and unmerged branches are discarded.
Multi-person cooperation
When git remote views the information of the remote library, it will display origin. 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 pushes other to the origin remote warehouse.
Grab branch
When there is a conflict in the picture above
Git pull grabs the latest submissions from the remote repository and merges locally to resolve conflicts. Git pull in progress
If the git pull also fails, specify the link between the branches, and Git will remind you what to do. And then git pull.
The mode of multi-person collaboration is usually like this:
First, you can try to use git push origin
Push your own changes
If the push fails, because the remote branch is newer than your local, you need to use git pull to try to merge first
If there is a conflict in the merge, resolve the conflict and submit it locally
After there is no conflict or after the conflict is resolved, use git push origin
Push will be successful!
If git pull prompts no tracking information, 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 collates the bifurcated submission history into a straight line to make it more intuitive. The disadvantage is that the local bifurcation submission has been modified.
Finally, git push-u origin master is being carried out.
The purpose of rebase is to make it easier for us to view changes in historical submissions, because bifurcated submissions require tripartite comparison.
Label management
For example, when an APP is to be launched, it is usually marked with a tag in the version library, so that the tagged version is determined. Whenever you take the version of a tag in the future, you will take out the historical version of that tagged moment. Therefore, the tag is also a snapshot of the version library.
Although the Git tag is a snapshot of the version library, it is actually a pointer to a commit.
Tag is actually a meaningful name that is easy to remember. It is tied to a commit. For example, tag v2.1 refers to a version of history as v2.1.
Create label
Steps:
Git branch looks at the current branch and git checkout master switches to the master branch.
Git tag is tagged. The default is HEAD. Such as git tag v1.0
The default tag is typed on the most recently submitted commit. If you want to tag the previous commit, ask git log to find the history submitted commit id.
If a commt id is du2n2d9, executing git tag v1.0 du2n2d9 will label this version v1.0.
Git tag looks at all the tags to know the historical version of tag
The labels are not listed in chronological order, but alphabetically.
Git show looks at the tag information.
Git tag-a-m ", create a label with a description. -a specifies the label signature, and-m specifies the description text. You can view the instructions with show.
Operation label
Git tag-d v1.0 removes the tag. Because the tags created are only stored locally, they are not automatically pushed to the remote. Therefore, mistyped labels can be safely deleted locally.
Git push origin pushes a tag to the remote
Git push origin-- tags pushes all local tags that have not been pushed remotely at one time
If the label is pushed remotely. Git tag-d v1.0 removes the local label v1.0 first. Git push origin: refs/tags/v1.0 removes remote tags v1.0
Custom Git
Git config-- global color.ui true lets Git display colors, which makes the command output look more eye-catching
Ignore special files to create a .gitignore file and fill in the file names that need to be ignored. Git automatically ignores these files. I have also encountered such problems in my study, such as node_modules files can be ignored.
Ignore file principle: ignore files automatically generated by the operating system, such as thumbnails, etc.; ignore intermediate files, executable files generated by compilation, etc., that is, if a file is automatically generated through another file, then automatically generated files do not need to be put into the version library, such as .class files generated by Java compilation; ignore your own configuration files with sensitive information, such as configuration files that store passwords.
Force the submission of ignored files. Git add-f
Git check-ignore-v checks why Git ignores the file.
Give the Git command an alias, which is a bit coquettish, that is, when you want to type git rebase later, you give it a "nickname" and call it git nb. You can use git nb instead of git rebase in the future.
Summary of common Git commands
Git config-global user.name "your name" binds all your Git repositories to your name
Git config-global user.email "your mailbox" binds all your Git repositories to your mailbox
Git init initializes your warehouse
Git add. Submit all the files in the workspace to the temporary storage area
Git add. / / submit the workspace files to the staging area
Git commit-m "xxx" submits all files from the temporary storage area to the storage 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 submits the main branch master of the warehouse area to the remote warehouse
Git push-u origin submits other branches to the remote warehouse
Git status to view the status of the current warehouse
Git diff to view the specific contents of file modifications
Git log displays the submission history from the recent to the farthest
Download clone file from git clone + warehouse address
Git reset-- hard + version number backtracking version, which is followed by master when commit
Git reflog displays command history
Git checkout-undo the command and replace the workspace file with the file in the version library. I think it's like ctrl + z in the Git world.
Git rm deletes files from the version library
Git branch view all current branches
Git branch create branch
Git checkout switches to branch
Git merge merge branches
Git branch-d deletes branches, which may fail because Git protects branches that have not been merged
Git branch-D + forcibly delete and discard branches that are not merged
Git log-- graph View Branch merge Diagram
Git merge-no-ff disables Fast forward mode when merging branches because this mode loses branch history information
When other tasks are plugged in, git stash "stores" the current work site and resumes its work later.
Git stash list to see where the job you just "saved" went to.
Git stash apply restores without deleting stash content
Git stash drop deletes stash content
Delete the stash content when git stash pop recovers.
When git remote views the information of the remote library, it 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 repository and merges locally, as opposed to git push
Git rebase "arranges" the bifurcated submission history into a straight line, which looks more intuitive.
Git tag looks at all the tags to know the historical version of tag
Git tag is tagged. The default is HEAD. Such as git tag v1.0
Git tag labels the version number, which is the alphanumeric string next to it when it is commit
Git show View tag Information
Git tag-a-m "" creates a label with a description. -a specifies the label signature,-m specifies the description text
Git tag-d delete tags
Git push origin pushes a tag to the remote
Git push origin-- tags pushes all local tags that have not been pushed remotely at one time
Git push origin: refs/tags/ removes remote tags
Git config-- global color.ui true lets Git display colors, which makes the command output look more eye-catching
Git add-f forces the submission of ignored files
Git check-ignore-v checks why Git ignores the file
Gitbranch other
Thank you for reading! This is the end of this article on "how to use git". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.