Using Git for Version Control
120021. Concepts
- What is Git
Git is a distributed version control system that records the history of file modifications and version changes, allowing multiple people to collaborate on a project. Using Git facilitates version control and code management, reducing the risk of code conflicts and loss.
Git has three important concepts: Working Directory, Staging Area, and Repository. The working directory refers to the folder on your local computer where project files are stored, the staging area is the region on your local computer where modifications to be committed are kept, and the repository is the storage for historical versions on your local computer or a remote server.
2. GitHub
- Introduction to GitHub
GitHub is a code hosting platform based on Git that allows for the storage of code repositories and collaboration among multiple developers. On GitHub, you can browse the code of other open-source projects, submit your own code, and communicate and collaborate with other developers.
GitHub is an open community where you can enhance your technical skills by contributing code to other open-source projects. Additionally, GitHub can also be used for storing and managing private code repositories.
- Similar Products
GitLab:
GitLab is a code hosting platform similar to GitHub, offering similar functionalities along with some features not available on GitHub, such as built-in continuous integration and container registry functionalities.Bitbucket:
A code hosting service provided by Atlassian, supporting both Git and Mercurial, and offering integration with tools like Jira and Confluence.GitKraken:
A visual Git client that supports Windows, macOS, and Linux, providing a graphical interface for users to manage version control and collaborative development easily.SourceForge:
A platform that provides code hosting, issue tracking, discussion forums, and other services, historically one of the earliest open-source project hosting services.Coding.net:
A leading developer community and code hosting platform in China, offering services such as code hosting, team collaboration, and continuous integration.
3. Installing Git
- Installing Git on macOS
Using Homebrew:
Open the terminal and run the following command:brew install gitUsing Xcode Command Line Tools:
If you have Xcode installed, you can install the Xcode Command Line Tools, which includes Git, by running the following command:xcode-select --install
- Installing Git on Windows
Using the official Git installer:
Download the official Git installer and follow the installation wizard. Download link: GIT FOR WINDOWSUsing Chocolatey:
If you use the Chocolatey package manager, you can run the following command to install:choco install git
- Using Git
Configure user information:
After installing Git, you need to configure your user information, including your username and email. Run the following commands, replacing with your actual information:git config --global user.name "Your Name" git config --global user.email "your.email@example.com"Initialize a repository:
Run the following command in your project directory to initialize a Git repository:git initClone a repository:
To clone a project from a remote repository, run the following command:git clone remote_repository_urlAdd and commit files:
Use the following commands to add files to the staging area and commit:git add filename git commit -m "Your commit message"Check status and history:
Use the following commands to check the repository status and commit history:git status git logPush and pull:
If collaborating with a remote repository, you can use the following commands to push and pull changes:git push origin branch_name git pull origin branch_nameCreate branches and merge:
Use the following commands to create a new branch and merge branches:git branch new_branch git checkout new_branch git merge branch_to_mergeOther common commands:
Other commonly used Git commands include git diff (to view changes), git branch (to view branches), git remote (to view remote repositories), etc.
Please note that this is just the basic usage of Git; Git has many advanced features and options that can be explored in the official Git documentation or other tutorials based on specific needs.
4. Common Git Commands
Here are some commonly used Git commands:
Initialize a repository:
git initClone a repository:
git clone remote_repository_urlAdd files to the staging area:
git add filenameCommit changes to the repository:
git commit -m "Your commit message"Check status:
git statusView commit history:
git logView specific file modifications:
git diff filenameCreate a branch:
git branch branch_nameSwitch branches:
git checkout branch_nameor
git switch branch_nameCreate and switch to a new branch:
git checkout -b new_branch_nameor
git switch -c new_branch_nameMerge branches:
git merge branch_to_mergeDelete a branch:
git branch -d branch_namePush changes to the remote repository:
git push origin branch_namePull changes from the remote repository:
git pull origin branch_nameView remote repository information:
git remote -vCreate a tag:
git tag tag_nameView tags:
git tagUndo changes in the working directory:
git restore filenameUndo changes in the staging area:
git restore --staged filenameView help:
git --help
This is just a small portion of Git commands; Git has many more features and options. You can use git --help to view the complete help documentation for Git or refer to the official Git documentation for more detailed information.
5. Using Git in VScode
Using Git in Visual Studio Code requires some configuration and basic Git command usage. Here are the steps for configuration and basic usage:
- Configure Git:
First, ensure that Git is installed on your system. Then, configure Git in VS Code:
Open VS Code, select "Extensions" from the left menu, search for and install the Git extension.Configure user information:
Open the terminal and execute the following commands to configure global user information:git config --global user.name "Your Name" git config --global user.email "your.email@example.com"
- Using Git in VS Code:
VSCode uses Source Control by default to manage Git, and common Source Control operations are as follows:
Initialize Repository: Initialize a Git repositoryCommit All: Commit all changes to the Git repositoryPush: Push local changes to the remote repositoryPull: Pull changes from the remote repository to localCreate Branch: Create a new Git branchSwitch to Branch: Switch to another Git branchMerge Branch: Merge a Git branch into the current branchView History: View Git commit history
Source Control can be opened through the Source Control icon in the left sidebar, supporting Git, Subversion, and other version control tools, allowing for easy management of code versions, viewing modification history, and performing code merges. Additionally, VS Code supports many version control plugins, such as GitLens, GitHistory, etc., which can further extend the functionality and usage of Source Control.
6. Assignment
- Create a repository on GitHub to document your own notes.