theme: hydrogen
Copy the code
This is the 31st day of my participation in the August Text Challenge.More challenges in August
preface
Hello everyone, today we are going to talk about git and Github use, the standard mainly from the version control system, and then is the basic operation of Git, basic command, and branch operation, remote repository are going to talk about. Please make a detour
Version control system
In the development process, we often need to modify or delete a file, but we want to save the history of the file, if through backup, it will be very complicated to manage.
What is a version control system
Version Control System: A System that records changes to the contents of one or more files for future reference of revisions to a particular Version.
Version control system can not only be applied to the text file of software source code, but also can control the version of any type of file.
Classification of version control systems
See article: Introduction to version control
Local version control system
A local version control system is a system that records changes in versions on a single machine to ensure that content is not lost. If you have multiple developers, everyone is working on different systems and computers, and you can’t work together.
Centralized version control system
The SVN is a centralized version control system. A centralized version control system has a single centrally managed server (central server) to store the revised versions of all files. All users can access this server through clients to retrieve the latest files or submit updates.
- A central server is required to manage versioning and backups of code
- All user computers either fetch code from the central server or submit local code to the central server
- Depending on the network environment, if you cannot connect to the central server, you cannot submit and retrieve code.
Distributed version control system
Git is a distributed version control system. The client of a distributed version control system does not just take a snapshot of the latest version of the file, but mirrors the entire code repository. This way, a failure of any of the co-operating servers can be later recovered using any of the mirrored local repositories. Because each extraction operation is actually a complete backup of the code repository
- A server is required as a code repository
- Each user’s computer is a server (code repository), and is mirrored with the code repository, and the user’s modification and acquisition of code are submitted to their own server.
- You don’t need a network to work.
- When connected to the network, users can choose to synchronize their servers with the code repository.
Git Basics
Git is a free, open source distributed version control system designed to handle any project, small or large, with agility and efficiency.
Git is an open source version control software developed by Linus Torvalds to help manage Linux kernel development.
The installation of the git
Download address
Note:
- Do not install in a Chinese directory
- Do not use desktop management software
Installation is simple, just go to the next step. Right-click in any directory, you can see the menu, indicating that the installation is successful.
Git three area
To manage a project with git, initialize a git repository with git init, which creates a hidden folder in the current directory. Do not modify anything in this folder.
Git repositories are divided into three zones
Workspace: The directory where we write code is called workspace.
Staging: An area of temporary storage. In Git, code cannot be submitted directly from a workspace to the repository. Instead, it needs to be added from the workspace to staging before it can be submitted from staging to the repository. The purpose of the staging area is to avoid misoperation.
Local repository: Dump content stored in staging area permanently to Git repository to generate version numbers. After the version number is generated, you can go back to a specific version at any time.
Git Basic commands
git init
- Use git to initialize a git repository. You need to use Git to manage a project
git init
Initialize
Create a hidden folder in the current directoryCopy the code
git add
- Function: File by
The workspace
Added to theThe staging area
In Git, files cannot be added directly from workspace to warehouse. You must first add files from workspace to staging area and then from staging area to warehouse area. - Command:
Git add File name/directory name
Git add *.js # git add *.js # git add *.js # Git add. Git add -a git add --allCopy the code
git commit
Effect: Add file from temporary storage area to warehouse area, generate version number
Git commit -m "commit" git commit -m If it is a temporary file, it can be committed quickly. If it is not traced, the command will not take effect. Git commit -a -m 'submission instructions # modified the latest submission instructions, if accidentally mistyped the submit all, use this command git commit, amend the -m "submit"Copy the code
Git config configuration
If the submission is the first time, you need to configure the sender information, which is recommended to be the same as the github email address
Git config user.email: git config user.email: git config user.email: git config user.email: git config user.email Git config --global user.name Jepson git config --global user.email [email protected] config --listCopy the code
git status
- Function: View the file status
-
Git status command
- Red indicates that files in the workspace need to be submitted
- Green indicates that files in the staging area need to be submitted
-
Git stauts -s to simplify the log format
git log
- Effect: View the commit log
git log
View the submitted logsgit log --oneline
Concise log information
Git contrast
git diff
Git diff can see the difference in each commit
Git diff --cached git diff --cached Git diff c265262 de4845b git diff c265262Copy the code
Git reset
git reset
-
Role: Version rollback, which restores the code to a version that has been committed.
-
Git reset –hard Version resets the code to a specified version (only the first few digits of the version are required)
-
Git reset –hard head~1 reverses the version back to the last commit
- ~1: indicates the last submission
- ~2: last submission
- ~0: current submission
Git reset --soft: only reset the repository git reset --mixed: Git reset [default] Git reset --hard version number: reset the warehouse and staging area and workspace. Git reset version number: same effect as --mixedCopy the code
- When using the
git reset
After the command is executed, the version will be rolled backgit log
Only the information before the current version is displayed. usegit reflog
You can view all version information
Git ignores files
There are some files in the repository that you don’t want to be managed by Git, such as configuration passwords for data, and how to write code. Git can be configured to ignore files so that they don’t have to be committed.
- Create one at the root of the repository
.gitignore
The file name is fixed. - Add file paths that don’t need to be managed by Git to
.gitignore
In the
Gitignore # Ignore the index.js file in CSS /index.js # Ignore all js files in CSS /*.js # Ignore all files under CSS /*.* # Ignore CSS folder CSSCopy the code
Git branch operations
A branch is a parallel universe in a science fiction movie, where you are trying to learn Git while you are trying to learn SVN in another parallel universe.
If the two universes didn’t interfere with each other, it wouldn’t matter to you right now. At some point, though, the two parallel universes merged, and as a result, you learned both Git and SVN!
Why branches?
- If you want to develop a new feature, it takes 2 weeks, you can only write 50% of the code in the first week, and if you submit it immediately, the code is not finished, and the incomplete code will affect the work of others. If you wait until the code is written before submitting it, it’s easy to lose the code.
- With branches, you can create a branch of your own, invisible to others, and not affect others. You work on your branch, commit to your branch, and once the features are developed, merge into the original branch. This is safe and does not affect other people’s work.
Git branch command
In Git, a branch is essentially just a pointer that moves backwards after each code commit, ensuring that it always points to the last version committed. Git uses HEAD to point to the current branch
Create a branch
Git Branch Branch name
Create a branch, and the code in the branch is exactly the same as the current branch when it is created.- Git has a name called git on its first commit
master
The main branch of. git branch dev
, creating a branch called dev
See the branch
git branch
You can look at all the branches,- There will be one before the current branch
*
- In Git, there is a special pointer
HEAD
Will always point to the current branch
Switch branch
Git Checkout branch name
The HEAD pointer points to another branch- Anything done on the current branch does not affect other branches unless a branch merge occurs.
- When the code is submitted, the version number is produced and the current branch points to the latest version number.
Create and switch branches
-
Git checkout -b branch name creates and switches branches
-
Switching branches does two things
- Create a new branch
- Points the head pointer to the current branch
Delete the branch
Git branch -d Specifies the branch name
You can delete branches- Note: You cannot delete the current branch from the current branch. You need to switch to another branch to delete the current branch.
- Note:
master
Branches can be deleted, but this is not recommended.
Merging branches
Git merge branch name
Merges the contents of other branches into the current branch.- in
master
Execute in branchgit merge dev
将dev
The code in the branch is merged intomaster
branch - Branch merge
Git merge conflict
- For the same file, if multiple branches need to be merged, conflicts are easy to occur.
- When merging branches, if there is a conflict, you have to handle it manually and commit it again, usually by putting your own code after the conflicting code.
Git remote repository
At present, common remote warehouses include Github, code cloud, etc.
Making and git
Git is not directly related to Github.
- Git is a version control tool.
- Github is a code hosting platform, open source community, and remote repository for Git.
//1. GitHub is a hosting platform for open source and private software projects. It is named gitHub because it only supports Git as the only version library format for hosting. //2. Github is free. Everyone can see the code, but only you can change it yourself. Paid ones can be hidden. //3. When creating a Git project, do not include Chinese.Copy the code
git clone
- Action: Clone remote repository code to local
- Git clone
git clone git://github.com/jepsongithub/test.git
A new one will be created locallytest
Folder, which contains one in test.git
Directory, used to save all version records, and test file also has the latest code, you can directly for subsequent development and use.- Git clones use the project name of the remote repository by default, or you can specify it yourself. To do this, run the following command:
Git clone
git push
- Action: Commits code from a local repository to a remote repository
Git push repository master
Before submitting code to the remote repository, note that the master branch must be written and cannot be omitted- Example:
git push [email protected]:jepsongithub/test.git master
If you want to use github for the first time, enter the github user name and password
git pull
- Function: Downloads remote code locally
- Usually you need to pull once before pushing.
Get updates to the remote repository and merge git pull with the local branchCopy the code
git remote
Each push operation needs to bring the address of the remote warehouse, which is very troublesome. We can set an alias for the address of the warehouse
# set an alias to the remote warehouse git remote add alias warehouse Warehouse address git remote add jepson [email protected]: jepsongithub/test. The git # delete jepson this alias git Remote remove jepson # Git Clone repository has an origin alias by defaultCopy the code
SSH password-free login
Git supports multiple data transfer protocols:
- The HTTPS protocol:
https://github.com/jepsongithub/test.git
A user name and password are required - SSH protocol:
[email protected]:jepsongithub/test.git
You can configure password-free login
Each push or pull code, if using HTTPS, requires the user name and password to confirm identity, which is very troublesome.
- For account security, Github needs to verify the user’s identity for every push request. Only legitimate users can push
- Using SSH, you can configure SSH password-free to push codes to Github without password
SSH password-free login configuration
Note: These commands need to be typed in bash
-
1 Create an SSH Key: ssh-keygen -t rsa
-
2 Locate the.ssh folder in the file path C:\ user \ current username
-
3 There are two files in the folder:
- The private key:
id_rsa
- Public key:
id_rsa.pub
- The private key:
-
4 On github > Settings > SSH and GPG Keys, create an SSH key
-
5 Paste the contents of the public key ID_rsa. pub to the corresponding text box
-
5 Create a new repository on Github or use the existing repository and go to [email protected]: username/repository name.git
-
6 After that, communicate with Github over SSH again without entering a password for identity confirmation
Afterword.
Hello, I am the South Pole ice cube, a technology and appearance level proportional to the front-end engineer, advocating to solve front-end problems, I hope my blog has helped you.
Pay attention to me and walk together on the front road. Hey ~ 😛