current position:Home>Git common commands

Git common commands

2022-01-27 03:46:54 Good tempered girl

GIT Basic operation

establish / Cloning of warehouse

  1. Remote GIT Clone the warehouse locally .
git clone <url>
#  for example  
git clone ssh://url/projectName && git config -f .git/config yourName && git config -f .git/config [email protected]
 Copy code 

git config -f

  1. Create a new project
mkdir projectName
cd projectName
git init
 Copy code 

Create project directory , And use git init initialization , Generate .git Catalog .

Push local changes to remote warehouse

Remote GIT Check out the code in the warehouse to the working directory , When we complete a phase of development , Changes need to be recorded and pushed to a remote location .

git add <fileName> / git add .
git commit / git commit -m "commitMessage"
git push origin branchName
 Copy code 
  • Local development
  • git add Track the required documents ;
  • git commit Submit and prepare submission information ;
  • git push Push this submission to remote warehouse .

Check the status of the local warehouse

The files in the working directory are divided into tracked (staged) And untraceable status (unstaged).git status You can view the tracking status of the file ( Cached 、 Not cached 、 No trace ), That is to say git add and git commit Operation progress .

$ git status
On branch master
Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded.
  (use "git pull" to update your local branch)

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   src/

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   src/

Untracked files:
  (use "git add <file>..." to include in what will be committed)
 Copy code 
  • Changes to be committed: The following file is the tracked status ;
  • Changes not staged for commit: In the back are the tracked files that have changed , But it hasn't been added to the staging area yet ;
  • Untracked files: The following file is untracked .

Need to use git add Tracked files that have changed can be added to the staging area ; You can also change untracked files to tracked status and add them to the staging area .

.gitignore In the file you want to be GIT Ignored file path .

git commit After that, the working directory will be consistent with the last submission , here git status Will prompt :

On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean
 Copy code 

View history submissions

git log All submissions will be listed in chronological order , The latest updates are at the top .

git log <-limit> Such as git log -2 git log --stat -2 Show the last two submissions .limit Limit the number of submissions , The following parameters can be used .

git log -p perhaps git log --patch All differences will be displayed for each submission (diff).

git log --stat The reality of each submission is that all modified documents are listed below 、 How many files have been modified and which lines of the modified files have been removed or added .

git log --name-status Show new 、 modify 、 List of deleted files .

git log --oneline One line shows one submission .

Cancel the operation

1. Undo last commit

Sometimes when we submit it, we find that we missed a few documents and didn't add them , Or the submitted information is wrong . here , Can run with  --amend  Option to submit again . This command commits files in the staging area (git add Later documents ). If you haven't made any changes since the last submission , The snapshot will remain unchanged , And all you have to do is submit information .

git commit --amend Get into vim Edit submission .


Try not to undo / Modify the code that has been pushed to the public warehouse

2. Undo files in staging area

git reset HEAD <file> Revocable git add Files added to the staging area .

3. Undo the change of the document

git checkout -- <file> Revocable files with changes in the working directory that have not been added to the staging area .

Remote warehouse

git remote View remote warehouse server

  • -v It will show the remote warehouse that needs to be read and written Git The saved shorthand corresponds to URL
  • git remote add <shortname> <url> Add a new remote Git Warehouse , At the same time, specify a convenient abbreviation .
  • git remote show <remote> Check out a remote warehouse , for example git remote show origin
  • git remote rename Rename the short name of the remote warehouse . for example git remote rename origin origin2
  • git remote remove origin2 perhaps git remote rm origin2 Remove remote warehouse , All remote tracking branches and configuration information related to the remote warehouse will also be deleted .

git clone  The command will add the remote warehouse itself . Remote under default configuration Git Every version of every file in the warehouse will be pulled down

Keep your code in sync ( Pull and push )

git fetch + git merge / rebase

  1. Get remote branch commit

git fetch <remote> <branch> The command will only remote commit Download the data to the remote branch of your local warehouse , It does not automatically merge or modify your current working directory . have access to git log origin/branch git checkout origin/branch To view the remote branch ; Or manually put it into the working directory .

notes : origin/master and master Not the same branch .

  1. Merge manually

git merge origin/master perhaps git rebase origin/master Command will merge the obtained remote warehouse and local working directory .

git pull

Pull to the remote commit After the data, merge the remote branch to the current branch .

  • git pull origin master = git fetch origin master + git merge origin/master

  • git pull --rebase origin master = git fetch origin master + git rebase origin/master ( be based on rebase Submission of )

git push

git push <remote> <branch> Transfer... From local warehouse commit Push data to remote branch .

Branch merging

The current branch master, take hotfix Branch into master Branch

git checkout master ---> git pull origin hotfix ---> ( Resolve conflicts ---> git add. ---> git commit -m 'merge') ---> git push origin master

GIT skshu

