PS: Friends interested in front-end technology, you can pay attention to my “dedicated to the front-end technology blog” oh! If it is helpful to you, welcome to send a ⭐️, we will update the content often, please look forward to! ❤ ️ ❤ ️
This article summarizes the basic operation methods of git version management and team collaboration in the actual work process.
Git is introduced
What is Git? Git is a distributed version control software.
Pay attention now, and keep these concepts in mind if you want to make Git work better. Git has three states, and your file may be in one of them: Committed, Modified, or staged.
- Modified indicates that the file has been modified but not saved to the database.
- Staging means that the current version of a modified file is marked for inclusion in the next committed snapshot.
- Committed indicates that the data is securely stored in the local database.
Three major regions:
- The workspace
- The staging area
- repository
The basic Git workflow is as follows:
- Modify files in the workspace.
- Optionally staging the changes you want to commit next time will only add the changed parts to the staging area.
- Commit the update, find the files in the staging area, and store the snapshot permanently to a Git directory.
If the Git directory holds a particular version of the file, it is committed. If the file has been modified and placed in the staging area, it is in the staging state. If changes have been made since the last check out but have not been placed in the staging area, the state is modified.
After installing Git, configure Git for the first time:
$ git config --global user.name "John Doe"
$ git config --global user.email [email protected]
Copy the code
Check your configuration: If you want to check your configuration, use git config –list to list all the configurations git can find at the time.
In actual combat
Upload code from home
git remote add origin [github@git]
git push -u origin
Copy the code
2. Get the code for the first time on a new company computer
git clone [github@git]
git checkout [branch]
Copy the code
3. Develop in the company
Git add. Git commit -m "xx" git push origin devCopy the code
4. After going home
git checkout dev
git pull origin dev
git add .
git commit -m "xx"
git push origin dev
Copy the code
5, the completion of development, online
Git merge dev branch git merge dev branch git push origin master Git merge master git merge dev git merge master git push Origin dev # pushes the dev branch to remoteCopy the code
Git rebase git rebase git rebase
1. Help you consolidate your multiple commit records into one commit record
Git rebase -i HEAD=3 git rebase -i HEAD=3 git rebase -i HEAD=3 git rebase -i HEAD=3 git rebase -i HEAD=3 git rebase -i HEAD=3Copy the code
[Note] If a record is already committed to the remote repository, it is best not to rebase it as well. Try to merge the records that have not been committed to the remote repository. Try not to use base changes after you have pushed to a remote library.
The master branch is being merged into the dev branch, and the dev branch is being merged into the master branch.
If you want to simplify recording, insert C3 between C2 and C4 (merge into one record, reduce bifurcating log records)
git checkout dev
git rebase master
git checkout master
git merge dev
Copy the code
3. After writing code in the company, I forgot to push it to the remote, and then I developed other functions and pushed it to the remote after returning to the company. In this case, I need to pull the code and merge it, which will cause log record fork (if I use Git pull directly to pull the code). Git fetch origin dev+git rebase origin/dev
Git rebase may cause conflicts. Resolve the conflicts before proceeding
conclusion
- Adding a Remote Connection
Git remote add origin Git clone 4. Git pull origin dev = git fetch origin dev+git merge origin/dev Git rebase (keep code submission simple) Git log –graph –pretty=format: %h %s
Collaborate to develop gitFlow workflow
- Master branch V1 (master branch only contains the company’s online running code)
- Create branch dev and split out all sub-functional branches (A, B, C…)
- After the function development of sub-branch A is completed, it shall be merged into dev branch (using pull Request) after code review. The same applies to other sub-function branches after development. After the sub-function is developed, the corresponding branch can be deleted.
- Create a release branch for testing, test the code on dev branch, and fix the bugs found. After the repair is completed, submit it back to dev branch, and only after there are no bugs can it be merged into master online
- If an emergent bug occurs online while each sub-function is under development, an additional bug branch should be created and merged back to master after fixing the bug
Open source contribution
Contributing code to open source software:
- Fork source code (copy source code to your own repository)
- Modify code in your own repository
- Submit a bug fix pull Request to the source code author
Avoid close login
(1) Reflected in URL
The original address: https://github.com/iloveyou11/my-project.git change of address: https://[username]:[password]@github.com/iloveyou11/my-project.git git remote add origin https://username:[email protected]/iloveyou11/my-project.git git push origin masterCopy the code
(2) SSH implementation
1. Generate public and private keys (id_rsa.pub public key and ID_RSA private key) ssh-keygen 2. Copy the public key content and set it to github 3. In the local git directory, configure the SSH address git remote add origin [email protected]:iloveyou11/my-project.git 4. Git push origin master is used in the futureCopy the code
Common commands
1. Obtain the Git repository
There are two common ways to get a Git project repository:
- Turn local directories that have not yet been versioned into Git repositories;
$ git init
$ git add *.c
$ git add LICENSE
$ git commit -m 'initial project version'
Copy the code
- Clone an existing Git repository from another server.
$ git clone https://github.com/libgit2/libgit2
$ git clone https://github.com/libgit2/libgit2 mylibgit
Copy the code
Common operations:
Initializes the git clone git init | warehouse [url] | copy from a remote warehouse git status | show the state of the working directory git add [file1] / file2 file3 | Tracking new file, the modified files to add to the staging area git commit -m (MSG) git rm [file] | | submitted file will have been tracking the file from the working directory, remove the staging area, pay attention to are the track git mv | renaming files will have been tracking, Or move files from one directory to another directory git diff - cached | the staging area and the difference between the warehouse git diff [file] | view the specified file differences git diff [commitId_1] [commitId_2] | Two specified version of the difference between the git log (graph) | view history git reflog | see history record, Including you don't have update submit git reset - hard HEAD ~ | local warehouses, temporary, working directory on the back to a version (all changes will be lost) git reset - mixed HEAD ~ | local warehouse, return to the previous version registers, Working directory to save the modified git reset - soft HEAD ~ | will local warehouses, a version of the staging area, working directory to save the modified git reset HEAD ~ 2 [path] | file path, the default is mixed, only to the staging area, Path path before back to the two versions of files git checkout [file] | undo the modified files in the workspace git commit - amend | covering the last submission.Copy the code
Remote warehouse related commands:
Git remote show [remote-name] git remote show [remote-name] Git remote rename [old name] [new name] Rename a remote repository git remote rm [remote-name] Delete a remote repository git remote add <shortname> <url> Add a remote repository git fetch [remote-name] Git pull [remote-name] [branch-name] The magic of pulling data from a remote repository to a local repository and merging git pull on its own is often confusing, so it is usually better to use fetch and merge explicitly alone. Git push [remote-name] [branch-name] git push [remote-name] [branch-nameCopy the code
Branch related commands:
Git branch -v displays the last commit of each branch. Git branch -vv displays details about each branch. Git branch <branchname> Git checkout <branchname> git checkout -b <branchname> Git mergr <branchname> Merge branches. If you want to merge branches to the master branch, switch to the Master branch first and then merge branches. Git merge --no-ff -m "merge with no-ff" <branchname> Git branch --merged Displays branches merged to the current branch. Git branch --no-merged Displays branches that are not merged into the current branch. Git branch -d <branchname> You can forcibly delete a branch that has not been merged git push origin --delete serverfix Deletes a remote branch Git checkout -b branch-name specifies the name of the branch that you want to create. Git checkout -b branchname specifies the name of the branch that you want to create. The local and remote branches should have the same nameCopy the code