Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Git for full stack development of Python

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

No.1 Git feature version control: can solve the code problem developed by multiple people at the same time, but also can retrieve the history code distributed: Git is a distributed version control system, the same Git warehouse, can be distributed to different machines, first of all, there will be a computer as a server, this computer 24 hours a day service, other computers are from this computer to clone a copy of the code to their own computers And each submit their own changes to the server repository, or you can pull other people's submissions from the server repository to install the sudo apt-get install git-yNo.2 version to create and fallback to use.

Create a version library

Git init

Create a version

Git add test.txtgit commit-m 'v1.0'

View workspace status

Get status

View version record

Git loggit log-- pretty=oneline puts each submission on one line to display git log-- pretty=oneline-- graph displays each submission graphically

Version fallback

Git reset-- hard head ^ where HEAD represents the latest version, a ^ represents a fallback of one version, and several ^ indicates a fallback of several versions git reset-- hard HEAD~1 1 means a fallback of one version, then 10 means fallback of 10 versions of git reset-- hard HEAD

View operation record

The directory in the git reflog workspace, staging area, version library computer is a workspace where there is a hidden directory .git, which is the version library. there are many things in the version library, the most important of which is stage (temporary storage area), and one is the first branch master that git automatically created for us, and the HEAD pointer to master, so the first branch is master. So when we execute git commit, we push to the master branch, the git add file puts the modified file in the temporary storage area, and the git commit submits the data in the temporary storage area to the workspace management to modify echo 'this is first line' > > test.txtgit add test.txtecho' this is first line' > > test.txtgit commit-m 'v1.1'git status # to find that after the second modification of the test.txt file, it is not really added to the workspace So it was not submitted to the version library On branch masterChanges not staged for commit: (use "git add." To update what will be committed) (use "git checkout--..." To discard changes in working directory) modified: test.txtno changes added to commit (use "git add" and/or "git commit-a") undo the modification

Undo changes to the test.txt file

Get checkout-test.txt

If you want to undo the changes to the temporary storage area,

Git reset HEAD test.txt comparison file

What's the difference between the test.txt file in the workspace and the version library?

Git diff test.txt HEAD-test.txt

Compare the differences between the test.txt files in the two versions

Git diff HEAD head ^-- test.txt deletes files

We delete the test.txt in the directory, and git knows that we deleted the files, but the workspace and version library do not know, so we can execute git status to know which files have been deleted.

Rm-rf test.txtgit status

Now we have two choices, one is to make sure that the file is to be deleted, and the other is to make sure that the file is deleted by mistake.

# 1.git rm test.txtgit commit# 2.git checkout-- test.txtNo.4 Branch Management creation and merge

Git forms a timeline of the version we submit each time, and this timeline is a branch. At present, this branch is called master. After each submission, the HEAD points to the master,master and points to the submission. Each time the master moves forward, so the timeline is getting longer and longer. When we create the branch slave, the HEAD points to the slave and points to the new submission. From then on, the second submission will submit the version to the slave branch, but the master will remain the same. When our work on the salve branch is done, we can merge the branches

View branch git branch create and switch branch git checkout-b switch branch git checkout merge branch git merge delete branch git branch-d resolve conflict

Merging is not plain sailing. For example, I have a test.txt file in my workspace and there is no data in this file. I added a row of data to the first line in the master branch, submitted it to the version library, then switched to slave, added a new line to this file, and then submitted it to the version library. After merging, there will be conflicts.

Kernel@DESKTOP-67P59AO MINGW64 / d/Codes/Git/test (master) $git checkout-b slaveSwitched to a new branch 'slave'kernel@DESKTOP-67P59AO MINGW64 / d/Codes/Git/test (slave) $cat test.txtmaster:the new codekernel@DESKTOP-67P59AO MINGW64 / d/Codes/Git/test (slave) $vi test.txtkernel@DESKTOP-67P59AO MINGW64 / d/Codes/Git/test (slave) $cat test.txtmaster:the new codeslave:the new codekernel@DESKTOP-67P59AO MINGW64 / d/Codes/Git/test ( Slave) $git add test.txtkernel@DESKTOP-67P59AO MINGW64 / d/Codes/Git/test (slave) $git commit-m '1.2' [slave 5035bfb] 1.21 file changed 1 insertion (+) kernel@DESKTOP-67P59AO MINGW64 / d/Codes/Git/test (slave) $git checkout masterSwitched to branch 'master'kernel@DESKTOP-67P59AO MINGW64 / d/Codes/Git/test (master) $cat test.txtmaster:the new codekernel@DESKTOP-67P59AO MINGW64 / d/Codes/Git/test (master) $vi test.txtkernel@DESKTOP-67P59AO MINGW64 / d/Codes/Git/test (master) $cat test.txtmaster:the new codemaster:the new codekernel@DESKTOP-67P59AO MINGW64 / d/Codes/Git/test (master) $git add test.txtkernel@DESKTOP-67P59AO MINGW64 / d/Codes/Git/test (master) $git commit-m '1.3' [master e46f9b7] 1.31 file changed 2 insertions (+) kernel@DESKTOP-67P59AO MINGW64 / d/Codes/Git/test (master) $git merge slaveAuto-merging test.txtCONFLICT (content): Merge conflict in test.txtAutomatic merge failed Fix conflicts and then commit the result.# resolves conflicts kernel@DESKTOP-67P59AO MINGW64 / d/Codes/Git/test (master | MERGING) $cat test.txtmaster:the new code > slavekernel@DESKTOP-67P59AO MINGW64 / d/Codes/Git/test (master | MERGING) $vi test.txtkernel@DESKTOP-67P59AO MINGW64 / d/Codes/Git/test (master | MERGING) $cat test.txtmaster:the new codeslave:the new codekernel@DESKTOP-67P59AO MINGW64 / d/Codes/Git/test (master | MERGING) $git add test.txtkernel@DESKTOP-67P59AO MINGW64 / d / Codes/Git/test (master | MERGING) $git commit-m '1.4' [master b252477] 1.4 Branch Management Policy

