With the popularization of technology and the advent of the open source era, more people are beginning to pay attention to GitHub. As the world's largest open source community and version control management platform, GitHub is an essential tool for programmers. It facilitates the storage and management of code, as well as the exchange and sharing of code. This article will introduce how to set up a GitHub environment on a Mac computer.
The first step is to install Git
Git is a free, open source distributed version control system, its main function is to manage source code. Installing Git on a Mac computer is a necessary step to set up a GitHub environment.
First open your terminal and use the following commands to install Homebrew.
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Then use the following command to install Git.
brew install git
After the installation is complete, you can use the following command to verify whether Git is installed correctly.
git --version
The second step is to register a GitHub account
Before using GitHub, you need to register a GitHub account. Visit [GitHub official website](https://github.com/), click the "Sign up" registration button in the upper right corner, and follow the prompts to complete the registration.
If you already have a GitHub account, you can proceed directly to the next step.
The third step is to create a local warehouse
Creating a local warehouse and connecting it to the warehouse in GitHub is a key step in setting up a GitHub environment.
First create a directory locally, for example: create a folder named "GitHub" on the desktop.
Use the cd command in the terminal to enter the directory.
cd ~/Desktop/ mkdir GitHub cd GitHub/
Then use the following command to initialize the warehouse.
git init
At this time Git will create a .git hidden folder under this folder. This folder will be used to store Git configuration and warehouse-related information.
Then use the following command to associate the repository with the GitHub remote repository.
git remote add origin git@github.com:username/repo.git
The username and repo are respectively your GitHub account and the name of the warehouse you want to create. This command will establish an SSH connection. Make sure you have added your SSH key to the GitHub website.(https://docs.github.com/en/github/authenticating-to-github/connecting-to-github- with-ssh).
The fourth step, submit the code
In the previous step, we have connected the local warehouse with the remote warehouse of GitHub. Now, we want to commit a new file to the repository.
Create a file called "README.md" in the "GitHub" folder and add some content.
Use the following command to add the file to the warehouse.
git add README.md
Then use the following command to submit the file to the local warehouse.
git commit -m "initial commit"
The "initial commit" here is the description of this submission record, you can change it at will.
Finally use the following command to push the file to the remote repository on GitHub.
git push -u origin main
main is the default branch. If you need other branches, please change it according to the actual situation.
The fifth step, view the submission record
In the previous step, we have successfully submitted the local code to GitHub. Now, we want to view the commit record.
Use the following command to view all commit records under the current branch.
git log
This command will list the information of all submission records, including the submitter, submission time, submission record information, etc.
If you want to view the details of a commit record, you can use the following command.
git show commit_id
The commit_id is the hash value of the submission record, which can be viewed through the git log command.
The sixth step, pull the code
In the process of collaborative development, in addition to pushing the code, you also need to pull other people’s code in a timely manner to ensure that your local code is consistent with the code on GitHub The code stays in sync.
Use the following command to pull the code on the master branch.
git pull origin main
This command will pull the latest code from the remote repository and merge it into the local repository.
If you need to pull the code of other branches, please replace main with the name of the branch that actually needs to be pulled.
So far, we have successfully set up a GitHub environment on a Mac computer. Remember to constantly learn and update your code, and collaborate with others on development. I wish you a successful and happy journey as a coder on GitHub!
The above is the detailed content of Detailed explanation of how to set up GitHub environment on mac. For more information, please follow other related articles on the PHP Chinese website!