6git -------------------- git multi person development collaboration tool ------------------- full stack development 46

Posted by chrislive on Sun, 23 Jan 2022 02:11:28 +0100

1, What is Git

introduce

  • When developing functional projects, what should we do when we want to switch versions and realize multi person development?
  • We have software similar to QQ and office to realize multi person sharing, as well as version control: similar to graduation thesis, copywriting, video editing, etc., we need to repeatedly modify and retain the original historical data. But we have a more convenient tool for developers, git
  • Why version control: keep all previous versions for rollback and modification.

definition

  • Git is a distributed version control software.

Install Git

  • Git address: https://git-scm.com/book/zh/v2/%E8%B5%B7%E6%AD%A5-%E5%AE%89%E8%A3%85-Git

File status under git management

Basic command

  • git status: view the status of files in the directory. Files have three statuses: workspace, staging area and version library.
    • In the workspace: in addition to configuration files and files that do not need to be managed, they will be automatically managed. Managed files will be displayed in red in git status
    • In the staging area: the mediation point will display green in git status
    • In the version Library: the file has been submitted at this time, so the file does not exist in git status.
      View the file status in the directory. There are three statuses: working area (red or white), temporary storage area (green) and version Library (none)
  • git log: View version submission status
    • Git log -- one line view
  • History: view history command

common problem

  • The folder just initialized needs to be configured with personal information, otherwise the version cannot be submitted
  • The configuration information of global is set here. Generally, it's OK to set the global system. The project is local global system
# No configured prompt * * * Please tell me who you are
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
  • Solution to Chinese garbled code in git
git config --global i18n.commitencoding utf-8
git config --global i18n.logoutputencoding utf-8
export LESSCHARSET=utf-8

git configuration file

  • git configuration files are hidden files, which are generally not modified, and the modification is only global and project
  • Because the configuration file is a hidden file, it will not be managed by git detection
    • Project configuration file path: project / git/config
    • Global configuration file path: ~ / gitconfig
    • System configuration file path: / etc / gitconfig

(1) Workspace

1. Specify the folder to manage

  • Enter the folder to be managed, right-click Git Bash Here, and enter GIT
  • git init initializes the folder and generates git configuration files, which are hidden folders and become master mainlines

2. Automatically detect new documents and changed documents

Create new / changed files
-Create or vim edit a file by using methods such as touch a.py to view the file status git status
-It has been managed by git detection

How to cancel git management of a file

  • Because some files, such as configuration files, may be exposed if uploaded to the public platform, we need to cancel the automatic detection of some files
    #Create a new file gitignore
    touch .gitignore
    # Write the file name that needs to be unmanaged in it, and support regularization, inversion and folder
    vim .gitignore
    # For example, * Txt cancel all txt files
    # For example! demo.py except demo Py files are unmanaged
    # For example, files / the files in this folder are unmanaged
    

3. Put it in the temporary storage area

  • git add file name
  • git add . Submit all documents
  • When the file turns green, it indicates that it has been put into the temporary storage area

(2) Staging area

1. Modify the file and return to the workspace

2. Submit to version area

  • git commit -m 'description information'

(3) Version Library

1. Switch version

Rollback to previous / later versions

  • git reset – hard version number ## combined with git log to view the version number

2. Modify the submitted version

First in modification page

  • git rebase -i the version number of the last to be modified, which can be multiple
  • git rebase -i HEAD~2 modify recent records
# Modified Commands:
	# p. Pick < commit > = Default
	# r. Reword < commit > = edit description information
	# e, edit <commit> = use commit, but stop for amending
	# s. Square < commit > = merge version
	# f, fixup <commit> = like "squash", but discard this commit's log message
	# x, exec <command> = run command (the rest of the line) using shell
	# d, drop <commit> = remove commit
	# l, label <label> = label current HEAD with a name
	# t, reset <label> = reset HEAD to a label
	# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
	# .       create a merge commit using the original merge commit's
	# .       message (or the oneline, if no original merge commit was
	# .       specified). Use -c <commit> to reword the commit message.

Modify description information

  • Exit pick - > R - > Modify description information wq
  • Or directly enter git commit --amend into the latest one, and you can only modify the description

Merge version

  • Change the last pick - > to s - > modify the merged description wq and exit

Abandon modification of xxx|REBASE-i

  • git rebase --abort exit modification