Usually, when merging branches, git will use fast forward mode as much as possible, but sometimes the quick merge is not successful but there is no conflict, and then a new commit is made after the merge.

Kernel@DESKTOP-67P59AO MINGW64 / d/Codes/Git/test (slave) $vi test1.txtkernel@DESKTOP-67P59AO MINGW64 / d/Codes/Git/test (slave) $cat test1.txtadd new codekernel@DESKTOP-67P59AO MINGW64 / d/Codes/Git/test (slave) $git add test1.txtkernel@DESKTOP-67P59AO MINGW64 / d/Codes/Git/test (slave) $git commit-m '1.5' [slave 5fa46e2] 1.51 file changed 1 insertion (+) create mode 100644 test1.txtkernel@DESKTOP-67P59AO MINGW64 / d/Codes/Git/test (slave) $git checkout masterSwitched to branch 'master'kernel@DESKTOP-67P59AO MINGW64 / d/Codes/Git/test (master) $vi test.txtkernel@DESKTOP-67P59AO MINGW64 / d/Codes/Git/test (master) $cat test.txtmaster:the new codeslave:the new codemaster:add new codekernel@DESKTOP-67P59AO MINGW64 / d/Codes/Git/test (master) $git merge slaveMerge made by the' recursive' strategy. Test1.txt | 1 + 1 file changed 1 insertion (+) create mode 100644 test1.txtkernel@DESKTOP-67P59AO MINGW64 / d/Codes/Git/test (master) $git log-- pretty=onelinebee1e463867f605f16c3fbad6a50ea36f4e0b20a (HEAD-> master) merge slave branches 5fa46e2ec62346526554b04b62a468afbde0f9ba (slave) 1.5b2524777899baab52783e1b49360fedc1e6a7e50 1.4e46f9b7b5c379238ddca5db70746bc4f9f8c5edc 1.35035bfba43172b02bdbb7447fd9ebb018e6f49f5 1.22ae2bcac4258f41cb1a1e3a3a81a572be80c28f9 1.18d3dee945a68e5996d9c088a9769f43fc5c75228 1.0kernel@DESKTOP-67P59AO MINGW64 / d/Codes/Git/test (master) $git log-- pretty=oneline-- graph* bee1e463867f605f16c3fbad6a50ea36f4e0b20a (HEAD-> master) merge slave branches |\ | * 5fa46e2ec62346526554b04b62a468afbde0f9ba (slave) 1.5 * | B2524777899baab52783e1b49360fedc1e6a7e50 1.4 |\ | | / | * 5035bfba43172b02bdbb7447fd9ebb018e6f49f5 1.2 * | e46f9b7b5c379238ddca5db70746bc4f9f8c5edc 1.3 | / * 2ae2bcac4258f41cb1a1e3a3a81a572be80c28f9 1.1 * 8d3dee945a68e5996d9c088a9769f43fc5c75228 1.0kernel@DESKTOP-67P59AO MINGW64 / d/Codes/Git/test (master) $git branch-d slaveDeleted branch slave (was 5fa46e2).

But deleting branches after quickly merging branches will lose branch information, so we often need to disable fast merging.

