1. The GIT’s brief introduction:

GIT is a free and open source distributed version control system. Centralized management: SVN Distributed management: Git In centralized mode, developers can only submit the code to the server, in distributed mode, developers can submit the code database on the remote server, in distributed mode, each developer machine has a code repositoryCopy the code

Distributed:



Centralized:



2. Working principle of Git:

Repository :.git directory for storing version information of records

  • Staga in version library:
  • Master branch: The first branch that Git automatically creates
  • HEAD pointer in repository: used to point to the current branch

.git files: HEAD: a commit to the current branch Description: a description of the project config: configuration of the project info/: contains a exclude file that specifies the objects to be ignored in the project /: Git object library (commit/tree/blob/tag) refs/: identifies each branch to which commit hooks/: default hook scriptsCopy the code

Working Directory: the contents of a repository folder other than the. Git Directory

Git add and Git commit naming

  • Git add: Add file changes to the hold area


  • Git commit: Commit all the contents of the hold area to the branch pointed to by the current HEAD pointer


Git使用实战:

Step 1: We’ve set up Github as a remote repository, the company’s equivalent of a server, and created a project called Test on Github





Step 2: Download the project locally using Git’s Clone command



git clone "Project URL"Copy the code

Step 3: Modify the file and operate the project locally

1. View the git command

Git instruction help manual git --helpCopy the code

Git repository Initialize git repository Initialize git repository So there is no need to init again, the main purpose is to generate.git directory.

