Practical use of git in projects

Common command manual

operate instruction illustrate
Initialize warehouse git init Prerequisites for using other instructions
Add files to cache git add <file> Can be used repeatedly
Submit cached files to local repository git commit -m <message> /
View local branch git branch /
View all branches git branch -a Include local/remote branches
Create a branch git branch <name> /
switch branch git checkout <name> Alias ​​git switch <name>
Create + switch branches git checkout -b <name> Alias ​​git switch -c <name>
Merge a branch into the current branch git merge <name> Need to switch to the current branch first
delete branch git branch -d <name> /
Forcefully delete a branch git branch -D <name> The branch has not been merged and cannot be deleted normally.
Go to a commit version git reset --hard <commit_id> /
View commit history git log Easily go back to past versions
View command history go reflog Easy access to future versions
Discard workspace modifications git checkout – <file> Not added to cache yet
Discard cache modifications git reset HEAD <file> Need to perform the previous step again
Restore deleted files from repository git checkout – <file> Requires version library to exist in this file
Restore modified files from repository git checkout – <file> Requires version library to exist in this file
Delete files from repository git rm <file> Need to submit operation
Associated remote library git remote add origin <url.git> origin is the customary name for the remote library
Push the current branch to the remote and associate it git push -u origin <name> Associating current and remote branches, subsequent push/pull can simplify commands.
/ If the remote branch does not exist, a new remote branch will be created.
Push (update) the current branch to the remote git push origin <name> Branches often have the same name
Push (update) the current branch to the remote git push origin Need to associate current and remote branches
Push (update) the current branch to the remote git push It is also required that the current branch has only one remote branch
Clone from remote repository git clone <url.git> /
View remote library information git remote -v /
Pull a remote branch to local git checkout -b <branch> origin/<branch> There should be no next step required
Associate local and remote branches git branch --set-upstream-to <branch> origin/<branch> /
Get the latest version from remote to local and merge git pull Used when the remote branch is newer than the local branch. Need to associate first
Get the latest version from remote to local git fetch Ability to obtain remote new branch information
merge branches git merge /

Common operations

Add modifications to local repository

1. Check status

git status

2. Add all modified files to the staging area

git add .

3. Submit to local warehouse

git commit -m 'xxx'

4. Check status

git status

Add changes to the remote repository

git push

Change the associated remote library

For error reporting fatal: remote origin already exists..

1. Delete the remote Git repository

git remote rm origin

2. Add a remote Git repository

git remote add origin https://github.com/SpringLoach/manager-copy.git

Connect locally and push to remote

Initialize local repository, ignore files

git init 

Create .gitignorefile, ignorenode_modules

Submit to local warehouse

git add .
git commit -m "renew"

Create a new warehouse on github

Push local warehouse association to remote

/* 关联远程库 */  
git remote add origin https://github.com/用户名/仓库名.git

/* 强制重命名分支 */  
git branch -M main

/* 推送当前分支到远程并关联 */ 
git push -u origin main

Clone and modify then push

The first step: git clone xxx // Download the remote warehouse to local

Step 2: git status // View current status

Step 3: git add . or git add xxx // Submit the file to the local cache area

Step 4: git commit -m "submit code" // Submit the file to the local warehouse

Step 5: git pull <remote host name> <remote branch name> // Retrieve the update of a branch from the remote host, and then merge it with the local specified branch.

Step 6: git push <remote host name> <remote branch name> // Push the code currently submitted to the git local warehouse to a remote branch of the remote host

Subsequent push: You can omit the remote host name and remote branch name, and just git push.


Submit operator information

Add --globalthe parameter to indicate that all Git repositories on the machine will use this configuration.

$ git config --global user.name "Your Name"
$ git config --global user.email "[email protected]"  

Verify operator information

$ git config user.name 
$ git config user.email 

Association_Get a branch of the new remote library

order step Order
Clone from the remote repository. By default, only the master branch is pulled.main git clone <url.git>
Pull a remote branch to local git checkout -b <branch> origin/<branch>
View branches and normal operations

Remote library cloning error

Due to network instability and connection timeout, try the cloning command again.

fatal: unable to access 'https://github.com/SpringLoach/test.git/': OpenSSL SSL_read: Connection was reset, errno 10054

Conflict resolution

order step Order
After modification, try to push normally git push
Prompts that the remote submission is ahead of the local one, in which case you can use git pull
The conflicting code portion of the conflicting file will be marked /
Manual modification to resolve conflicts /
Normal operation, push again /

More scenarios

git pull error

Need to specify merge strategy

fatal: Need to specify how to reconcile divergent branches

Specify as the default merge strategy:

git config pull.rebase false

git commit error

Preprocessing file not found

pre-commit hook failed

Ignore validation :

git commit -m "message" --no-verify

Sometimes it is required to use double quotes

git restore

If you commit to the wrong branch, you need to discard the latest changes immediately:

If you are using HbuilderX: Display the log, reset to the version before modification, and then resubmit the push.

related software

TortoiseGit

HbuilderX is a pre-dependency for git operations. For the installation tutorial, please refer to the blog .

Additional words

In the warehouse , it also provides a summary of many common front-end requirements and implementations. Guests are welcome to take a look~

If this note can help you, please help to highlight it on githubstar , thank you!

Guess you like

Origin blog.csdn.net/qq_61270298/article/details/130049296