Original link: my blog garden article
Git installation and configuration and common commands
- Download the installation package from the official website and install it manually.
- Open the
Git Bash
Command line tool to execute commandsssh-keygen -t rsa -C Email-Addresss
Generate a key pair. - Log in to
GitLab
, click on your user profile picture in the upper right corner, and clickEdit Profile settings
, click on theSSH Keys
, click on theAdd SSH Key
And fill inTitle
Bar, copy user directory under.ssh/id_rsa.pub
The contents of the documentKey
, click on theAdd Key
. - Click on the top right
New project
, and then clickCreate project
To create a new warehouse, clickActivity
, click on theSSH
After the copySSH
The address in the sidebar. - Open the
Git Bash
Command line tool, switch to an appropriate directory, use the commandgit clone
‘Just copied URL’ clone created by repository. - Go to the directory CD repository name and run the command
git config --global user.email your-email
, set up your emailgit config --global user.name your-name
, set your name.
- Execute command:
echo "# Description" > README.md
, add a filegit status
Status. Untraced files were foundgit add .
, all files in the current directory are added to the staging areagit diff
Compare the difference between the current workspace and the staging areagit status
To view the current status, the file is not submittedGit commit -m
To submit the contents of the staging area to the local repositorygit push -u origin master
To push a commit from the local repository to the remote repositorygit log
To view the commit log
- Simulate remote update login to
GitLab
, click on theFiles
TAB, clickREADME.md
File, clickEdit
Button to modify the file content, add a new line: “* 9 remote modify file record”, you can clickPreview
Preview, you can also fill in comments and save directly. - Execute command:
git pull
, pull the update commit of the remote repository and do the automatic merge, which may cause conflictsgit fetch
To get the update submission of the remote repository, do not do automatic merge, need to manually mergegit merge origin/master
To manually merge the remote update commit locallymaster
branch
- Configuring command Alias
git config --global alias.co checkout
To configure the check out command aliasgit config --global alias.br branch
To configure the alias of the branch commandgit config --global alias.ci commit
To configure the alias of the submit commandgit config --global alias.st status
To configure the current status command aliasgit config --global alias.df diff
To configure the alias of the comparison commandgit config --global alias.pl pull
To configure the alias of the pull commandgit config --global alias.pu push
To configure the alias of the push commandgit config --list
To view the configuration listgit config --global credential.helper store
When configured to remember passwords to USE HTTPS
- Other commands
git clean -f
To clear untraced filesgit checkout .
To clear workspace changesgit checkout some-branch-name file-name.js
, checks out the specified file for the specified branchgit checkout {{some-commit-hash}} file-name.js
, checks out the specified file for the specified commit
- other
* Git does not manage empty directories. If you have an empty directory, you will never commit it to the repository. * Git will automatically recognize the command name operation based on the file name. Gitignore files can be deleted and then added to the.git equivalent directory. The file can be written to the ignored file information, which will be automatically ignored when checking status and submitting. Git sparse check out tutorial: Pro Githelp 'command'There are also some other features, such as rebase, RM, reset, remote, Blame, cherry pick, MergeTool, * DiffTool, which are not covered in this tutorial and will be updated graduallyCopy the code
Git local branch management
Create, merge, delete, compare branches
git branch
, displays all pointsgit branch b1
Create a branch called b1 from the current branchgit checkout b1
, switch to branch B1git checkout -b b1
Is equivalent to the combination of the preceding two commandsgit checkout master
, switch to themaster
The main branchgit merge b1
, merge the b1 branch code intomaster
ongit branch -d b1
To delete branch B1, this operation cannot be performed on the deleted branchgit diff branch-1 branch-2
Compare the differences between two branchesgit diff --name-only branch-1 branch-2
, displays only the names of files with two different branchesgit diff some-branch some-filename.js
Compares the current branch to another branch specified file
Git remote branch management
Create, merge, and delete remote branches
git push origin dev
To push the local branch dev to the Origin remote repositorygit pull origin dev
, pull the remote repository Origin dev branch and merge it automaticallygit push orgin :dev
Delete the remote branch on the origin remote repositorygit push origin --delete dev
This command is available after version 1.7.git remote prune origin
To clean up the local trace branch, add the -dry-run parametergit pull --rebase
, and pull the update in the form of change basis
Tips: Remote management of tags is similar to remote branch management
Git Tag management
Create and delete labels
git tag t1
Create a label named T1 from the current branchGit tag -m 'comment' t1
Create a label with commentsgit tag -d t1
Delete the label named T1
The Git Log Log
git log
To view historical logsgit log --graph
To display the merge trajectory with text-based graphicsgit log --pretty=oneline
One line displays brief log informationgit log --pretty=format:"%h - %an, %ar : %s"
To view logs in the specified format. For the format parameter, go to the official manualgit log --pretty="%h - %s" --author=gitster --since="2008-10-01" --before="2008-11-01"--no-merges
, displays logs from the specified date range and the specified submitter in the specified format, excluding merge commitsgit log -p -2 --stat
-p displays file differences, -2 displays the last two submits, and -stat displays statistics about the number of modified rowsgit log -p filename
To view detailed changes to the file, including actual changes to the fileThe git log - L, 1, 1: some - file. TXT
To view changes at the specified location in the filegit log --no-merges master..
To view changes that have not been merged into the Master branch
Other advanced commands
git show --no-merges master..
, function as abovegit show some-branch:some-file.js
To view files of other branches without switching branches
General usage
1. The cloning
git clone< warehouse address >Copy the code
2. View all branches
git branch -a
The origin/dev branch has been created for the server by default. The origin/dev branch has been created for the server by default. The origin/dev branch has been created for the server by default.
Git clone: create a local dev branch and push it to the server. git branch dev ; git checkout dev ; Git push.
# Method 2: Create a dev branch directly on Gilab
Copy the code
3. Switch to the dev branch
git checkout dev Git checkout -b dev origin/dev
Copy the code
4. Create your own branch based on the dev branch
Git checkout dev git branchCopy the code
5. Switch to < branch name > and then happily coding
Git checkoutCopy the code
6. Add it to the local staging area
git add ./
Copy the code
7. Check the working area status at any time
# check to see if there are untraced files in the current branch workspace (that is, if there are files not added to the staging area)
git status
Check the difference between the files in the current branch workspace and those in the staging area
git diff
Copy the code
8. Submit it to the local warehouse
git commit -m "Description"
Copy the code
9. Push to the server repository
You can also not push to the server, but only put it in a local repository, such as a temporary branch that only I use
Git push --set-upstream origin#-- Set association, will also be pushed incidentally, only used for the first time
git push origin
Copy the code
10. Merge to dev branch (example)
A. Developer request merge on web page (recommended)
Web page request merger can conveniently conduct code review, set milestones such as reference: gitlab how to implement the code review mechanism (controlling) through role: www.jianshu.com/p/adb598c52…
If a conflict occurs, the developer pulls down the latest version of the server and then merges the dev branch into his own branch
Git merge --no-ff devCopy the code
If there is a conflict, resolve it by referring to the reference link above, or by following the method in B. Master role directly local merge
B.Master Directly merges roles locally
To merge a local branch into a local dev branch, you must first pull the latest code from the server and switch to the dev branch, and then merge:
Git checkout dev git merge --no-ffCopy the code
Merge command, if there is a conflict, you need to manually resolve the conflict, open the conflict file, you can see something like:
. 111 <<<<<<< HEAD 222 masterdo it
=======
222 source do it
333 sourceJJJ >>>>>>> < branch name >...Copy the code
Git add. Git add. git commit ; Git merge) :
git add .
git commit -m "For merge instructions"Git merge --no-ff < branch name >Copy the code
In case of conflict, merge can also be undone:
git merge --abort
Copy the code
11. Delete branch, switch to master branch and delete < branch name > branch (cannot delete current branch)
git checkout master
git branch -d< branch name >Copy the code
12. Delete remote, dangerous, remember to only delete your own branch, do not delete public branches, such as dev, release, etc
Git push origin:Copy the code
Introduction and routine use of GitLab
GitLib has five identity permissions, which are:
Owner
The project owner, who has all operation rightsMaster
The manager of the project can perform other operations except to change or delete the project meta informationDeveloper
The developer of the project, who does some development work, has no access to the protected contentReporter
The reporter for the project, who only has read permission for the project, can create code snippetsGuest
Visitors to the project can only submit questions and comment content
For details, see GitLab Permissions to specify the identity permissions of members when adding members to a project.
Naming rules
- Each submission must be annotated, and if it is a Bug fix, please add a Bug number
- Create a feature branch with a name starting with f-, followed by the feature name
- Create a release branch with a name starting with r- and a pre-release number
- Create a Bug fix branch with a name starting with b- and a Bug number
- Create a label with a name starting with t- and a release number
- The –no-ff parameter must be used when merging branches (fast forward merging is prohibited) to preserve the merge history
Main branch (Protection branch)
master
Main branch, stable code, ready for productiondevelop
Development branch, for development services
Feature branch
- from
develop
Branch creation, used for feature development, to be merged back after completiondevelop
Branch. git checkout -b newfeature develop
, fromdevelop
Branch to createnewfeature
Feature branchgit checkout develop
After the development is complete, it needs to merge backdevelop
Branch, let’s switch todevelop
branchgit merge --no-ff newfeature
And merge backdevelop
Branches, you have to add--no-ff
parametergit branch -d newfeature
Delete the feature branchgit push origin develop
, put the mergeddevelop
Branch push to remote repository
Release branch
Created from the Develop branch for pre-release versions to allow minor bug fixes and then merged back into Develop and Master.
Git checkou-b release-1.2 develop
Create a publishing branchgit checkout master
, switch to themaster
Branch, ready to mergeGit merge - no - ff in release 1.2
,Release 1.2
Branch merge intomaster
branchGit tag 1.2
.From the master
The branch is labeledgit checkou develop
, switch to thedevelop
Branch, ready to mergeGit merge - no - ff in release 1.2
,Release 1.2
Branch merge intodevelop
branchGit branch - d release 1.2
Delete the publishing branch
Repair branch
Created from the Master branch for Bug fixes on production and merged back into Develop and Master.
Git checkout -b hotfix-1.2.1 master
, frommaster
Branch Creates a Bug fix branchgit checkout master
, switch to themaster
Branch, ready to mergeGit merge - no - ff hotfix - 1.2.1
And incorporated into themaster
branchGit tag 1.2.1
Create a label for the master branchgit checkout develop
, switch to the Develop branch and prepare to mergeGit merge - no - ff hotfix - 1.2.1
And incorporated into thedevelop
branchGit branch - d hotfix - 1.2.1
, delete theHotfix - 1.2.1
branch
Git is different from SVN
1. Developer’s local?
SVN: Only one version of the code
Git: All versions of the entire repository
2. In the case of no network, can the development also carry out version control
SVN: No, the SVN does not have self-version control
Git: Yes, git has a local repository and local version control
3. Most commonly used commands
svn: checkout/commit/update
git: clone/add/commit/push/pull
Git /gitLab/ Github
1. Configure the Github user name and email address globally
Git config --global user.name git config --global user.name
// Configure the user nameGit config --global user.email
// Configure the mailboxgit config --list
// View the configuration list
2. Create a local repository
git init
// Start a local creationgit clone url
// From remote clone repository to local
3. Add the file and submit it
git add *
// Add workspace changes to the cacheGit commit -m
// Commit cache changes to version areagit status
// Check the status
4. Git maintains three regions locally
- The workspace
(working dir)
- Cache/staging area
(Index/Stage)
- Version of the area
(HEAD)
5. Push: Commit updates from the local repository to the remote repository
git remote add origin url
// Connect local warehouse with remote warehousegit push origin master
// Push updates from local repositories to remote repositories
6. Pull pull
Git pull origin master // Pull updates from remote repository to local repository note: merge automatically
7. Git manages real projects
Gitignore // Run the vim command to create a file I // Enter editing mode. Press ESC // to enter viewing mode ZZ // Save and exit the specified file or folder /.idea## Ignore this file in the current directory (folder)
node_modules Ignore this file in any directory (folder)Git add * git commit -m"xxx"3. Create github New Repository VueSource 4. Pushed to the remote warehouse git remote add origin https://github.com/zxfjd3g/VueSource.git git push origin masterCopy the code
8. Multi-person collaboration in the company
new organization
- Find a friend account and add it
- Example Change the access permission of an organization member to write
- Send the organization name/address to your partner
- After logging in, the partner accesses the organization’s address and agrees to the collaboration request
- Clone /pull/push repositories created in an organization
9. Open source project collaboration
* User A: project XXX * User B: the next level or friend of user A, without permission to directly push the code to warehouse XXX * B: Access the home page of XXX, fork A branch to account B * B:clone=> Modify the code ==>commit==> Push * B: submit A pull request to A * A: receive A message, check /diff/merge * AB: send messagesCopy the code
Git /gitLab/ Github other operations
1. The fetch pull
Git fetch origin master: TMP git diff TMP git merge TMP: Merge the TMP branch to the current branchCopy the code
2. Other commands:
git logGit reset --hard HEAD^ : /reflog git checkout commit_id <file.name> : Go back to the previous versionCopy the code
3. Using git in webstorm
Use git commands to initialize local repositories and associate them with remote repositories. Direct version control through the Version Control windowCopy the code
4. Use TortoiseGit
Use git SSH to create a public Key for the TortoiseGit project. And configure github to generate a private key and save it as a PPK file through the remote repository SSH addresscloneAnd specify the local PPK fileCopy the code
Git general commandswww.07net01.com/2015/04/827…
The end