Gits an open - source distributed version control system, which is developed by Linus Torvalds to help manage the development of the Linux kernel. It is designed to handle projects of any size, be it small or large, in an agile and efficient way. Different from common version control tools like CVS and Subversion, Git adopts a distributed repository approach and does not require server - side software support.
Git is a Distributed Version Control System (DVCS) and has two types of repositories: local repository and remote repository.
- Local Repository: It is a Git repository on the developer's own computer.
- Remote Repository: It is a Git repository on a remote server.
- Clone: It means copying the remote repository to the local.
- Push: It refers to uploading the code from the local repository to the remote repository.
- Pull: It is the action of downloading the code from the remote repository to the local repository.
Git Download and Installation
You can download Git from the official website: [https://git - scm.com/download](https://git - scm.com/download). After the download is completed, you will get the installation file (the 64 - bit version is downloaded by default here). Double - click the downloaded installation file to start the installation. Just keep clicking "Next" until the installation is completed. After the installation, right - click on the computer desktop (or other directories). If you can see two menus, "Git GUI" and "Git Bash", it means that Git is installed successfully.
Git Code Hosting Services
To set up a Git remote repository, we can make use of some code hosting services available on the Internet. Here are some commonly used ones:
- GitHub (URL: https://github.com/): It is a hosting platform for open - source and private software projects. Since it only supports Git as the only version library format for hosting, it is named GitHub.
- Gitee (Code Cloud) (URL: https://gitee.com/): It is a domestic code hosting platform. Because its server is in China, compared with GitHub, Gitee has a faster access speed.
- GitLab (URL: https://about.gitlab.com/): It is an open - source project for a repository management system. It uses Git as the code management tool and builds a web - based service on this basis.
Git Workflow
The work flow of Git is as follows:
- Clone the code from the remote repository to the local repository.
- Check out the code from the local repository, then modify the code.
- Before committing, submit the code to the staging area.
- Commit the code to the local repository. The local repository stores all the historical versions of the modifications.
- After the modification is completed, when you need to share the code with team members, push the code to the remote repository.
Concepts of Working Directory, Staging Area, and Repository
To learn Git better, we need to understand some related concepts, which will be frequently mentioned in the latter study.
- Working Directory (Workspace): The directory containing the .git folder is the working directory. It is mainly used to store the developed code, that is, the place where the project code files are stored.
- Staging Area (Stage / index): There are many files in the .git folder, among which the index file is the staging area, also called stage. The staging area is a temporary place to save the modified files.
- Repository (Local Repository): The .git hidden folder we saw before is the version library. The version library stores a lot of configuration information, log information, and file version information. Among them, HEAD points to the latest version put into the repository.
- Remote Repository: The server that hosts the code, such as GitHub.
Git Code Submission Steps
- Synchronize code from the remote repository: Use the command
git pull. - Check the current status: Use the command
git status. - Submit code to the local Git cache area: Use the command
git add. - Push code to the local Git repository: Use the command
git commit. - Submit local code to the remote repository: Use the command
git push.
Configuration of User Information
Before submitting code, you need to configure your email and name so that others can contact you if there are problems with your code. In the command line (cmd), use the following commands:
git config --global user.email "your_email@example.com"
git config --global user.name "Your_Username"
Cloning Code from GitHub
To pull code from GitHub, open the cmd window and use the following command:
git clone [repository_url] # Pull the code to the relative path you are in
Example of Git Submission
First, navigate to the file directory (for example, my_project directory) in the command line:
cd C:\Users\YourName\Desktop\my_project
Then, add the changes (the -A parameter means to add changes from all tracked and untracked files):
git add -A
Next, commit the changes to the local repository (the -m parameter is used to write the information about the changes, and the following is an example of a successful commit):
git commit -m "Add git_learning.txt"
[master d123456] Add git_learning.txt
1 file changed, 1 insertion(+)
create mode 100644 "git_learning.txt"
Finally, push the code to the remote repository (take a GitHub repository as an example):
git push
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 4 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 334 bytes | 167.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/your_username/your_repo.git
a123456..d123456 master -> master