Undertake Quick Start Using Git One, the previous section talked about initializing a Git repository and tracking files. This section will cover branches, remote repositories, rebasing and merging, and briefly mention the usage scenarios of stash and GPG authentication.
Branch Creation, Switching, Deletion#
git branch
# Output
* main
This command can query the number of branches in the current repository and the current branch (main). After the initial commit in Git, a master/main branch will be automatically created.
Creating a Branch#
git branch dev
# Create a branch named dev, but it won't switch to the dev branch, still on the main branch at this time
Switching Branches#
git checkout dev
# Switch the current branch from main to dev, this step can also be combined with the previous step
git checkout -b dev1
# Create the dev1 branch directly and switch to the dev1 branch
Deleting a Branch#
git branch -d dev1
# Delete the branch named dev1
Branch Merging#
Merge and rebase are two different methods for handling branch merging.
Merging#
# The following demonstrates merging using merge
git checkout main
git merge dev
Rebasing#
# The following is the method of rebasing the dev branch
git checkout main
git rebase dev
Summary#
Merge and rebase are both methods of merging branches, with the following differences:
- Merge merges two branches and creates a commit
- Rebase completely merges two branches into one branch
The diagrams are as follows:
merge
Merge is suitable for collaborative development environments, such as using the dev branch to develop a new feature and merging it into the main branch using merge.
rebase
Rebase is suitable for personal development processes, such as developing a project individually and using rebase to keep all commit records in one branch for a clean history.
Remote Repository#
Using Github as an example
git remote add origin https://github.com/kyleten/ddd
# Name the URL link as origin
After each commit, you can use
git push origin
# Push the commit to the remote repository
# The first time you need to set it up
git push -u origin
Usage Scenarios of Stash#
In multi-person development, when you are working on a feature (which is not completed and you don't want to commit it) but need to pull updates from the remote repository, you need to use stash to save the current workspace.
git stash -m 'stack1'
# Stash the current content with the remark stack1
After completion, you can use the following command to restore the workspace
git stash pop
# Restore the most recent stash
# You can also specify a specific stash number, first use list to view the numbers
git stash list
# Output
stash@{0}: On main: stack1
# Use the following command to restore
stash pop 0
GPG Signature#
The specific steps of creating a key, installing software, and creating a signing subkey are not covered in this article.
Add to Configuration#
git config --global user.signingkey xxxxxxxxx!
# The ! is used to specify a specific subkey, remove it if there is only one key
git config --global commit.gpgsign true
# If it cannot be used normally in Windows, use the following command
git config --global gpg.program 'path\to\bin\gpg.exe'