The local GIT The warehouse contains three trees ,

  1. HEAD Is the pointer to the current branch reference , Always point to the last commit of the branch ;
  2. Index Buffer zone , Used to temporarily store local changes ;
  3. Working Directory working directory , Point to the actual local file ;
  • Working Directory git add Index git commit HEAD
  • HEAD git checkout files Working Directory


Reset / Roll back

git reset

7.7 Git Tools - Reset disclosure

git reset It's actually operational git skshu , First of all HEAD Move forward .

  • --soft Move HEAD. Only modified HEAD,Index and Working Directory No change , In essence, it revoked commit At this time, the status in the working directory is cached .
  • --mixed Update index Index. It's moving HEAD Later updated Index, At this time, it is revoked commit And the working directory is not cached .
  • --hard Update the working directory . Updating the index Index after , Undo working directory to and Index The same state , At this time, the commit And undo the local modification , The last submitted modification cannot be replied . dangerous ️ dangerous ️ dangerous ️

git revert

git revert commitId It's a new one commit To roll back the previous commit , The middle submission still exists .

reset and revert Scene

  • If the code of fallback branch is needed later, use git revert
  • If you use the wrong Branch , The code on this branch is useless and doesn't want to be seen by others , Suitable for use git reset

git config

#  Set global user name 、 mailbox 
git config --global "John Smith"
git config --global [email protected]

#  Add instruction alias 
git config --global status
git config --global commit

#  Add instruction alias / Simplify instructions 
# gpush git gpush master => git push origin HEAD:refs/for/master
git config --global alias.gpush '!f() { : push ; r=$1; [[ -z $r ]] && r=origin; b=$2; t=$(git rev-parse --abbrev-ref HEAD); t=${t#refs/heads/}; [[ -z $b ]]; cmd="git push $r HEAD:refs/for/$b"; echo $cmd; echo; $cmd; }; f'
 Copy code 

git checkout The role of

  • git checkout <branch> By moving HEAD Check out the branch
  • git checkout <commitId> A commit based on the current branch , Detect out branches
  • git checkout -b <branch> Create a new branch
  • git checkout <fileUrl> Undo changes to the file

git merge and git rebase The difference between

Reference link git merge and git rebase difference

1 -> 2 -> 3 -> 4 -> master
     └─-> 5 -> 6 -> hotfix
 Copy code 

1. The difference between submitting records

# merge
1 -> 2 -> 3 -> 4 -> 7 -> master/hotfix
     └─-> 5 -> 6 ——─┘
 Copy code 
# rebase
1 -> 2 -> 3 -> 4 -> 5' -> 6' -> master/hotfix
 Copy code 
  • merge Will put public branches and your current commit Merge together , Form a new commit Submit ;
  • rebase Will take your current branch commit At the back of the public branch , Whole commit Submit in a straight line ;
  • merge Can reflect the timeline ; rebase Time is chaotic ;

2. Conflict resolution is different

The current local branch is master Branch

  • merge: In execution git merge Post prompt conflict , Execute again after modification conflict , The execution process is :
    git add . ---> git commit -m 'xx' ---> git pull origin hotfix ---> 
     Resolve conflicts  ---> git add . ---> git commit -m 'xx' ---> 
    git push origin master
     Copy code 
  • rebase: hotfix Each commit of a branch needs to be associated with master Compare the new code , Therefore, it is necessary to solve the conflict circularly , The execution process is :
    git add . ---> git commit -m 'xx' ---> git pull origin hotfix --rebase ---> 
    (`Repeat` Resolve conflicts  ---> git add . --->git rebase --continue )---> 
    git status( Only in working tree clean To submit ) ---> 
    git push origin master –f
     Copy code 

git rebase Use

git rebase -i [startpoint] [endpoint] Can merge multiple commit , among -i representative --interactive Interaction means .

[startpoint] [endpoint] Then an edit interval is specified , If you don't specify [endpoint], The end point of the interval is the current branch by default HEAD The point is commit.

git rebase -i HEAD~2 among HEAD~2 Indicates merging the last two submissions .


## git The command instructions provided to us , In the figure above, each  commit  Previous  pick  For instructions .

pick: Keep it commit( abbreviation :p)

reword: Keep it commit, But I need to modify the commit Notes ( abbreviation :r)

edit: Keep it commit,  But I'm going to stop and revise the submission ( It's not just about changing notes )( abbreviation :e)

squash: Will be commit And the previous one commit Merge ( abbreviation :s)

fixup: Will be commit And the previous one commit Merge , But I don't want to keep the comments that should be submitted ( abbreviation :f)

exec: perform shell command ( abbreviation :x)

drop: I'm going to throw away the commit( abbreviation :d)
 Copy code 

label git tag

  • git tag -a tagName <-m "added description release notes"> Create a label
  • git tag git tag --list/-l see tag
  • git checkout tagName Detection specified tag
  • git tag -d tagName Delete local tag
  • git push origin tagName Will local tag Push to remote

Reference article

copyright notice
author[Good tempered girl],Please bring the original link to reprint, thank you.

Random recommended