Kernel@DESKTOP-67P59AO MINGW64 / d/Codes/Git/test (master) $git checkout-b slaveSwitched to a new branch 'slave'M test.txtkernel@DESKTOP-67P59AO MINGW64 / d/Codes/Git/test (slave) $vi test2.txtkernel@DESKTOP-67P59AO MINGW64 / d/Codes/Git/test (slave) $git add test2.txtkernel@DESKTOP-67P59AO MINGW64 / d/Codes/Git/test (slave) $git commit-m' 1.6' [slave 1e833e6] 1.61 file changed 2 insertions (+) create mode 100644 test2.txtkernel@DESKTOP-67P59AO MINGW64 / d/Codes/Git/test (slave) $git checkout masterSwitched to branch 'master'M test.txtkernel@DESKTOP-67P59AO MINGW64 / d/Codes/Git/test (master) $git merge-- no-ff-m' disable fast merge 'slaveMerge made by the' recursive' strategy. | test2.txt | 2 + + 1 file changed, 2 insertions (+) create mode 100644 test2.txtkernel@DESKTOP-67P59AO MINGW64 / d/Codes/Git/test (master) $git log-- graph-- pretty=oneline* cabeb0f4edc6617d27e1a7624be927a0f5f7da59 (HEAD-> master) disable fast merge |\ | * 1e833e62109f3dd266883834bf11014d85c4a3dc (slave) 1.6 | / * bee1e463867f605f16c3fbad6a50ea36f4e0b20a merge slave branches |\ | * 5fa46e2ec62346526554b04b62a468afbde0f9ba 1.5* | b2524777899baab52783e1b49360fedc1e6a7e50 1.4 |\ | | / | * 5035bfba43172b02bdbb7447fd9ebb018e6f49f5 1.2 * | e46f9b7b5c379238ddca5db70746bc4f9f8c5edc 1.3 | / * 2ae2bcac4258f41cb1a1e3a3a81a572be80c28f9 1.1 * 8d3dee945a68e5996d9c088a9769f43fc5c75228 1.0bug branch |

In software development, the emergence of bug is a frequent thing. In git, each bug can be repaired by a temporary branch, and the branch can be deleted after repair. If a bug suddenly appears, it needs to be repaired immediately, but the work of the branch has not been completed, and there is no way to submit it, so the save site function (stash) in git is used.

Kernel@DESKTOP-67P59AO MINGW64 / d/Codes/Git/test (master) $git statusOn branch masterChanges not staged for commit: (use "git add..." To update what will be committed) (use "git checkout--..." To discard changes in working directory) modified: test.txtno changes added to commit (use "git add" and/or "git commit-a") kernel@DESKTOP-67P59AO MINGW64 / d/Codes/Git/test (master) $git stashSaved working directory and index state WIP on master: cabeb0f disable fast merge kernel@DESKTOP-67P59AO MINGW64 / d/Codes/Git/test (master) $git statusOn branch masternothing to commit Working tree cleankernel@DESKTOP-67P59AO MINGW64 / d/Codes/Git/test (master) $git checkout slaveSwitched to branch 'slave'kernel@DESKTOP-67P59AO MINGW64 / d/Codes/Git/test (slave) $git checkout-b bug001Switched to a new branch' bug001'kernel@DESKTOP-67P59AO MINGW64 / d/Codes/Git/test (bug001) $vi test.txtkernel@DESKTOP-67P59AO MINGW64 / d/Codes/Git/test (bug001) $git add test.txtkernel@DESKTOP-67P59AO MINGW64 / d/Codes/Git/test (bug001) $git Commit-m 'bug001 repair complete'[bug001 aab106b] bug001 repair complete 1 file changed 1 deletion (-) kernel@DESKTOP-67P59AO MINGW64 / d/Codes/Git/test (bug001) $git checkout slaveSwitched to branch 'master'kernel@DESKTOP-67P59AO MINGW64 / d/Codes/Git/test (slave) $git merge bug001Merge made by the' recursive' strategy. Test.txt | 1-1 file changed 1 deletion (-) kernel@DESKTOP-67P59AO MINGW64 / d/Codes/Git/test (slave) $git branch-d bug001Deleted branch bug001 (was aab106b). Kernel@DESKTOP-67P59AO MINGW64 / d/Codes/Git/test (slave) $git checkout masterSwitched to branch 'master'kernel@DESKTOP-67P59AO MINGW64 / d/Codes/Git/test (master) $git stash liststash@ {0}: WIP on master: cabeb0f disable fast merging kernel@DESKTOP-67P59AO MINGW64 / d/Codes/Git/test (master) $git stash popgOn branch SlaveChanges not staged for commit: (use "git add..." To update what will be committed) (use "git checkout--..." To discard changes in working directory) modified: test.txtno changes added to commit (use "git add" and/or "git commit-a") Dropped refs/stash@ {0} (8fe9c203f75afc7929433faabc52e02a6d555ff4) kernel@DESKTOP-67P59AO MINGW64 / d/Codes/Git/test (master) $git statusOn branch slaveChanges not staged for commit: (use "git add..." To update what will be committed) (use "git checkout--..." To discard changes in working directory) modified: test.txtno changes added to commit (use "git add" and/or "git commit-a") No.5 uses GitHub to add ssh accounts

If a machine needs to interact with GitHub, add the ssh public key of that machine to the GitHub account

Edit the .GitHub file and use ssh-keygen-t rsa-C "email address" to generate the ssh key.

Click the drop-down triangle after the account avatar, select settings, select the SSH and GPG keys option, click New SSH keys, copy the contents of the id_rsa.pub file, and click the Add SSH key clone project.

Git clone project address

Upload branch

Git push origin Branch name

Tracking remote branches

Git branch-set-upstream-to=origin/slave master

When the remote branch is successfully tracked locally, when the code is modified, only git push is needed to submit the modification to the remote

Pull the code from the remote branch

Git pull origin Branch name

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report