In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the relevant knowledge of "what are the common operation commands of Git". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
I. the concept of Git
Git is an open source distributed version control system, used for agile and efficient processing of any small or large projects, is currently one of the most popular version management tools.
2. The main difference between SVN and Git
SVN is a centralized version control system, the version library is centrally placed on the central server, and when you work, you use your own computer, so you have to get the latest version from the central server first, and then you need to push your work to the central server when you are done. Centralized version control system must be connected to the network to work, if it is OK in the local area network, the bandwidth is big enough, the speed is fast enough, if under the Internet, if the network speed is slow, then wonder.
Git is a distributed version control system, so it does not have a central server, and everyone's computer is a complete version library, so that there is no need to connect to the Internet when working, because the versions are all on their own computers. Since everyone's computer has a complete version library, how can multiple people cooperate? For example, if you change file An on your computer and others change file An on your computer, you just need to push your changes to each other and you can see each other's changes.
3. Install Git on Windows
Git was originally developed on Linux, and for a long time, Git could only run on Linux and Unix systems. Slowly, however, someone transplanted it to Windows. Git now works on Linux, Unix, Mac, and Windows platforms.
1. Download Git
To use Git, the first step is, of course, to install Git. Download from https://git-for-windows.github.io (if the network speed is slow, please move to the domestic image), and then install it by default.
two。 Installation steps
When the download is complete, open it for installation
3. Installation succeeded
Then you just need to wait quietly for the installation to be completed, and then right-click in the blank location of the desktop or any folder, and the two menu bars shown in the following figure indicate that the installation is successful.
4. Set up
After the installation, a command box will pop up, and we need to do the last step to set the identifier. Because git is a distributed management tool, you need to set a user name and mailbox as the identity. Enter the following code in the pop-up box.
Git config-global user.name "Your Name" git config-global user.email "email@example.com"
Note: git config-- global parameter, which means that all Git repositories on your machine will use this configuration. Of course, you can also specify different usernames and mailboxes for a warehouse.
Fourth, GIt common command parsing 1. Initialization
Before you can operate on Git, you have to create a Git repository and create an empty folder in the location you need. Then enter the folder, then right-click in the blank space, and click Git Bash Here to Git the current folder.
Initialization command
Git init
Common command
Cd: enter a directory mkdir: create a file pwd: display the current directory path mouse selection is copy, paste can be right-click paste, you can also use the shortcut key: Shift+INS2. Add files to version library 2.1 add to staging area
Create a new a.txt file in the root directory with the content aaa
Add the a.txt file to the staging area using the following command
Add to staging area command
Git add a.txt
Add all files under the current root directory to the staging area
Git add. 2.2 submit to warehouse
Use the following command to submit the files from the staging area to the version library
Inside the git commit-m "double quotes are comments-your submission instructions" 2.3.Why do you need add and commit to add files to Git?
Reason: commit can submit many files at a time, so you can add different files multiple times
For example:
Git add file1.txt # single add file to staging area git add file2.txt file3.txt # multiple add files to staging area git add. # add all files under the current folder to the staging area git commit-m "add 3 files." # submit all staging area files 3 to view the file status (check if there are any uncommitted files) 3.1 status one
Check the current file status using the following command
Git status
Result: there are no documents to submit.
3.2 State two
Create a new file b.txt with the content of bbb, and then check the file status
Result: there are untracked files that are not added to the staging area and submitted to the version library
3.3 status 3
After adding b.txt to the staging area, check the file status
Result: a new b.txt file in the staging area was not added to the version library
3.4 status four
After submitting the b.txt to the version library, then change the b.txt content from bbb to bbba, and then check the file status.
Result: the changed file b.txt was not added to the staging area and was not submitted
4. View submission history 4.1 first add the previously modified b.txt file and submit git add b.txtgit commit-m "submit modified b.txt file" 4.2 View history
So far, we have done a total of three operations, submitting an and b, and modifying b. The view history command is as follows
Git log
Note: if you use a lot of command information above, you will enter log mode. If you want to exit, press Q on the premise of English input method to exit.
As shown in the figure above, each submission has version number, time and other information, so it looks messy, appears too much information, and is dazzled. We can use the following command to get a simplified version of the log record, so that the history record can be displayed on one line.
Git log-pretty=oneline
You can restrict the history log you want to view, such as a recently submitted history log information
Git log-14.3 View submission History Command Summary git log # View all historical submission records git log-- pretty=oneline # condensed display of all historical submission records git reflog # you can view all operational records of all branches (including deleted commit records and reset ) git log-p # View all submission history and show each modification git log-2 # View the last 2 submission history (Note: the following numbers can be customized In other words, this type of writing is the embodiment of git log-n) git log-p-2 # view the history of the last two submissions and show the modified content git log-- stat # View the submission history, and show the summary (the summary lists the modified files and how many lines have been modified in each file) 5. Version fallback and recovery 5.1 fallback to the previous version or versions
First of all, Git must know which version the current version is. In Git, the current version is represented by HEAD, the previous version is head ^, and the previous version is head ^ ^. Of course, it is easy to write 100 ^ s up to 100 versions, so it is written as HEAD~100.
Git reset-- hard head ^
View the historical version information after the fallback
Result: from the historical version information, we can see that we have retreated from "modify b.txt" to "submit b.txt", and the current version after fallback is "submit b.txt".
5.2 fallback to the specified version
We directly copy the version number where we want to roll back, and use the following code to go back to the copied version number.
Git reset-hard
For example: I copy the version number of "submit a.txt file"
Git reset-hard 6ec9373d22d1a869b67681a39dc04df34292133b
Result: from the results of the historical version viewed, we can see that we have fallen back to the "submit a.txt" version
5.3 restore the fallback version
Sometimes we fall back on the version and want to go back to the back version, then we need to find the previous commit id, but from the above command to view the historical version, we can see that we can't find the previous commit id, so we can use the following command
Git reflog
Result: reflog can view all operation records of all branches (including commit records that have been deleted and reset operations)
Suppose we want to go back to the location of "submit modified b.txt file", that is, the penultimate line of historical version information, we just need to copy the 7a42e7b in front of HEAD, and then use the following command
Git reset-hard 7a42e7b
Result: the historical version information shows that we have fallen back to the version we want.
6. View the differences between different versions
When we changed the code and didn't add it to the staging area to submit, the next day we forgot what code we changed yesterday, so we can follow these steps to see the differences between different versions.
For example, we change the contents of the b.txt file to bbbaaa, and then look at it with the following code, we can see what we have modified
Git diff # View the file differences between different versions 7. Modify and undo 7.1 modified files
Recommended: first modification-> git add-> second modification-> git add-> git commit
Note: it is recommended to check whether any files have not been add before each commit
7.2Undo and modify git checkout-filename
Git checkout-filename can discard changes to the workspace:-followed by a space
The command git checkout-- readme.txt means to undo all changes to the readme.txt file in the workspace. There are two situations:
1: readme.txt has not been placed in the temporary storage area (git add) since it was modified. Now, undoing the modification will return to the same state as the version library.
Second: after readme.txt has been added to the temporary storage area, it has been modified. Now, undoing the modification will return to the state after it was added to the temporary storage area.
In short, get the file back to the state it was when it was last git commit or git add.
Note: git checkout-it becomes a * * "switch to another branch" * * command. We will encounter the git checkout command again in the later branch management.
8. Delete files / cache 8.1 use the rm command to delete files
Usually delete useless files directly in the file manager, or delete them with the rm command, for example, delete b.txt
Git rm b.txt
Delete step
Rm deletes a file
Rm is automatically added to the staging area after deletion, omitting the manual add command
Finally, the commit submission is made and the file is deleted.
Note: the deletion step can be withdrawn by reset undo operation
Delete cache git rm b.txt-- cached
The b.txt is in an untracked state, that is, removed from the staging area.
Special note: it is in an untracked state but does not exist in the temporary storage area, and the record in the history submission record still exists.
8.3 clear the cache
The so-called temporary storage area is just an index file in the .git directory, which is why it is called index (index). When deleting the contents of the temporary storage area, it is actually deleting the content in the index file, and the content in the .git / objects directory will not be deleted.
Rm .git / index9. Branch Management 9.1 Branch Operation Command git branch # View Branch git branch # create Branch git checkout # switch Branch git checkout-b # create + switch Branch git merge # merge a branch into the current branch git branch-d # Delete branch 5. The Git command integrates 1. Set global user name and mailbox command function git config-- global user.name "Your Name" set user name git config-- global user.email "email@example.com" set mailbox
two。 Initialization command
The command functions as git init to initialize git and create .git files
3. Common command
Command action cd to enter a directory mkdir to create a file pwd displays the current directory path mouse selection is to copy direct mouse button paste / shortcut key: Shift+INS paste
4. Add to staging area
The command functions git add a.txt to add a.txt to the staging area git add. Add all files under the current root to the temporary storage area git commit-m "double quotes with comments-your submission instructions" submit the files in the temporary storage area to the version library (all at once)
5. View file status
Command functions git status to check file status (check if there are any unsubmitted files)
6. View submission history
Command functions git log to view all historical submission records git log-- pretty=oneline concise display all historical submission records git reflog can view all operation records of all branches (including deleted commit records and reset operations) git log-p View all submission history and show the content of each modification git log-2 View the last two submission history (Note: the following numbers can be customized, that is to say This type of writing is the embodiment of git log-n) git log-p-2 looks at the history of the last two submissions and shows the modified content git log-- stat view the submission history, and shows the summary (the summary lists the modified files and how many lines have been modified in each file)
7. Version fallback and recovery
The command functions git reset-- hard head ^ to fall back to the previous version git reset-- hard HEAD~N (not -, wavy line) back to the last N versions git reset-- hard fallback to the specified version git reflog and git reset-- hard using the recovery fallback version
8. View the differences between different versions
Command functions as git diff to view file differences between different versions
9. Undo the modification
Command function git checkout-b.txt 1: readme.txt has not been placed in the temporary storage area (git add) since it was modified. Now, undoing the modification will return to exactly the same state as the version library.
Second: after readme.txt has been added to the temporary storage area, it has been modified. Now, undoing the modification will return to the state after it was added to the temporary storage area.
10. Delete
Command acts as git rm b.txtrm to delete files
Rm is automatically added to the staging area after deletion, omitting the manual add command
Finally, the commit commits, and the file is deleted git rm b.txt-- cached deletes a single file in the cache, and b.txt is in an untracked state, that is, delete rm .git / index from the staging area to clear all caches.
11. Branch management
Command action git branch view branch git branch create branch git checkout toggle branch git checkout-b create + switch to new branch git merge merge a branch into the current branch git branch-d delete branch
This is the end of the content of "what are the common operation commands of Git". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.