Using Git for Version Control

12002

1. 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
  1. 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.

  2. Bitbucket:
    A code hosting service provided by Atlassian, supporting both Git and Mercurial, and offering integration with tools like Jira and Confluence.

  3. 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.

  4. SourceForge:
    A platform that provides code hosting, issue tracking, discussion forums, and other services, historically one of the earliest open-source project hosting services.

  5. 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
  1. Using Homebrew:
    Open the terminal and run the following command:

    brew install git  
  2. Using 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
  1. Using the official Git installer:
    Download the official Git installer and follow the installation wizard. Download link: GIT FOR WINDOWS

  2. Using Chocolatey:
    If you use the Chocolatey package manager, you can run the following command to install:

    choco install git  
  • Using Git
  1. 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"  
  2. Initialize a repository:
    Run the following command in your project directory to initialize a Git repository:

    git init  
  3. Clone a repository:
    To clone a project from a remote repository, run the following command:

    git clone remote_repository_url  
  4. Add 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"  
  5. Check status and history:
    Use the following commands to check the repository status and commit history:

    git status  
    git log  
  6. Push 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_name  
  7. Create 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_merge  
  8. Other 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 init  
  • Clone a repository:

    git clone remote_repository_url  
  • Add files to the staging area:

    git add filename  
  • Commit changes to the repository:

    git commit -m "Your commit message"  
  • Check status:

    git status  
  • View commit history:

    git log  
  • View specific file modifications:

    git diff filename  
  • Create a branch:

    git branch branch_name  
  • Switch branches:

    git checkout branch_name  

    or

    git switch branch_name  
  • Create and switch to a new branch:

    git checkout -b new_branch_name  

    or

    git switch -c new_branch_name  
  • Merge branches:

    git merge branch_to_merge  
  • Delete a branch:

    git branch -d branch_name  
  • Push changes to the remote repository:

    git push origin branch_name  
  • Pull changes from the remote repository:

    git pull origin branch_name  
  • View remote repository information:

    git remote -v  
  • Create a tag:

    git tag tag_name  
  • View tags:

    git tag  
  • Undo changes in the working directory:

    git restore filename  
  • Undo changes in the staging area:

    git restore --staged filename  
  • View 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:
  1. 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.

  2. 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 repository
  • Commit All: Commit all changes to the Git repository
  • Push: Push local changes to the remote repository
  • Pull: Pull changes from the remote repository to local
  • Create Branch: Create a new Git branch
  • Switch to Branch: Switch to another Git branch
  • Merge Branch: Merge a Git branch into the current branch
  • View 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.