Git init --bare Initialize repository (shared repository, repository on remote serverCopy the code

3. Set the configuration information

1. Configure the user name for a single project: git config user.name"Username"Git config "user.email""Email"Git config (for communication between multiple developers-lGit config: Displays configuration information-eGit config --global user.name manager git config --global user.email [email protected]Copy the code

4. Create a main.c file and view the latest changes in the file

1. Check for the most recent change to a file: git diff filename 2. See git diff for the latest changes to all files in your current pathCopy the code



5. Check the file status

1. Check the status of a file: git status File name 2. Check the status of all files in the current path: git statusCopy the code

If the file is red, it is not yet added to the hold area of the.git directory



6. Save the files in the workspace to the deferred area

2. Save all files in the current path to the hold area: git add.Copy the code

After the previous step, check the status of git status. If the modified file turns green, it indicates that the modified file has been added to the hold area.



7. Commit the deferred area file to the current branch

1. Commit a file to the branch: git commit -m Git commit -mCopy the code



8. Delete files

git rm "File name"Note: Deleting a file is only done in the hold area, and does not Commit the deletion to the master branchCopy the code

9. View file modification logs

1. Check the change log of a file: gitlogFile name 2. View the change logs of all files in the current path: gitlogView simple log information in one line: gitlog-- pretty=oneline 3log-n (N is an integer)Copy the code

The results are as follows:



10. Commit the local warehouse to the remote warehouse

Note: If the project is created on Github through your own account, you can participate in the submission. In order for other developers to participate in their own projects, you must add a public key SSH key to your own account and submit it to a remote repository git pushCopy the code



Submit successfully, open the test project on Github, and the main.c file does exist



11. View branch reference records (can view all version numbers)

git reflogCopy the code



Note: 1. In the first red box, the head pointer points to the local master branch 2 before pushing to the remote repository. In the second red box, after the push to the remote repository, the head pointer points to OriginCopy the code

12. The version is rolled back

1. Go back to previous version: git reset --hard HEAD^ 2 Git reset -- Hard HEAD^^ 3 Git reset --hard HEAD~N (N is an integer) 4. Git reset --hard version numberCopy the code

Note: 1. When you use the command line to rollback to a version, you are only reverting the local repository to a version, the remote repository is not reverted to the specified version, and the head pointer points to the local repository master 2. To push the remote repository back to a version, you need to do the following git push --force operation (this operation needs to be done carefully and may be hit)Copy the code



Local warehouse files:



Remote repository files on Github:



Git push -f now aligns the local files with the remote repository files



13. Version rollback is another way

Git revert HEAD~1 git revert HEAD~1 git revert HEAD~1 git revert HEAD~1Copy the code

Note that the git revert command means to undo a commit. It makes a new commit, and even though the code reverts, the version is still forward, so when you revert it and push it to a remote repository, and everyone pulls, their code automatically reverts as well. So the commit ID is the last commit of the version that you want to roll back to. Using revert HEAD undoes the most recent commit. If your most recent commit was created using the REVERT command, you undo the most recent undo. Use REVERT HEAD~1 to undo the most recent two submissions. This number starts at 0. If you have previously undone a COMMi ID, it will count. If you use revert to undo something other than the most recent commit, you can expect a code conflict that requires you to merge the code. Simply remove the current code and keep the previous version. 1. Since we only want to go back to one commit, we need to remove the current most recent code, which is code 2 for the HEAD tag. Once the conflict is resolved, add the Commit push again, and the version on the remote server is the same as the local versionCopy the code

How do I get people to participate in projects I create on Github

1. Generate an SSH Key









2. Add an SSH key





3. Check whether the configuration is successful

Ssh-vt [email protected] Tip: Hi ****! You've successfully authenticated, but GitHub does not provide shell access. Prove successfulCopy the code

Note: 1. When generating the SSH key, if a message indicating that the key already exists! 2. Run ls -al ~/. SSH to view the public and private key directories 3. You can view the public key in the cat directory, for example, cat /Users/yangtao/.ssh/id_rsa.pubCopy the code

4. Other developers can clone the github project locally using SSH

git clone [email protected]:codeXiaoQiang/test.gitCopy the code



5. Download the latest information of the remote warehouse to the local warehouse

git pullCopy the code

6. Push the local warehouse information to the remote warehouse

git pushCopy the code

Note:

If the remote repository has the latest code submitted by someone else at the time of submission, you must pull first and then commitCopy the code

7. Conflict resolution:

< < < < < < < < < < HEAD your local new code = = = = = = = = = = = = = = = server and conflict of code you > > > > > > > > > > > b7d36a90cb826abc91b6b6b8531706ba4d27a06eCopy the code

Branch management

1. Create a Git branch"Branch name"2. Branch git Checkout"Branch name"3. Create and switch git checkout -b"Branch name"4. Merge a branch with git Merge"Branch name"5. Delete git branch-d "Branch name"Note: 1. Do not delete yourself from the current branch. 2. After the branch has modified the code, go back to the branch and merge code 3. Git will select "Fast Forward" by default when merging branches. In this mode, branch information will be discarded after deleting branches. Git merge -- no-ff-m"Merge information" "Branch name"After the branch merge, you can view the merge information :gitlog --graph --pretty=oneline --abbrev-commitCopy the code



Application Scenario 1-Feature branch:

When you finish developing a new project on a feature branch and cut back to the main branch for merging, the product manager suddenly says this feature has been cancelled and you execute Git Branch-d "feature"How do I delete this branch? Git branch -d"feature"Forced to deleteCopy the code



Application Scenario 2- Bug branch

If you are working on a new feature on dev and you need to fix a bug, the first thing you want to do is create a new bug on the master branch to fix the bug, but the code changed on dev cannot be submitted to the master branch. Step 1: Execute the git Stash command on the dev branch. At this point, check with git status that your workspace is clean and the code you developed is hidden Step 2: Determine which branch you want to create a bug on if you want to create a bug on the master branch step 3: Change the code on the bug branch and fix the bug step 4: Switch to the Master branch and merge the code Step 5: Delete the bug branch Stash stash stash stash stash stash stash stash stash stash stash stash stash stash stash stash After step 7, switch the master branch to merge the codeCopy the code

Push the local master branch to the remote library

git pushCopy the code

Conclusion:

1. Add the file to the cache git add.2. Add the file from the cache to the master branch git commit -m"Description"3. Push to remote server git pushCopy the code