This’s note sorting in Git practice site, pushing the (learngitbranching.js.org/?demo=&loca…). .
Step by step exercises in pass form and animations to show relationships in Git are very intuitive and fun. Common local and remote instruction instruction exercises are available and can be done in a few hours.
Local Git operations
1. Create a branch and commit
-
Git commit: Leave a commit in the current branch.
-
Git branch
: creates a new branch.
-
Git checkout
: Switch from the current branch to the new branch.
2 and 3 can also be combined with a git checkout -b
implementation to set up and switch directly to the new branch.
2. Merge branches
-
Git merge
: merge branches into main (or master).
If we need to merge main into branch B1, then:
git checkout b1 git merge main Copy the code
Switch branches first, then merge main.
-
Git rebase
: The first description of rebase is to merge commits into one COMMIT, and the second description is to paste an activity to another branch. Given that currently on a BRANCH B1, executing git rebase main copies the current HEAD commits of Main to B1, and maintains main.
3. Intra-branch operations
- HEAD is the most recent commit pointed to under the path and can be used on the current path
git checkout <node name>
To move the HEAD position. ^ Numbers
: indicates that n commit records are moved up at the current branch or HEAD.Reference ^
It is placed after the reference to find the parent node of the reference, for examplemain^
Represents the parent node of main,main^^
Represents the parent node of main^.~
: indicates upward movement,~n
It’s n points up, n is at least 1.git branch -f main HEAD~3
: switches to the main path and points to the parent commit three levels up of the HEAD.- undo
git reset HEAD~1
andgit revert HEAD
. Reset resets the previous commit directly. This makes the commit invisible and is usually used locally. Often used remotely, revert also returns the last commit, but it extends a node to indicate that the last commit was a visible undo.
4. Freely modify the commit tree
git cherry-pick <n1,n2... >
: connects nodes N1, n2, and so on directly to the HEAD of the current path.git rebase -i HEAD~3
: When you want to modify multiple commits in the current path, clickrebase -i
To open up the interface, hide unwanted nodes by tying, change the node order by dragging, and generate a new branch according to the interaction after confirmation.
5. Locally submit miscellaneous items
- Quick submission, available
rebase -i
orcherry-pick
To select the commit to commitgit rebase branch1 main
Is equivalent togit checkout main
addgit rebase branch1
That is, jump from Branch1 to main and connect Branch1 to main. - B1 has created a new branch B2 on a branch, and has committed, now want to return to the old branch B1 for modification.
git rebase -i HEAD~2
: Reorder and create new branches.git commit --amend
: Modifies the HEAD node.git rebase -i HEAD~2
: Reverses the order of the current branch and generates a new branch.git rebase b1 main
Move main to the front of B1.
- The above results in too much sorting, so it works
cherry-pick
To operate:git checkout main
: Returns main.git cherry-pick C2
: Just remove the original C2 point and add it under main.git commit --amend
: Make changes at point C2.git cherry-pick C3
: Then take the child node C3 of C2 and connect it to C2.
- You can use
git tag <tag-name> <n-name>
To tag a node, you can also use checkout to find the node and tag it directly. git describe <ref>
The return value of<tag>_<numCommits>_g<hash>
Hash represents the first few bits of a given ref.
6. Local advancements
- Multinomial rebase, used
git rebase b1 b2
Let’s connect B2 to B1 and set the path to b2. main^2
Represents another parent of main, in addition ^ and ~ can be chain-operated.
Remote repository Git operations
1. Basic operations
-
Git clone: Copy a remote repository to a local repository, or make a remote connection to the local repository. There is usually a branch of o/main after copying locally. This is the remote branch. The naming convention for remote branches is
/
, so the o here is origin.
-
The remote branch enters the detached HEAD state when it is checked out. In order not to have main operations left directly on O/Main. Be aware of the branch you commit to locally.
-
Git fetch: Fetch data from the remote repository (communication, or just download), fetch commit records missing from the local repository, and update the remote branch pointer (e.g. O /main). But it does not change the state of the local repository, update the main branch, or modify the files on disk.
Usually via the Internet (http:// or git:// to communicate with a remote repository).
-
Git pull: Grab the update first and merge it. Git fetch is the same as git merge.
-
Git push: Upload my changes to the designated remote repository and merge my new commit records on the remote repository, meaning that the rest of the team can download my push from the remote repository after the push.
The behavior of push without parameters is related to the Git default configuration. The default value depends on the git version you are using.
-
Git forces developers to merge the latest code before pushing.
- You can fetch the remote repository, then rebase it to the local O /main, move the personal work to the latest commit record, and then use push
git pull --rebase
). - The order of fetch-merge-push is also possible, which differs from Rebase in that local commits are merged with O /main without erasing (as a branch) and then committed to the remote repository as a branch (short for that)
git pull
).
- You can fetch the remote repository, then rebase it to the local O /main, move the personal work to the latest commit record, and then use push
-
However, pushing directly on the master is generally not allowed, and pull Requests need to be configured to submit updates.
Specific operation:
git reset --hard o/main
Reset o/main with the local main.- then
git checkout -b feature C2
Create a new branch node. - The last
git push origin feature
To the remote warehouse.
2. Advanced operations
-
Push main branch: in the case of a large number of personal local warehouses, it is necessary to integrate all branches and push them uniformly.
git fetch
: Fetching updatesgit rebase o/main side1
: Merges and locates to side1git rebase side1 main1
: Merge and locate to maingit push
: Pushes the integrated record to the remote repository
-
In merging remote repositories, you can choose either Rebase or Merge. Rebase has the advantage of a clean commit tree, with all commits on one line. The disadvantage is that the history of the commit tree is modified. Merge preserves commit history.
git checkout main
: Moved to local main.git pull
: Grabs and merges remote repositories.git merge side1
Merges side1 into main (head in main and main in sync with O /main).git push
: Keeps all commit history pushes the same commit record to the remote repository.
-
Remote tracing. At clone time git creates a local remote branch (such as O /main) and keeps track of local women in active branches of the remote repository (such as main).
You can customize any branch to track remote branches, so that the merge target is the branch you set when pushing.
- Properties can be set in the first way
git checkout -b foo o/main
This way Foo can directly trace the main branch of the remote repository when he pulls or pushes.
- Or use the second method:
git branch -u o/main foo
Foo then tracks o/main, and if it’s on foo itself, you can just omit foo.
-
Git push
: specify an argument to push, switch the local branch to get all the commits, and find the branch in origin and add it to the remote repository. If no argument is specified, push is performed at the HEAD position. It can also be used for pull directives. -
argument: The argument above can specify the source and destination -
Next, when the fetch command is used, the source and destination are the opposite of the above. The former is remote, and the latter is local. If there is no local branch, a new branch will be generated automatically. If git Fetch does not specify an argument, commit records are downloaded to all branches.
-
Git push origin :foo removes the remote foo directly by passing a null value to source.
Git fetch Origin :bar creates a new branch locally.
-
Git pull is equal to git fetch and git merge. The only thing a pull cares about is where the commit is merged.