current position:Home>Git common command quick reference manual [produced by man san dao sauce]

Git common command quick reference manual [produced by man san dao sauce]

2022-01-27 01:30:37 Mansandao sauce

Preface

In your daily development , There will be many Git The operation of , But you've never done , This article is a Book Git Command quick reference manual , It summarizes the most commonly used in my work in recent years Git usage . I believe it is helpful for daily development .

Most of the following common methods and commands only involve using , It doesn't involve the principle explanation . If in doubt , Please leave a message ~

General function

Git Commit Statute

feat: new function (feature)
fix: repair bug
docs: file (documentation)
style:  Format ( Changes that do not affect code operation )
refactor: restructure ( It's not new , It's not a revision bug Code changes )
test: Add tests 
chore: Changes in the build process or AIDS 

git Ignore managed files and folders

Only later git already tracking There are some files you want to ignore , At this time, only adding the folder you want to ignore in the file will not take effect . Can be interpreted as With cache , We need to delete it manually tracking The file of , Execute the following command :

git rm -r --cached ignoreFile(ignoreFile Is the file you want to ignore ), Give Way git No longer tracking These documents .

How to use specific SSH Key Submit GIT

https://www.jianshu.com/p/82aa1678411e

Git overall situation username And current project username

Study git When , Before we started using it, we all configured a global user name and mailbox

git config --global user.name "github's Name"

git config --global user.email "[email protected]"

git config --list

If your company's project is self built gitlab above , If you don't configure the user name and email , The global , This time is wrong , The right approach is to target the company's projects , Configure it separately under the root directory of the project

git config user.name "xxxxxxx"

git config user.email "[email protected]"

git config --list

View the current configuration , The configuration viewed under the current project is the global configuration + Configuration of the current project , The configuration of the current project will be used first

Global Config and Warehouse Config

Do not modify on the machine used together Global config, Can cause commit It's all your name .

Please use folder config (–local),git Priority will be given to detecting folders config And look for global config

Modify the submitted author information

The answers given online are all scripts written by themselves , A little too cumbersome , Walking around segmentfault1 Found the answer :

First find the modification commit Previous , perform

git rebase -i commit id

git It will automatically call the configured editor to open an interface

