-
conceptual
- What is git?
- What is the difference between git and GitHub?
- Commits, repositories, branches
-
Using git
-
Downloading other people's code from GitHub
- Download the code directly to your local
- Clone the repository to get the code
- Uploading your own code to GitHub
-
Downloading other people's code from GitHub
This article is intended to describe the use of git in layman's terms, if there is any discrepancy, please criticize and correct!
conceptual
What is git?
Git can be thought of as a super-efficient "time machine" designed specifically for programmers to save and manage their code. Just as you can use a time machine to go back in time and see what you used to look like, Git allows developers to go back to past versions of their code and see what it looked like before. A tool to help developers manage their code efficiently and make programming more organized and secure.
What is the difference between git and GitHub?
Git and GitHub are two different concepts, but they are often used together in software development.
Here's how they differ:
-
Git is a version control system for tracking and managing the change history of your code. It is a local tool, which means you can use it on your own computer without the need for a network connection.Git provides powerful features such as branching, merging, and rolling back to help developers manage different versions of their code.
-
GitHub is a Git-based online service that provides a platform to host and manage code repositories. It is a remote service and requires an internet connection to use.GitHub offers additional features such as a user interface, collaboration tools, code review, issue tracking, a wiki, and more.
Simply put, with Git, you can create, modify, and delete files on your own computer and view a detailed history of each operation. GitHub, on the other hand, provides a platform. using GitHub, you can upload your local code repository to the web so that others can see, download, comment, and contribute to your code. gitHub also provides some social features, such as following projects, liking, and discussing them, which all help to build an active developer community.
Commits, repositories, branches
Before we learn how to use git, let's first understand a few core concepts of git
-
Commit:
Committing is all about saving your work. In Git, when you finish making some changes, like writing code or modifying files, you can commit those changes. This is like telling Git, "Hey, I've done all this work, save it for me." Each commit documents the changes you've made, and you can leave some notes telling people why you did it. -
Repository:
The repository is where you put all your work. It contains all your files and the changes you make every time you commit. You can think of it as a folder, but this one is so smart that it remembers what you saved each time and helps you manage the different versions. -
Branch:
Branching is like when you have multiple tasks to do but don't want to mix them up. You can create a new branch to specialize in one task. That way, you can work on that branch without affecting your other tasks. When you're done with that task, you can merge that work back into the main branch, like merging two story lines together.
Simply put, a commit is where you save your work, a repository is where you store that work, and a branch is a tool that helps you work on multiple tasks at once.
Using git
Downloading other people's code from GitHub
Download the code directly to your local
This way is simple and rough, but by directly downloading to the local code project will be an ordinary folder, if the GitHub repository has been updated, you can not synchronize the updated content by pulling it, you can only download it again, which lacks convenience.
Clone the repository to get the code
-
Find the code repository (same method as above):
First, you need to find the GitHub repository where the code you want to download is located. Typically, this can be done by visiting the GitHub website in your browser and searching for the name of the project or by typing in the project's URL directly. -
Cloning Warehouse:
- Once you have found the project, you can use the git clone command to clone the repository. This requires that you have a command line or terminal open on your local computer. To open a terminal or command line: right click in the folder where you want to store your code - > click "Open Git Bash here".
- Use the clone command in the terminal: git clone [repository URL. (Here [repository URL] is the URL of the project on GitHub.) For example:
Waiting for the clone to finish: After executing the clone command, Git will start downloading the entire repository, including all branches and tags. This may take some time, depending on the size of the repository.
-
Open the project: After cloning is complete, you can use the cd project name command on the command line to enter the project directory and then open the project using your favorite code editor.
-
Pull latest changes (if needed): If you have cloned the repository but want to get the latest changes, you can use the git pull command to update your local copy.
-
Setting up a remote repository (if needed): If you want to contribute code to a project, you may need to set up a remote repository so you can push your changes. Typically, this can be done by running git remote add origin [repository URL], but this is usually set automatically at cloning time.
Uploading your own code to GitHub
-
Create a GitHub account: If you don't already have a GitHub account, you'll first need to sign up for one.
-
Create a new repository: Log in to your GitHub account, click the "+" sign in the top right corner of the page, and select "New repository".
-
Fill in the name of the repository, description (optional), choose whether it is public or private, and click "Create repository".
-
Initialize your local repository: If your project directory is not already a Git repository, you need to initialize it. Open a terminal or command line, navigate to your project directory, and run:
git init
-
Add files to the repository: Add your project files to the staging area of the local repository: run in the files directory of the terminal:
git add .
-
Commit Changes: Commit your changes to the local repository:
git commit -m "Initial commit"
-
Add a remote repository: link a repository on GitHub to your local repository. This is usually done automatically when you create the repository, but if not, you can use the following command:
git remote add origin /your username/repository name.git
-
Push to GitHub:
Push changes from your local repository to a remote repository on GitHub:git push -u origin master
If your remote repository uses the default branch name main, replace master with main. -
Set access privileges:
If your repository is private, make sure your GitHub account has permission to push code. -
Check the GitHub page:
Once the push is complete, visit your GitHub repository page to check if your code has been successfully uploaded. -
Subsequent submissions:
Each time you want to upload new changes in the future, just repeat steps 4 through 7.
Note that if you already have a .git directory in your project, then your project is already a Git repository and you can proceed directly from step 3. Also, if you have any problems pushing, for example because the remote branch doesn't exist, you may need to create the remote branch first or use thegit push -u origin master --set-upstream
to set the upstream branch.