Table of contents
Clone from a remote repository
This blog is a study note based on Liao Xuefeng's Git tutorial.
Original tutorial address: Git tutorial - Liao Xuefeng's official website (liaoxuefeng.com)
Git
time travel
version rollback
########################################### # git log View historical submission records git log lijiahao@lijiahao-virtual-machine:~/study_git/study-git$ git log commit cafd14328a9c94134d7c707d8902b60c231faac2 (HEAD -> master, tag: v1.0, origin/master, origin/HEAD) Merge: 50aa1bb 925c073 Author: lijiahao <lijiahao@qq.com> Date: Mon Mar 6 17:01:22 2023 +0800 Merge branch 'dev' commit 50aa1bbd7f9a48d30b15ef5d45734eac55bbd7c6 Author: lijiahao <lijiahao@qq.com> Date: Mon Mar 6 16:57:23 2023 +0800 create test.cc commit 925c073f4664f3ce830b0ea39101aeefdff38deb (origin/dev, dev) Author: lijiahao <lijiahao@qq.com> Date: Mon Mar 6 16:54:05 2023 +0800 changed test.cc commit 9567efa8c1ae6ee9e10b09efee6944d1400d58eb Author: lijiahao <lijiahao@qq.com> Date: Mon Mar 6 16:48:41 2023 +0800 add readme.md commit 327494513e1a5cc387ed6ae87e5e7d7feca0cdc4 Author: waywtl <1148117782@qq.com> Date: Mon Mar 6 08:45:09 2023 +0000 Initial commit # display per row git log --pretty=oneline cafd14328a9c94134d7c707d8902b60c231faac2 (HEAD -> master, tag: v1.0, origin/master, origin/HEAD) Merge branch 'dev' 50aa1bbd7f9a48d30b15ef5d45734eac55bbd7c6 create test.cc 925c073f4664f3ce830b0ea39101aeefdff38deb (origin/dev, dev) changed test.cc 9567efa8c1ae6ee9e10b09efee6944d1400d58eb add readme.md 327494513e1a5cc387ed6ae87e5e7d7feca0cdc4 Initial commit ################################################################# # Roll back to the previous version, HEAD is a pointer to the current version, HEAD^ represents the previous version pointed to by HEAD, every time you add a ^, you will add a version git reset --hard HEAD^ # If you go back to the previous 100 versions, you can also git reset --hard HEAD~100 lijiahao@lijiahao-virtual-machine:~/study_git/study-git$ git reset --hard HEAD^ HEAD Now at 50 aa1bb create test.cc lijiahao@lijiahao-virtual-machine:~/study_git/study-git$ git log commit 50aa1bbd7f9a48d30b15ef5d45734eac55bbd7c6 (HEAD -> master) Author: lijiahao <lijiahao@qq.com> Date: Mon Mar 6 16:57:23 2023 +0800 create test.cc commit 9567efa8c1ae6ee9e10b09efee6944d1400d58eb Author: lijiahao <lijiahao@qq.com> Date: Mon Mar 6 16:48:41 2023 +0800 add readme.md commit 327494513e1a5cc387ed6ae87e5e7d7feca0cdc4 Author: waywtl <1148117782@qq.com> Date: Mon Mar 6 08:45:09 2023 +0000 Initial commit # If you go back to the previous two versions git reset --hard HEAD^^ lijiahao@lijiahao-virtual-machine:~/study_git/study-git$ git reset --hard HEAD^^ HEAD Now at 9567 efa add readme.md lijiahao@lijiahao-virtual-machine:~/study_git/study-git$ git log commit 9567efa8c1ae6ee9e10b09efee6944d1400d58eb (HEAD -> master) Author: lijiahao <lijiahao@qq.com> Date: Mon Mar 6 16:48:41 2023 +0800 add readme.md commit 327494513e1a5cc387ed6ae87e5e7d7feca0cdc4 Author: waywtl <1148117782@qq.com> Date: Mon Mar 6 08:45:09 2023 +0000 Initial commit # If you choose the specified version # Each version has a unique commitId, which can be used both for rollback and forward git reset --hard commitId ################################################################## # show historical git commands git reflog lijiahao@lijiahao-virtual-machine:~/study_git/study-git$ git reflog cafd143 (HEAD -> master, tag: v1.0, origin/master, origin/HEAD) HEAD@{0}: reset: moving to cafd 9567efa HEAD@{1}: reset: moving to HEAD~1 50aa1bb HEAD@{2}: reset: moving to HEAD~1 cafd143 (HEAD -> master, tag: v1.0, origin/master, origin/HEAD) HEAD@{3}: reset: moving to cafd 9567efa HEAD@{4}: reset: moving to HEAD^^ cafd143 (HEAD -> master, tag: v1.0, origin/master, origin/HEAD) HEAD@{5}: reset: moving to cafd 50aa1bb HEAD@{6}: reset: moving to HEAD^ cafd143 (HEAD -> master, tag: v1.0, origin/master, origin/HEAD) HEAD@{7}: reset: moving to cafd 9567efa HEAD@{8}: reset: moving to HEAD^^ cafd143 (HEAD -> master, tag: v1.0, origin/master, origin/HEAD) HEAD@{9}: reset: moving to cafd1 50aa1bb HEAD@{10}: reset: moving to HEAD^ cafd143 (HEAD -> master, tag: v1.0, origin/master, origin/HEAD) HEAD@{11}: checkout: moving from dev to master 925c073 (origin/dev, dev) HEAD@{12}: checkout: moving from master to dev cafd143 (HEAD -> master, tag: v1.0, origin/master, origin/HEAD) HEAD@{13}: commit (merge): Merge branch 'dev' 50aa1bb HEAD@{14}: checkout: moving from dev to master 925c073 (origin/dev, dev) HEAD@{15}: checkout: moving from master to dev 50aa1bb HEAD@{16}: commit: create test.cc 9567efa HEAD@{17}: checkout: moving from dev to master 925c073 (origin/dev, dev) HEAD@{18}: checkout: moving from master to dev 9567efa HEAD@{19}: checkout: moving from dev to master 925c073 (origin/dev, dev) HEAD@{20}: commit: changed test.cc 9567efa HEAD@{21}: checkout: moving from master to dev 9567efa HEAD@{22}: commit: add readme.md 3274945 HEAD@{23}: clone: from https://gitee.com/waywtl/study-git.git
manage changes
########################################################################### # View current branch status git status lijiahao@lijiahao-virtual-machine:~/study_git/study-git$ git status at the branch master your branch vs the upstream branch 'origin/master' unanimous. Changes not yet staged for commit: (use "git add <document>..." Update what to submit) (use "git restore <document>..." Discard workspace changes) Revise: readme.md untracked files: (use "git add <document>..." to include the content to be submitted) myfile Amendments have not yet been added to the commit (using "git add" and/or "git commit -a") ########################################################################### # View file modifications git diff readme.md # View the difference between the latest version in the workspace and the repository git diff HEAD -- readme.md lijiahao@lijiahao-virtual-machine:~/study_git/study-git$ git diff HEAD -- readme.md diff --git a/readme.md b/readme.md index 76d770f..a9c5755 100644 --- a/readme.md +++ b/readme.md @@ -1,4 +1,4 @@ Git is a distributed version control system. Git is free software distributed under the GPL. Git has a mutable index called stage. -Git tracks changes. +Git tracks changes of files.
Undo changes
########################################################################### # Discard workspace changes git checkout -- readme.md git restore readme.md Bundle readme.txt All modifications to the file in the workspace are undone. There are two situations here: one is readme.txt Since the modification has not been put into the temporary storage area, now, undoing the modification will return to the state exactly the same as the version library; one is readme.txt After it has been added to the temporary storage area, it has been modified. Now, undoing the modification will return to the state after adding to the temporary storage area. In short, let this file go back to the last time git commit or git add time status. ########################################################################## # Undo the modification in the temporary storage area and put it back into the work area git reset HEAD readme.md # The git reset command can not only roll back the version, but also roll back the changes in the temporary storage area to the work area. When we use HEAD, it means the latest version. lijiahao@lijiahao-virtual-machine:~/study_git/study-git$ echo "My stupid boss still prefers SVN." >> readme.md lijiahao@lijiahao-virtual-machine:~/study_git/study-git$ cat readme.md Git is a distributed version control system. Git is free software distributed under the GPL. Git has a mutable index called stage. Git tracks changes of files. My stupid boss still prefers SVN. lijiahao@lijiahao-virtual-machine:~/study_git/study-git$ git add readme.md lijiahao@lijiahao-virtual-machine:~/study_git/study-git$ git status at the branch master Changes to commit: (use "git restore --staged <document>..." to unstage) Revise: readme.md lijiahao@lijiahao-virtual-machine:~/study_git/study-git$ git reset HEAD readme.md Unstaged changes after reset: M readme.md lijiahao@lijiahao-virtual-machine:~/study_git/study-git$ git status at the branch master Changes not yet staged for commit: (use "git add <document>..." Update what to submit) (use "git restore <document>..." Discard workspace changes) Revise: readme.md Amendments have not yet been added to the commit (using "git add" and/or "git commit -a") lijiahao@lijiahao-virtual-machine:~/study_git/study-git$ git checkout -- readme.md lijiahao@lijiahao-virtual-machine:~/study_git/study-git$ git status at the branch master No files to commit, clean workspace
Delete Files
########################################################################### # delete file from repository git rm myfile rm myfile two cases of 1.If you really want to delete the file, after deleting the local file, then git rm myfile,Delete files in the repository 2.delete wrong, use git checkout -- myfile,restore local myfile document # The essence of git checkout -- file is to replace the file version in the workspace with the file version in the repository
remote warehouse
Add remote library
There are local libraries first, and remote libraries later.
########################################################################### # Add an existing remote warehouse locally git remote add origin https://gitee.com/waywtl/study-git.git # origin is the local name of the remote library, which is the default name of Git, and can also be changed to something else ########################################################################### # Push all the contents of the local repository to the remote repository git push -u origin master # Push the version library of the master branch in the local origin remote library to the remote library, and add -u to make Git associate the local master branch with the remote master branch, which can simplify the command when pushing or pulling in the future # Subsequent pushes can be directly git push origin master
delete remote repository
########################################################################### # View the remote library information associated with the local git remote -v lijiahao@lijiahao-virtual-machine:~/study_git/study-git$ git remote -v origin https://gitee.com/waywtl/study-git.git (fetch) origin https://gitee.com/waywtl/study-git.git (push) ########################################################################### # Delete the connection between local and remote libraries git remote rm origin
Clone from a remote repository
There are remote libraries first, and then local libraries.
########################################################################### # Clone the remote library to the local git clone https://gitee.com/waywtl/study-git.git
branch management
Create and merge branches
########################################################################### # Check the branch, the one with * in front of the branch indicates the current branch git branch lijiahao@lijiahao-virtual-machine:~/study_git/study-git$ git branch dev * master ########################################################################### # create branch git branch <name> ########################################################################### # switch branch git checkout <name> / git switch <name> ########################################################################### # create + switch branches git checkout -b <name> / git switch -c <name> ########################################################################### # Merge a branch into the current branch git merge <name> ########################################################################### # delete branch git branch -d <name>
resolve conflict
########################################################################### # Conflicts after merging branches git merge <name> After the conflict occurs, you need to manually change the conflicting file to your own expectations before submitting
branch management strategy
Usually, when merging branches, Git will use Fast forward mode if possible, but in this mode, after deleting the branch, the branch information will be lost.
If you want to forcibly disable the Fast forward mode, Git will generate a new commit when merging, so that the branch information can be seen from the branch history.
########################################################################### # disable ff mode git merge --no-ff -m "mrge with no-ff" dev
Since ff mode merging is disabled, there will be a new commit, so you need to bring the -m parameter and a description.
Bug branch
When you are working on the dev branch, you suddenly receive a notice that you are going to fix a bug on the master branch, and you don’t want to submit the content on the dev branch because the work is not finished yet, how to save the current work site?
########################################################################### # storage job site git stash # After storage, you can’t see the uncommitted content of the current branch. When you need to resume work, you can restore the site and continue working lijiahao@lijiahao-virtual-machine:~/study_git/study-git$ git switch dev switch to branch 'dev' lijiahao@lijiahao-virtual-machine:~/study_git/study-git$ echo "working......" >> readme.md lijiahao@lijiahao-virtual-machine:~/study_git/study-git$ cat readme.md Git is a distributed version control system. Git is free software distributed under the GPL. Git has a mutable index called stage. Git tracks changes of files. Creating a new branch is quick and simple. working...... lijiahao@lijiahao-virtual-machine:~/study_git/study-git$ git stash Save working directory and index state WIP on dev: 67d1123 conflict fixed lijiahao@lijiahao-virtual-machine:~/study_git/study-git$ cat readme.md Git is a distributed version control system. Git is free software distributed under the GPL. Git has a mutable index called stage. Git tracks changes of files. Creating a new branch is quick and simple. ########################################################################### # View Stored Job Sites git stash list lijiahao@lijiahao-virtual-machine:~/study_git/study-git$ git stash list stash@{0}: WIP on dev: 67d1123 conflict fixed ########################################################################### # recovery site # 1. After recovery, the stash content is not deleted, you need to use git stash drop to delete git stash apply # 2. While restoring, delete the stash content git stash pop ########################################################################### # After multiple stash, restore the specified stash git stash apply stash@{<number>} lijiahao@lijiahao-virtual-machine:~/study_git/study-git$ git stash list stash@{0}: WIP on dev: 67d1123 conflict fixed stash@{1}: WIP on dev: 67d1123 conflict fixed stash@{2}: WIP on dev: 67d1123 conflict fixed stash@{3}: WIP on dev: 67d1123 conflict fixed lijiahao@lijiahao-virtual-machine:~/study_git/study-git$ git stash apply stash@{3} at the branch dev Changes not yet staged for commit: (use "git add <document>..." Update what to submit) (use "git restore <document>..." Discard workspace changes) Revise: readme.md Amendments have not yet been added to the commit (using "git add" and/or "git commit -a") lijiahao@lijiahao-virtual-machine:~/study_git/study-git$ cat readme.md Git is a distributed version control system. Git is free software distributed under the GPL. Git has a mutable index called stage. Git tracks changes of files. Creating a new branch is quick and simple. working......
When we fix the bug in another branch and submit it, and the branch we are working on also has the same bug, do we need to redo the previous work? unnecessary!
########################################################################### # Copy the changes made by a commit to the current branch git cherry-pick commitID example: # Synchronize the changes made by commitId 67d1123 to the current branch git cherry-pick 67d1123
Feature branch
########################################################################### # Delete a branch that has not been merged git branch -D <name>
Collaboration
########################################################################### # View remote library information git remote -v ########################################################################### # Create a branch corresponding to the remote branch locally git checkout -b branch-name origin/branch-name ########################################################################### # Push branch from local git push origin branch-name ########################################################################### #If the push fails, first use git pull to grab the remote branch, merge the branch, resolve the conflict and then push #If git pull fails because the local branch has not established a link relationship with the corresponding remote branch, use: git branch --set-upstream-to=origin/<branch-name> <branch-name>
Rebase
########################################################################### # 1. First make two submissions to the readme.md file lijiahao@lijiahao-virtual-machine:~/study_git/study-git$ echo "comment" >> readme.md lijiahao@lijiahao-virtual-machine:~/study_git/study-git$ git add readme.md lijiahao@lijiahao-virtual-machine:~/study_git/study-git$ git commit -m "add comment" [master aad0247] add comment 1 file changed, 1 insertion(+) lijiahao@lijiahao-virtual-machine:~/study_git/study-git$ echo "author" >> readme.md lijiahao@lijiahao-virtual-machine:~/study_git/study-git$ git add readme.md lijiahao@lijiahao-virtual-machine:~/study_git/study-git$ git commit -m "add author" [master 3962c09] add author 1 file changed, 1 insertion(+) # 2. Then go to another corresponding warehouse and make a push to the master lijiahao@lijiahao-virtual-machine:~/study_git/test/study-git$ touch happy.py lijiahao@lijiahao-virtual-machine:~/study_git/test/study-git$ git add happy.py lijiahao@lijiahao-virtual-machine:~/study_git/test/study-git$ git commit -m "add happy.py" [master fb6ad9a] add happy.py 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 happy.py lijiahao@lijiahao-virtual-machine:~/study_git/test/study-git$ git push # 3. Go back to the original warehouse and push the two submissions just now lijiahao@lijiahao-virtual-machine:~/study_git/study-git$ git push Username for 'https://gitee.com': waywtl Password for 'https://waywtl@gitee.com': To https://gitee.com/waywtl/study-git.git ! [rejected] master -> master (fetch first) error: Unable to push some refs to 'https://gitee.com/waywtl/study-git.git' Hint: The update was rejected because the remote repository contains commits that don't exist locally on your end. This is usually because of the additional Hint: A repository has already pushed to this ref. You may need to integrate remote changes before pushing again Tips: (such as 'git pull ...'). Tip: see 'git push --help' middle 'Note about fast-forwards' section. # 4. Failed, indicating that someone pushed the remote branch before us, corresponding to the second step # First use git log --graph --pretty=oneline --abbrev-commit to view the historical operation flow, and found that the most recent is a straight line lijiahao@lijiahao-virtual-machine:~/study_git/study-git$ git log --graph --pretty=oneline --abbrev-commit * 3962c09 (HEAD -> master) add author * aad0247 add comment * f75243d (origin/master, origin/HEAD) remove comment * eb25d22 remove author * 13e306c Merge branch 'master' of https://gitee.com/waywtl/study-git # 5. Use git pull to pull the remote to the local, and then merge lijiahao@lijiahao-virtual-machine:~/study_git/study-git$ git pull Username for 'https://gitee.com': waywtl Password for 'https://waywtl@gitee.com': remote: Enumerating objects: 4, done. remote: Counting objects: 100% (4/4), done. remote: Compressing objects: 100% (2/2), done. remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0 In the expanded object: 100% (3/3), 240 byte | 240.00 KiB/s, Finish. from https://gitee.com/waywtl/study-git f75243d..fb6ad9a master -> origin/master Merge made by the 'recursive' strategy. happy.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 happy.py # 6. Use git log --graph --pretty=oneline --abbrev-commit again to view the historical operation flow and find that there is a fork # If you push to the remote now, it will make the historical operation flow complicated and ugly lijiahao@lijiahao-virtual-machine:~/study_git/study-git$ git log --graph --pretty=oneline --abbrev-commit * 9ccf9c0 (HEAD -> master) Merge branch 'master' of https://gitee.com/waywtl/study-git |\ | * fb6ad9a (origin/master, origin/HEAD) add happy.py * | 3962c09 add author * | aad0247 add comment |/ * f75243d remove comment * eb25d22 remove author * 13e306c Merge branch 'master' of https://gitee.com/waywtl/study-git # 7. Try using git rebase lijiahao@lijiahao-virtual-machine:~/study_git/study-git$ git rebase First, roll back the head pointer so you can replay your work on it... application: add comment application: add author # 8. Then use git log --graph --pretty=oneline --abbrev-commit to view # It was found that the fork before has now become a straight line, but the position of the local submission has been moved # Put the two commits after * fb6ad9a (origin/master, origin/HEAD) add happy.py # Their modification is no longer based on *f75243d remove comment # Instead based on * fb6ad9a (origin/master, origin/HEAD) add happy.py # But the last commit b4794d8 content is consistent ijiahao@lijiahao-virtual-machine:~/study_git/study-git$ git log --graph --pretty=oneline --abbrev-commit * b4794d8 (HEAD -> master) add author * 0b95c7d add comment * fb6ad9a (origin/master, origin/HEAD) add happy.py * f75243d remove comment * eb25d22 remove author * 13e306c Merge branch 'master' of https://gitee.com/waywtl/study-git # 9. After using git push, use git log to view lijiahao@lijiahao-virtual-machine:~/study_git/study-git$ git push Username for 'https://gitee.com': waywtl Password for 'https://waywtl@gitee.com': enumeration object: 8, Finish. object counting: 100% (8/8), Finish. Use 4 threads for compression compressed object: 100% (6/6), Finish. write object: 100% (6/6), 524 byte | 524.00 KiB/s, Finish. Total 6 (difference 4), multiplexing 0 (difference 0) remote: Powered by GITEE.COM [GNK-6.4] To https://gitee.com/waywtl/study-git.git fb6ad9a..b4794d8 master -> master lijiahao@lijiahao-virtual-machine:~/study_git/study-git$ git log --graph --pretty=oneline --abbrev-commit * b4794d8 (HEAD -> master, origin/master, origin/HEAD) add author * 0b95c7d add comment * fb6ad9a add happy.py * f75243d remove comment * eb25d22 remove author * 13e306c Merge branch 'master' of https://gitee.com/waywtl/study-git
The characteristics of the rebase operation: "organize" the commit history of the fork into a straight line, which looks more intuitive. The disadvantage is that the local fork commit has been modified.
Tag management
create tags
########################################################################### # Put a label on the latest commit (labels are always bound to commits) git tag <tag-name> ########################################################################### # view all tags git tag lijiahao@lijiahao-virtual-machine:~/study_git/study-git$ git tag v0.9 ########################################################################### # To tag historical commit s # First use git log to find the historical commitId, # Then use git tag <tag> <commitId> lijiahao@lijiahao-virtual-machine:~/study_git/study-git$ git log --graph --pretty=oneline --abbrev-commit * b4794d8 (HEAD -> master, tag: v0.9, origin/master, origin/HEAD) add author * 0b95c7d add comment * fb6ad9a add happy.py * f75243d remove comment * eb25d22 remove author # For example tag * f75243d remove comment git tag v0.8 f75243d # Then use git log to view lijiahao@lijiahao-virtual-machine:~/study_git/study-git$ git log --graph --pretty=oneline --abbrev-commit * b4794d8 (HEAD -> master, tag: v0.9, origin/master, origin/HEAD) add author * 0b95c7d add comment * fb6ad9a add happy.py * f75243d (tag: v0.8) remove comment * eb25d22 remove author ########################################################################### # View label information git show <tag> lijiahao@lijiahao-virtual-machine:~/study_git/study-git$ git show v0.8 commit f75243dfe4997d173df10fccc9bfa5e7b1eacc5f (tag: v0.8) Author: waywtl <1148117782@qq.com> Date: Mon Mar 13 11:18:46 2023 +0800 remove comment diff --git a/readme.md b/readme.md ... ########################################################################### # Include text descriptions when labeling git tag -a v0.7 -m "version 0.7 released" eb25d22 lijiahao@lijiahao-virtual-machine:~/study_git/study-git$ git tag -a v0.7 -m "version 0.7 released" eb25d22 lijiahao@lijiahao-virtual-machine:~/study_git/study-git$ git log --graph --pretty=oneline --abbrev-commit * b4794d8 (HEAD -> master, tag: v0.9, origin/master, origin/HEAD) add author * 0b95c7d add comment * fb6ad9a add happy.py * f75243d (tag: v0.8) remove comment * eb25d22 (tag: v0.7) remove author # You can view the explanatory text with git show v0.7 lijiahao@lijiahao-virtual-machine:~/study_git/study-git$ git show v0.7 tag v0.7 Tagger: waywtl <1148117782@qq.com> Date: Mon Mar 13 12:11:17 2023 +0800 version 0.7 released commit eb25d22360f5829408484ed8222f1ba7940dc732 (tag: v0.7) Author: waywtl <1148117782@qq.com> Date: Mon Mar 13 11:18:24 2023 +0800 remove author diff --git a/readme.md b/readme.md ...
Action tab
########################################################################### # delete local tags git tag -d v0.7 ########################################################################### # Push a tag to the remote # A label corresponds to a commit, and pushing a label to a remote means pushing the content of a certain commit to a remote git push origin v0.9 ########################################################################### # Push all the local tags that have not been pushed to the remote at one time git push origin --tags ########################################################################### # If the tag has been pushed to the remote, you need to delete a remote tag git push origin --delete v0.9