Modify the first row of data ( It's the one we expect to modify commit) Of pick by edit

Then we can go through git commit --amend To modify user information freely , The operation is as follows :

git commit --amend --author="xxx <[email protected]>" --no-edit

Continue to complete rebase,

git rebase --continue

Git hit tag Tag Push to remote warehouse

https://www.liaoxuefeng.com/wiki/896043488029600/902335212905824

hit tag:

git tag v1.0

In order to synchronize tags to remote servers , We can do that :

By default ,git push It doesn't mean that tag Transfer the tag to the remote server , Tags can only be shared to remote warehouses through explicit commands .

1.push Single tag, The command format is :

git push origin [tagname]

for example :
git push origin v1.0 # Will local v1.0 Of tag Push to remote server

2.push all tag, The command format is :

git push [origin] --tags for example :git push --tags or git push origin --tags

Windows Unable to create file name

As far as understand, you have the following options:

  • ask project author to rename the file/directory in question or do it yourself
  • try using git from a cygwin/msys2 build. The msys2 wrapper handles some Windows path issues, transforming them into legal ones, though I don’t know if it handles this issue.
  • fix git for windows - take into use the UNC path. The maintainer said he would consider taking such patch.
  • if you don’t really need to work on the file or directory, you could exclude it from checkout using sparse checkout.

clone Failure to deal with ( Cannot create special file name )

git statusgit checkout -f HEAD

modify remote Warehouse url

git remote set-url origin [url]

An existing local project is associated with a new project Git Warehouse

https://blog.csdn.net/u013325929/article/details/70313414

Compress commit

https://blog.csdn.net/itfootball/article/details/44154121

https://www.jianshu.com/p/43c60db5fadb

  1. First Rebase:
git rebase -i fb6ae281bad35fb70f15b93c22ef5ccf50ced238
  1. modify commit notes
  2. Strong push
git push -f

Will be a lot commit Compress to a few

Forcibly replace local with remote

Sometimes the same branch , Both remote and local have been modified beyond recognition , If you want to replace local with remote , Use the following command

git fetch --all
git reset --hard origin/master ( here master To change to the corresponding branch name )
git pull

modify commit notes

git commit --amend

submodule

Method 1 :

git clone --recursive xxxxx

Method 2 :

git submodule init 

git submodule update

submodule Change the warehouse url

https://stackoverflow.com/questions/913701/how-to-change-the-remote-repository-for-a-git-submodule

You should just be able to edit the .gitmodules file to update the URL and then run git submodule sync --recursive to reflect that change to the superproject and your working copy.

Then you need to go to the .git/modules/path_to_submodule dir and change its config file to update git path.

If repo history is different then you need to checkout new branch manually:

git submodule sync --recursive
cd <submodule_dir> 

git fetch
git checkout origin/master
git branch master -f
git checkout master

Fallback related functions

After execution commit after , withdraw commit

git reset --soft HEAD^

So you've successfully withdrawn your commit

Be careful , Just to withdraw commit operation , The code you wrote remains .

HEAD^ It means the last version , Or you could write it as HEAD~1

If you do 2 Time commit, I want to withdraw , have access to HEAD~2

As for these parameters :

–mixed( Default )
intend : Don't delete workspace change code , revoke commit, And cancel git add . operation
This is the default parameter ,git reset --mixed HEAD^ and git reset HEAD^ The effect is the same .

–soft
Don't delete workspace change code , revoke commit, No cancellation git add .

–hard
Delete workspace change code , revoke commit, revoke git add .
Note that after this operation , It's back to the last time commit state .

Go back completely, one that has push Remote to the warehouse commit

git reset --hard a7e1d279
git push -f origin master

After the local branch is rolled back , The version will lag behind the remote branch , The remote branch must be overwritten with forced push , Otherwise, it cannot be pushed to the remote branch

Recover the missing code that was pushed

Git Meeting time gc, Clean up reflog, So don't wait too long to recover after being covered .

  1. Back up the data in the current workspace

  2. git reflog/git log -g

Show all historical operations , Find the submission you need ( Including those that have been deleted commit Record ,git log You can't view the deleted commit Record )

2.1 Force back to what was deleted at that time commit

git reset --hard <SHA1>

2.2 Or directly

git cherry-pick <SHA1>

Just bring back the work of that version . But if there's a conflict, you have to deal with it .

3. Push the remote branch

git push -f origin <branch>

If it causes commit The reason for the loss is not recorded in reflog in , Like running rm -Rf .git/logs/, because reflog The data is stored in .git/logs/ In the catalog , So there's no reflog 了 .

have access to git fsck Tools , The tool checks the data integrity of the warehouse . If specified --full Options , This command shows all objects that are not referenced by other objects ( Point to ) All objects of .

then , It can be restored in the same way , That is to create a point to the SHA The branch of .

Branch related functions

Git Switch local branches

git checkout my-test 

Merge dev to master Branch

First switch to master On the branch

git checkout master

If it's multi person development It needs to be remotely master The code on pull Come down

git pull origin master

If it is a development of their own, there is no need , For the insurance period or pull
And then we put dev The code of the branch is merged into master On

git  merge dev

Again ,dev Pull master Branch

equally , stay dev Branch use git merge master

View the correspondence between remote branches associated with local branches

git branch -vv

One line of code directly pulls down the remote dev Branch

git checkout -b  Local branch name x origin/ Remote branch name x

To create local dev Branch and associate with remote dev Branch

Suppose the current branch is master, The branch to be created is my-test

git checkout -b my-test  // Create... Under the current branch my-test Local branch of git branch --set-upstream-to=origin/my-test // Take the local branch my-test Associate to remote branch my-test On    git branch -a // View remote branches 

At this point, the remote branch my-test It's already created , And the local branch has been associated with the remote branch
Local push The code will push To the associated remote branch .

To create local dev Branch and create a new remote branch

git checkout -b my-test  git push origin my-test:my-testgit branch --set-upstream-to=origin/my-test

Delete the branch ( Local 、 long-range )

Delete local branch :git branch -d Branch name (remotes/origin/ Branch name )

Force local deletion :git branch -D Branch name

Delete remote branch :git push origin --delete Branch name

copyright notice
author[Mansandao sauce],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/01/202201270130334981.html

Random recommended