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
- Remote GIT Clone the warehouse locally .
git clone <url>
# for example
git clone ssh://url/projectName && git config -f .git/config user.name yourName && git config -f .git/config user.email [email protected]
Copy code
- 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/test.md
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/test.md
Untracked files:
(use "git add <file>..." to include in what will be committed)
src/readme.md
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 URLgit 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 examplegit remote show origin
git remote rename
Rename the short name of the remote warehouse . for examplegit remote rename origin origin2
git remote remove origin2
perhapsgit 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
- 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 .
- 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 ,
- HEAD Is the pointer to the current branch reference , Always point to the last commit of the branch ;
- Index Buffer zone , Used to temporarily store local changes ;
- Working Directory working directory , Point to the actual local file ;
- Working Directory
git add
Indexgit 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 modifiedHEAD
,Index
andWorking Directory
No change , In essence, it revokedcommit
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 revokedcommit
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, thecommit
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 user.name "John Smith"
git config --global user.email [email protected]
# Add instruction alias
git config --global alias.st status
git config --global alias.ci 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 movingHEAD
Check out the branchgit checkout <commitId>
A commit based on the current branch , Detect out branchesgit checkout -b <branch>
Create a new branchgit 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 labelgit tag
git tag --list/-l
see taggit checkout tagName
Detection specified taggit tag -d tagName
Delete local taggit push origin tagName
Will local tag Push to remote
copyright notice
author[Good tempered girl],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/01/202201270346479974.html
The sidebar is recommended
- Spring IOC container loading process
- [thinking] the difference between singleton mode and static method - object-oriented programming
- Hadoop environment setup (MySQL environment configuration)
- 10 minutes, using node JS creates a real-time early warning system for bad weather!
- Git tool
- Force deduction algorithm - 92 Reverse linked list II
- What is the sub problem of dynamic programming?
- C / C + +: static keyword summary
- Idea does not have the artifacts option when configuring Tomcat
- Anaconda can't open it
guess what you like
-
I don't know how to start this
-
Matlab simulation of transportation optimization algorithm based on PSO
-
MySQL slow log optimization
-
[Vue] as the window is stretched (larger, smaller, wider and higher), the text will not be displayed
-
Popular Linux distributions for embedded computing
-
Suzhou computer research
-
After installing SSL Certificate in Windows + tomcat, the domain name request is not successful. Please answer!!
-
Implementation time output and greetings of jQuery instance
-
The 72 year old uncle became popular. Wu Jing and Guo fan made his story into a film, which made countless dreamers blush
-
How to save computer research
Random recommended
- Springboot implements excel import and export, which is easy to use, and poi can be thrown away
- The final examination subjects of a class are mathematical programming, and the scores are sorted and output from high to low
- Two pronged approach, Tsinghua Professor Pro code JDK and hotspot source code notes, one-time learning to understand
- C + + recursive knapsack problem
- The use of GIT and GitHub and the latest git tutorial are easy to understand -- Video notes of crazy God speaking
- PostgreSQL statement query
- Ignition database test
- Context didn't understand why he got a high salary?, Nginxfair principle
- Bootstrap switch switch control user's guide, springcloud actual combat video
- A list that contains only strings. What other search methods can be used except sequential search
- [matlab path planning] multi ant colony algorithm grid map path planning [including GUI source code 650]
- [matlab path planning] improved genetic algorithm grid map path planning [including source code phase 525]
- Iinternet network path management system
- Appium settings app is not running after 5000ms
- Reactnative foundation - 07 (background image, status bar, statusbar)
- Reactnative foundation - 04 (custom rpx)
- If you want an embedded database (H2, hsql or Derby), please put it on the classpath
- When using stm32g070 Hal library, if you want to write to flash, you must perform an erase. If you don't let it, you can't write continuously.
- Linux checks where the software is installed and what files are installed
- SQL statement fuzzy query and time interval filtering
- 69. Sqrt (x) (c + + problem solving version with vs runnable source program)
- Fresh students are about to graduate. Do you choose Java development or big data?
- Java project: OA management system (java + SSM + bootstrap + MySQL + JSP)
- Titanic passenger survival prediction
- Vectorization of deep learning formula
- Configuration and use of private image warehouse of microservice architect docker
- Relearn JavaScript events
- For someone, delete return 1 and return 0
- How does Java dynamically obtain what type of data is passed? It is used to judge whether the data is the same, dynamic data type
- How does the database cow optimize SQL?
- [data structure] chain structure of binary tree (pre order traversal) (middle order traversal) (post order traversal) (sequence traversal)
- Webpack packaging optimization solution
- 5. Operation element
- Detailed explanation of red and black trees
- redhat7. 9 install database 19C
- Blue Bridge Cup notes: (the given elements are not repeated) complete arrangement (arrangement cannot be repeated, arrangement can be repeated)
- Detailed explanation of springboot default package scanning mechanism and @ componentscan specified scanning path
- How to solve the run-time exception of test times
- Detailed explanation of k8s management tool kubectl
- Android system view memory command