1. Git usage summary

A brief introduction to Git

  • 1. Git is an open source distributed version control system designed to efficiently handle projects of any size.
  • You can use git -v to check whether the installation is successful. If the installation is successful, the corresponding git version number will be displayed.
    • General installation is very brainless direct next step can be the next step, whether Windows or MAC need to write and modify during the installation process

Configure the user name mailbox

1. Download Git installation directly from the official website and search for Git bash in the Start menu2Set.2.1 git config --global user.name "your name"
	2.2 git config --global user.email "[email protected]"
Copy the code

The use of git

Create a version library

A repository is a repository, or directory, in which all files are managed by Git. Git can keep track of changes, deletions, and recoveries at any time.

1. Create an empty directory in an appropriate location (it does not have to be an empty directory, mainly if you are not familiar with the learning stage or create an independent empty directory to study), and use Git Init will turn the directory into a Git repository. A.git file will be created in this directory. Do not touch this file or your Git repository will be destroyed.

The master branch is created by default when a project or remote repository is created, and the master branch is usually saved from the live code.

Method 1: Create the dev branch directly on the remote repository and use it locally

Git fetch git checkout-bDev origin/dev (create dev branch locally from remote origin repository)Copy the code

Method 2: Create a branch in the local repository

git checkout -bDev git push-uOrigin dev (push local branch to remote repository and create dev branch in remote repository)Copy the code
Git add. (Add changes to staging, all files,git add xxx. TXT // add a specific file.-m "Submit instructions"(submitted to the HEAD (the current branch) git remote add origin [email protected]: funny0628 / XXX/ss. The git (associated remote warehouse) git push-uOrigin Master (only used for the first price increase, can be omitted afterwards-u// If you have a commit error, you need to modify it (press I to enter the editing mode, press Esc to exit the editing mode, save the editing mode, exit the editing mode, and check the result) git commit --amend(Modify git commit description) Git branch (View local branches) Git branch-aGit branch-dDev git push origin --deleteDev (delete remote branch) git checkout-bDev (create and switch branches)Copy the code

The problem

In the project, the team leader will sometimes add readme. md when creating the warehouse, which will cause an error during push. The solution is to merge the remote warehouse to the local, and then there is no problem with push. The operation command is as follows:

git push --rebase origin master
Copy the code

2. Git generates an SSH public key on the server

//1. If there is no SSH file SSH-keygen -t rsa -C "your [email protected]"After generating the SSH file, press Enter (three times in total)2. Prerequisites The Linux syntax is supported and the SSH folder is availablecd~/.ssh (go to SSH folder)ls(see the file folder) < - typically display is this -- > Mode LastWriteTime Length Name -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --a----        2020/3/25     17:48           1823 id_rsa
-a----        2020/3/25     17:48           399 id_rsa.pub
-a----        2020/7/13     15:24           2188 known_hosts

cat~/.ssh/id_rsa.pub (If you query the public key, the following information is displayed) SSH-rsa AAAAAB3NzaC1yc2EAAAADAQABAAABAQDPrfWsvBtww23V0MQRYf9XDJJ4Y6jtUtQTL7JZLuuEmeP+tSi+c4yE2E/22ahy+Y57mpU55Bd16i3+MGkq2vKqzkl8dk5SOaoO5YOrBg6NS5WOT4Lod37DOIcoe0nImBhX5v4IceBrAgPi+xU5LXyOOhw4Ph7my9eQuLGVxzH3xzLub1k7S VsI4ySMxvQi9MjiJ9LtEzUIsAMbo7AgBB5Hj5Xun+1sTn3xMxIjDg/ttpfup2iAZKmX/OP9+eyjPYGsL1Q4q7EoVhoQ2AOY0Zq79rhDjEKHBPK4l6apEN [email protected]

Copy the code

3.gitOther commands

Git status (View the current status of the repository, changes to files, and newly added files) gitdiffYou can see what changes have been made, and the newly added files are not displayed-pretty=oneline (gracefully displayed on oneline of the submission record)Git checkout -- 
      
        -- git checkout -- 
       
         -- git checkout -- 
        
       
      Version back1.commit -> add -> Workspace git reset --softGit checkout -- <file> (workspace -> Previous version)2Git reset --hardHEAD^ // Note that HEAD refers to the current version,^ is the previous one, several ^, too troublesome simplification is HEAD~'X'Git reset --hardGit reset --hard HEAD~'X'Back up'x'// If from version3Back to version2And then I want to go back to version3The previous command window does not close can still find the version3Git reset --hard 'Version 3 version number'// If the command window is closed, the closed version can not be found git reflog (query the operation record) gitdiffHEAD -- <file> (view"Work area"with"Version library"Git git git git git git git git git git git git1Git add. / git commit-m "Submit information for deleting files".2If the file is missing and needs to be deleted from the repository, you can use the following command to delete it, and then commit itrm <file>
git commit -m "Submit information"// Delete filesrmGit checkout <file> <file> <file> (Undo workspace changes, delete, essentially replace the contents of the repository with the contents of the workspace, both deletion and modification can be restored.) Note that if a file that was never added to the repository is deleted (this can be restored by Ctrl+Z in Explorer)Copy the code

Git actual use record

Generally tend to be more than one branch in the project, the master is a main branch for online, usually don't directly in the above development, just as the test successful release branch, according to the actual situation back when collaboration to create one or more of the development of the main branch of development colleagues in creating their different development branch, each branch after writing the code in your own needs Merge into the development main branch, 1. Git add. Git commit -m "git pull origin dev // Git add. Git commit -m Git push origin msater "build" git push origin msater Git checkout // Git checkout/git checkout/git checkout Git merge dev // merge the main branch into your own branch, and then continue editing your own project files. Git fetch origin --set-upstream origin fanny_4.0 git fetch origin --set-upstream origin Check-out -b local branch name origin/remote branch nameCopy the code

Problem with file staging

In practice, when we are halfway coding in one branch, we need to cut out to another branch to deal with urgent needs. However, the current branch code is not in a complete node, so it is not convenient to commit. In this case, we can use 1.git Stash in the current branch Git stash pop // To pull back the code from the current workspace, it will revert back to the code from the last commit and branch out to handle our own logic. When we are done, we will go back to the current branch using 2. ** By default, just use command 2 to achieve the desired goal, but sometimes it is not successful, you need to use command 1 to assist **Copy the code

See which branch is checked out from

After the project was launched, some bugs and new requirements appeared. According to the seriousness and necessity of the problem, some tasks needed to be launched urgently, and some problems needed to be solved temporarily in the repair branch. When the project was launched at a specific time, the situation of the branch would be more complicated After the time is long will need to confirm a certain branch from which a branch detection, this command is not very common, record here, for future reference and learn git reflog - date = local - all | grep in the name of the branch to queryCopy the code

Play label (tag)

In the actual practice, the project was divided into several iterations, and there would be several development branches of different iterations, but only one local development branch was created. In order to distinguish different iterations and requirement records, the tag method was simplified to record. Git tag -a tag name -m "git show tag name" git show tag nameCopy the code

The end of the

Of course, our records are not completely comprehensive, but we can solve most of the problems encountered in the normal operation process, and we will gradually accumulate them in the future

Note: Github Learning Reference file

Invite colleagues to join the team: www.pianshen.com/article/220…

Making the official study address: docs.github.com/cn/github/s…