In the last article we introduced the basic concepts of Git. In this article we will talk about branching
branch
A branch in Git is really just a pointer to a commit object, rather than a copy of the current version as in a traditional version control system. Git /refs/heads/git/refs/heads/git/refs/heads/git/refs/heads The master branch contains the last COMMIT ID on the master branch, and the branch represents a chain of commit objects.
HEAD is also a pointer to the current branch, not the commit object. Git has a HEAD file in the.git directory that records the branch to which the HEAD points.
Branch merge
Git branch merge is an example of git branch merge. Let’s assume we are on the master branch
git checkout -b dev
git merge dev
--no-ff
git merge --no-ff dev
Reset
Git reset actually has 3 modes, mixed, soft and hard. If you do not write git reset by default, mixed mode will be called. So what is the difference between the three modes? Reset reset reset reset reset reset reset reset reset reset reset
Stash
So what happens with this git stash? Let’s say when we’re working on Feature1 and we need to work on Feature2 urgently, we need to temporarily save the stuff from Feature1 with this Git stash and work on Feature2. Then go back to Feature1 and pull out the changes you saved using a Git Stash pop. It should be noted that git Stash remembers the current commit ID, so if the stash changes the same file and the same line twice under the same commit, there will be a conflict after the first recovery and the second recovery.
Tag
A tag is a tag object that we mentioned earlier, and it also points to a commit object. The difference between a tag and a branch is that when a commit occurs, the branch moves forward, but once the tag is set, it doesn’t move any more, so it’s usually used when our project reaches a milestone, Used when a new version needs to be released. Git tags are classified into two types: Lightweight and annotated. The difference between lightweight and annotated tags is that one has description information and the other doesn’t.
Diff
Git diff uses the Linux diff module to compare files between files
instruction
-
View branch list
git branch
-
Create a branch
Git branch Specifies the branch name
-
Switch branch
Git Checkout branch name
-
Switch to the branch where you were last time
git checkout -
-
Delete branch ()
Git branch -d Specifies the branch name
You cannot delete a branch that is currently in use unless you use git branch -d to merge a branch
-
Create a new branch and switch to the new branch
Git checkout -b
-
Displays the most recent commit message for the current branch
git branch -v
-
Merges branches into the current branch
Git merge branch name
-
If fast-forward is disabled, a commit ID is added
Git merge --no-ff branch name
-
Fallback to last Commit (based on current COMMIT)
git reset --hard HEAD^
-
Fallback to last commit (both based on current COMMIT)
git reset --hard HEAD^^
-
The last n commits that fall back to the current branch (n before the current COMMIT)
git reset --hard HEAD~n
-
Fall back to specify commit
Git reset --hard commit
-
Changing the branch name
Git branch -m Old branch name New branch name
-
Save the workspace changes
git stash
-
List all saves
git stash list
-
Manually set stash description
The default git stash return is
This is the message that contains the latest commit, so you can modify the description with the following command
git stash save 'hello basic'
-
Restores the last save and deletes it from the list
git stash pop
-
Restores the most recent save, but will not be deleted from the list
git stash apply
-
Apply specifies a version and saves it in the list for deletion
git stash apply stash@{0}
-
Manually deletes a specified saved version
git stash drop stash@{0}
-
Create lightweight labels
Git tag v1.0.1
-
Create an annotation label
Git tag-a v1.0.2 -m 'release 1.0.2'
-
Check the label
Git tag allows you to view all tags, as well as git tag show
-
Find the label
Git tag -l ‘v1.0’ git tag -l ‘v1.0’ git tag -l ‘v1.0
-
Remove the label
Git tag -d Specifies the tag name
-
List each row that was modified by who, at what time and by which commit
Git Blame
-
Compares the difference between the current file and the workspace file that has been modified into the staging area
git diff
-
Compare the current latest COMMIT with the workspace
git diff HEAD
-
Compare a COMMIT with a workspace
git diff commit_id
-
Compares the difference between the latest commit and the staging area
git diff --cached
-
Compare a COMMIT with a staging area
git diff --cached commit_id
-
Push local tags that have not been submitted once
git push origin --tags
-
Deleting a Remote Label
You can push an empty tag by calling Git push Origin –delete tag name, or by using Git push Origin :refs/tags/v6.0
-
Clipping the current remote branch
Sometimes, user A may execute the command to delete the remote A1 branch. Then, user B will find that the remote A1 branch is stale when he executes git remote show Origin, that is, the remote branch has disappeared, but the local branch still exists. Git remote Prune Origin will delete the stale remote branch
scenario
-
When we use
git reset --hard
Execute when the instruction falls back to the previous COMMITgit log
If I want to go back to a later commit, how do I get my COMMIT ID?Git reflog: git reflog: git reflog: git reflog: git reflog: git reflog: git reflog Do not manually modify files in the. Git directory, because manual modification will not be recorded in reflog
-
I accidentally used a branch
Git branch -d Specifies the branch name
Forcibly deleted, but also saved back?Git reflog (); git reflog (); git reflog (); git reflog (); git reflog (); Then execute git branch with the branch name commit-id to recreate our branch. Git merge commit-id specifies the name of the commit chain to be merged. Git merge commit-id specifies the name of the commit chain to be merged.
Q&A
-
git checkout commit-id
与git reset --hard commit-id
What’s the difference?Both can return to the corresponding commit point. However, unlike reset, checkout will be in a dissociated state. If we do not commit any changes, we will be warned not to jump to another commit. Git branch (); git branch (); git branch ();