Original link: my blog garden article

Git installation and configuration and common commands

  1. Download the installation package from the official website and install it manually.
  2. Open theGit BashCommand line tool to execute commandsssh-keygen -t rsa -C Email-AddresssGenerate a key pair.
  3. Log in toGitLab, click on your user profile picture in the upper right corner, and clickEdit Profile settings, click on theSSH Keys, click on theAdd SSH KeyAnd fill inTitleBar, copy user directory under.ssh/id_rsa.pubThe contents of the documentKey, click on theAdd Key.
  4. Click on the top rightNew project, and then clickCreate projectTo create a new warehouse, clickActivity, click on theSSHAfter the copySSHThe address in the sidebar.
  5. Open theGit BashCommand line tool, switch to an appropriate directory, use the commandgit clone‘Just copied URL’ clone created by repository.
  6. Go to the directory CD repository name and run the command
    • git config --global user.email your-email, set up your email
    • git config --global user.name your-name, set your name.
  7. Execute command:
    • echo "# Description" > README.md, add a file
    • git statusStatus. Untraced files were found
    • git add ., all files in the current directory are added to the staging area
    • git diffCompare the difference between the current workspace and the staging area
    • git statusTo view the current status, the file is not submitted
    • Git commit -mTo submit the contents of the staging area to the local repository
    • git push -u origin masterTo push a commit from the local repository to the remote repository
    • git logTo view the commit log
  8. Simulate remote update login toGitLab, click on theFilesTAB, clickREADME.mdFile, clickEditButton to modify the file content, add a new line: “* 9 remote modify file record”, you can clickPreviewPreview, you can also fill in comments and save directly.
  9. Execute command:
    • git pull, pull the update commit of the remote repository and do the automatic merge, which may cause conflicts
    • git fetchTo get the update submission of the remote repository, do not do automatic merge, need to manually merge
    • git merge origin/masterTo manually merge the remote update commit locallymasterbranch
  10. Configuring command Alias
    • git config --global alias.co checkoutTo configure the check out command alias
    • git config --global alias.br branchTo configure the alias of the branch command
    • git config --global alias.ci commitTo configure the alias of the submit command
    • git config --global alias.st statusTo configure the current status command alias
    • git config --global alias.df diffTo configure the alias of the comparison command
    • git config --global alias.pl pullTo configure the alias of the pull command
    • git config --global alias.pu pushTo configure the alias of the push command
    • git config --listTo view the configuration list
    • git config --global credential.helper storeWhen configured to remember passwords to USE HTTPS
  11. Other commands
    • git clean -fTo clear untraced files
    • git checkout .To clear workspace changes
    • git checkout some-branch-name file-name.js, checks out the specified file for the specified branch
    • git checkout {{some-commit-hash}} file-name.js, checks out the specified file for the specified commit
  12. 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 points
  • git branch b1Create a branch called b1 from the current branch
  • git checkout b1, switch to branch B1
  • git checkout -b b1Is equivalent to the combination of the preceding two commands
  • git checkout master, switch to themasterThe main branch
  • git merge b1, merge the b1 branch code intomasteron
  • git branch -d b1To delete branch B1, this operation cannot be performed on the deleted branch
  • git diff branch-1 branch-2Compare the differences between two branches
  • git diff --name-only branch-1 branch-2, displays only the names of files with two different branches
  • git diff some-branch some-filename.jsCompares the current branch to another branch specified file

Git remote branch management

Create, merge, and delete remote branches

  • git push origin devTo push the local branch dev to the Origin remote repository
  • git pull origin dev, pull the remote repository Origin dev branch and merge it automatically
  • git push orgin :devDelete the remote branch on the origin remote repository
  • git push origin --delete devThis command is available after version 1.7.
  • git remote prune originTo clean up the local trace branch, add the -dry-run parameter
  • git 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 t1Create a label named T1 from the current branch
  • Git tag -m 'comment' t1Create a label with comments
  • git tag -d t1Delete the label named T1

The Git Log Log

  • git logTo view historical logs
  • git log --graphTo display the merge trajectory with text-based graphics
  • git log --pretty=onelineOne line displays brief log information
  • git log --pretty=format:"%h - %an, %ar : %s"To view logs in the specified format. For the format parameter, go to the official manual
  • git 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 commits
  • git log -p -2 --stat-p displays file differences, -2 displays the last two submits, and -stat displays statistics about the number of modified rows
  • git log -p filenameTo view detailed changes to the file, including actual changes to the file
  • The git log - L, 1, 1: some - file. TXTTo view changes at the specified location in the file
  • git 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 above
  • git show some-branch:some-file.jsTo 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:

  • OwnerThe project owner, who has all operation rights
  • MasterThe manager of the project can perform other operations except to change or delete the project meta information
  • DeveloperThe developer of the project, who does some development work, has no access to the protected content
  • ReporterThe reporter for the project, who only has read permission for the project, can create code snippets
  • GuestVisitors 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)
  • masterMain branch, stable code, ready for production
  • developDevelopment branch, for development services
Feature branch
  • fromdevelopBranch creation, used for feature development, to be merged back after completiondevelopBranch.
  • git checkout -b newfeature develop, fromdevelopBranch to createnewfeatureFeature branch
  • git checkout developAfter the development is complete, it needs to merge backdevelopBranch, let’s switch todevelopbranch
  • git merge --no-ff newfeatureAnd merge backdevelopBranches, you have to add--no-ffparameter
  • git branch -d newfeatureDelete the feature branch
  • git push origin develop, put the mergeddevelopBranch 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 developCreate a publishing branch
  • git checkout master, switch to themasterBranch, ready to merge
  • Git merge - no - ff in release 1.2,Release 1.2Branch merge intomasterbranch
  • Git tag 1.2.From the masterThe branch is labeled
  • git checkou develop, switch to thedevelopBranch, ready to merge
  • Git merge - no - ff in release 1.2,Release 1.2Branch merge intodevelopbranch
  • Git branch - d release 1.2Delete 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, frommasterBranch Creates a Bug fix branch
  • git checkout master, switch to themasterBranch, ready to merge
  • Git merge - no - ff hotfix - 1.2.1And incorporated into themasterbranch
  • Git tag 1.2.1Create a label for the master branch
  • git checkout develop, switch to the Develop branch and prepare to merge
  • Git merge - no - ff hotfix - 1.2.1And incorporated into thedevelopbranch
  • Git branch - d hotfix - 1.2.1, delete theHotfix - 1.2.1branch

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 name
  • Git config --global user.email// Configure the mailbox
  • git config --list// View the configuration list
2. Create a local repository
  • git init// Start a local creation
  • git clone url// From remote clone repository to local
3. Add the file and submit it
  • git add *// Add workspace changes to the cache
  • Git commit -m// Commit cache changes to version area
  • git 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 warehouse
  • git 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