Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

git common commands


May 30, 2021 Article blog


Table of contents


First, use Git cloning projects

Right-click Git Bash Here to configure your account number and email
git config --global user.name 'account name' git config --global user.email 'email'
Next, use clone to clone the project locally
git clone HTTP path/SSH path
When you're done waiting for the progress, you'll find that the project is already in the local file, and that's when cloning has been proven successful.
Below are commands commonly used by Git for Git beginner reference
View remote warehouse: git remote -v
Add a remote repository: git remote add (name) (url)
Delete remote warehouse: git remote rm (name)
Modify remote repository: git remote set-url --push(name) (newurl)
Pull Remote Warehouse: git pull (remoteName) (localBranchName)
Push Remote Warehouse: git push (remoteName) (localBranchName)

Second, Git branch operation commonly used commands

View local branch: git branch
View remote branch: git branch -r
Create a local branch: git branch (name) ---- at this point you need to be aware that when you use this command, the new branch does not automatically switch to the current branch when it is created
Switch branches: git checkout (name)
Create a new branch and switch to the new branch immediately: git checkout -b (name)
Delete branches: git branch -d (name) ---- note: The -d option can only delete branches that have participated in the merge, and cannot be deleted for branches that do not have a merge. If you want to force the deletion of a branch, you can use the -D option
Merge branches: the git merge (name) ---- merges a branch with the name "name" with the current branch
Create a remote branch (local branch push to remote): git origin push (name)
Delete remote branch: git origin push:heads/name

Third, Git submits common commands for code operations

View file status: git status
Add files: git add s --- note that the item here is all represented, if you need to specify the addition can be changed to the specified file
Commit file: git commit -m 'commit description' --- at this time the commit code has not been committed to the remote exit, but to the buffer that is discharged locally
Push file: git origin push master --- the master here is the main branch, if you need to push to the branch can change the master to the name of the branch
If you want to ignore some files or folders that you don't want to commit, create a file named ".gitignore" at the root of the warehouse, write the unwanted file name or folder name, and each file or folder occupies a row, such as the following:
File A
File B
File C
Can

Fourth, the Git version of the operation of common commands

View version: git tag
Create a version: git tag (name)
Delete version: git tag -d (name)
View remote version: git tag -r
Create a remote version (local version push to remote): git origin push (name)
Delete remote version: git origin push:refs/tags/name

V. Git submo template operation common command (submodule)

Add submodule: git submodule add (url)
Initialize submodule: git submodule init ---- note: Just run it once the first time you check out the warehouse
Update submodule: git submodule update ---- note that you need to run it every time you update or switch branches
To remove a subf template, you need the following steps:
First, enter git rm --cached (path) in Bash Here.
The second is to edit the ".gitmodules" file and remove the relevant configuration nodes for the submodules
Third, edit the ".git/config" file to remove the relevant configuration nodes for the submodules
Finally, manually remove the remaining directory of the submodule

Six, Git delete file operation common commands

Delete files: git rm file .txt
Submit a delete file: git commit -m "rm file"
Push file: git origin push master --- the master here is the main branch, if you need to push to the branch can change the master to the name of the branch
Note: Use the delete command with caution
Recovering files that were deleted locally by mistake: git checkout -- --- overwritten files in the staging area

Seven, Git rollback operation commonly used commands

Query log: git log
Roll back to a committed record: git reset commit_id
Roll back to this commit record: git reset --hard commit_id
Forced push to far end: git push origin HEAD --force
You need to roll back to the wrong commit_id when you delete a recovery by mistake, or if you delete a commit record by mistake, you can recover it by following the code below:
git relog --- copy the hash value of the operation to be resumed
git reset --hard hash --- replace the hash with the hash value of the history to be recovered
Note: It's a good idea not to fall back the remote library with git reset when deleting a commit, because others later use git pull to roll back their local repository to a previous version, making it easy to make mistakes and adding unnecessary effort
git rebase: Two branches are not on a line and use this command when you need to perform a merge operation.

If a commit in the middle needs to be deleted, it can be implemented through the git rebase command by:
git log
git rebase -i commit_id --- replace commit_id with copied values
Enter Vim edit mode and change the 'pick' before the commit to be deleted to 'drop'
Save and exit Vim

Eight, conflict resolution operations commonly used commands

View conflicting content: git diff
Manual conflict resolution: git add or git add -A
If you are still in the rebase state, you can use git rebase -continue to repeat the previous steps until the rebase completes the application
Finally git push submitted
Discard a commit: git revert
Undo merge node commit: you need to add a -m directive, such as git revert commit_id -m 'description'