3. Delete the submitted documents

git rm file name
git commit -m 'Description information'

2, Branches in Git

  • Branches can provide users with multiple environments, which means that you can separate your work from the development main line so as not to affect the development main line
  • The default is the master mainline. It is impossible to develop code directly in the master according to the actual production situation

View all branches

  • git branch -a

Create branch

  • git branch branch name

Switch branch

  • git checkout branch name
  • git checkout -b bbz create branch and switch

Branch merge (possible conflict)

  • git merge branch to merge

Delete branch

  • git branch -d branch name

3, GitHub, code cloud upload drop-down code

  • GitHub and code cloud function: upload the local code to the center, so that the code can be modified at home or in the company, and multiple people can modify it together

(1) Realize single person work steps

Register account and create warehouse

  • You need to register the github / code cloud account, create a remote warehouse, and then execute the following command to upload the code to the github / code cloud.

Set remote submission mode

  • git remote add operation name remote warehouse address
    • eg: git remote add origin https://gitee.com/wyt33/weather-forecast.git
  • Delete remote submission method
    • git remote rm origin

Push up code

  • git push -u origin branch name
  • If the new project has its own files, the upload may fail. You can directly force the push, which will directly overwrite the project git push -f origin master

Drop down code

  • git pull origin branch name
  • Pull down all
    • git clone remote database creation address
    • Download the code on the company's new computer for the first time

Solve the problem of inputting user name and password for push code every time

  • Create ssh public key
    # Input in git bash terminal
    ssh-keygen -t rsa -C "xxxxx@xxxxx.com" # Replace the mailbox with the code cloud registration mailbox.
    # Press enter three times to generate sshkey
    # View the public key and add it to the code cloud (Gitee.com)
    cat ~/.ssh/id_rsa.pub # Copy the content from SSH RSA to the mailbox
    # Add the user sshkey and add the ssh public key through personal > > settings in github or code cloud
    
  • Verify whether the binding is successful
    ssh -T git@git.oschina.net # Return to Welcome to Git@OSC, yourname! Indicates that the addition was successful.
    
  • use
    # In the future, https links will be replaced with ssh protocol authentication
    git remote add origin git@github.com:xxx/xxx.git
    # If the previous remote submission setting is http link, it needs to be changed to ssh protocol
    git remote rm origin # Delete the remote submission method, and then add a new submission method
    git remote add origin git@github.com:xxx/xxx.git
    

(2) Realize gitflow ---- multi person collaborative development workflow

First step

  • Project initialization, local submission

Step 2

  • Created an organization through code cloud
  • A warehouse was created under this organization
  • Invite development members in this repository

Step 3

  • Create branch dev
  • Create development member branch (not necessarily)
  • If developer A submits A pull request, the next step is code review
  • Merge the developer branch into the dev branch

Step 4

  • Pull the latest code locally and pull down the code on the dev branch
  • Branch merge
  • Switch to the master branch

Step 5

  • Function online
  • Submit to code cloud

4, Scene implementation

rename

  • git mv original file name modified file name
  • If you directly modify the git managed files without modifying them, an error will occur

Download the code on the company's new computer for the first time

  • There is no need to create a folder in the managed files. Right click git bash here, git clone remote library creation address, and you can drop down all the code folders
    git clone Remote creation address
    

bug fix

  • Create a bug branch line, modify the source file, upload it, and then merge the modified file with the mainline to upload the version successfully
    	# Create and switch to branch
    git branch -b bug
    	# Modify the source file in the branch and commit after modification
    	# Switch mainline
    git checkout master
    	# Merge branch
    git merge bug
    	# Delete branch
    git branch -d bug
    

Expand new functions

  • Newly added documents in the development branch line shall be merged and submitted to the version library by the main line after submission
  • 	# Create and switch to branch
    git branch -b dev
    	# Create a new file in the branch and commit after modification
    	# Switch mainline
    git checkout master
    	# Merge branch
    git merge dev
    

Forget to push up

  • If you forget to push it up during the day and change a computer to continue working at night, it is impossible to change all the codes during the day, then continue to modify and push the files up, and change to the original computer the next day, then the code of the machine during the day and the code written at night will conflict
  • Just modify the file directly, merge the two sections of code, delete the prompt line, and then submit the file

Topics: git github