First of all, I recommend this website. You can see the branch changes under each command:Learngitbranching.js.org/?locale=zh_…

Operations between workspace & staging area & local warehouse

Git has such three areas on the local computer, and the normal workflow is:

Workspace -1-> staging area -2-> local warehouseCopy the code

The first command is git add, and the second command is git commit.

The staging area is in the middle and is either the same as the workspace or the local repository.

If you want to undo the workspace changes and make the workspace contents consistent with the local repository HEAD version, you can use the following command:

git checkout -- filename
Copy the code

However, the change to the staging area will not be withdrawn, so before this command, it is best to execute first

git reset HEAD (filename)
Copy the code

To undo the temporary area change.

Branch management

Create a new branch:

git branch branchname
Copy the code

Toggle the current branch (change the HEAD pointer position):

git checkout branchname
git checkout HEAD^^
git checkout hashvalue~3
Copy the code

You can use the git stash command when you need to switch to another branch because you don’t want to commit on one branch yet.

git stash # Change in storage work area and temporary storage area
git stash pop # Restore the last stored content
Copy the code

Branch pointer rollback:

git reset HEAD~n
Copy the code

The implication of this command is to move the current branch pointer back n versions and undo the change in the staging area. In this way, the workspace is the content of the new version, and the local repository is the content of the old version after the pointer is moved.

If you want to “remove” a committed change, you need to run the following command:

git revert hashvalue
Copy the code

This command removes a commit change by committing a new commit, which does not cause the above problems.

Branch merge

There are two ways to merge branches, namely git merge and Git Rebase.

Git merge means merge. Git merge branch1 will merge branch1 into the current branch.

Git rebase branch1 resets the current branch to branch1, with the branch1 pointer unchanged.

When typing these two commands, I think you should pay attention to two things:

  1. Both of these commands are potentially in conflict
  2. If you’re confused about how the branch moves, remember: These two commandsbranch1The location of the branch represented by the parameter is unchanged

Remote warehouse

Another important feature of Git is the management of remote repositories to facilitate collaboration.

Clone branch:

git clone <u>url</u>
Copy the code

To synchronize a remote branch to the local:

git pull
Copy the code

To synchronize local branches to remote:

git push
Copy the code

Because multiple people collaborate by fitting into branches, there is potential for conflict.

On the other hand, when you close branches, the primary branch may not be the same as when you cut the branch, so you need to synchronize the remote repository to the local location.

Git merge

git fetch
git merge o/master
# Handle possible conflicts
git push
Copy the code

Simplified version:

git pull
# Handle possible conflicts
git push
Copy the code

Git Rebase

git fetch
git rebase o/master
# Handle possible conflicts
git push
Copy the code

Simplified version:

git pull --rebase
# Handle possible conflicts
git push
Copy the code

About git multi-user configuration

When the local host needs to communicate with the remote repository as only one identity, the following steps are needed to configure the user name, mailbox, and SSH key.

git config --global user.name "xxx"
git config --global user.email "[email protected]"
ssh-keygen -t rsa -C "[email protected]"
Copy the code

However, when the machine needs more than one identity, such as github&GitLab, it needs to be configured properly.

SSH /config file is required for the key. The format is as follows:

Host github.com
User xxx1
Hostname github.com
IdentityFile ~/.ssh/id_rsa_xxx1

Host gitlab.com
User xxx2
Hostname gitlab.com
IdentityFile ~/.ssh/id_rsa_xxx2
Copy the code

Be aware that you are using a different mailbox when generating the key, and you need to change the default ID_RSA file name to distinguish between the two cases.

Then look at the configuration of user name and mailbox. I set the global user name and mailbox first. For example, set the github user name and mailbox to global.

Next, when using GitLab’s repository, start with a local username and mailbox configuration, which can be set to an alias:

# ~/.bashrc
alias github-user="git config user.name 'xxx2'; git config user.email '[email protected]'"
Copy the code

The first time you use the GitLab repository, just execute github-user.