A guide to using Git on the Mac


This guide explains how to use Git on a Mac. Includes installing Git, creating Git repositories, configuring Git, committing code, branch management, remote repositories, cloning repositories, and undoing changes. Using these commands, you can better manage your code and collaborate with others on development.

1. Install Git

First, you need to have Git installed on your Mac. Follow the steps below:

  • Open a terminal.
  • HomebrewInstall Git using . Enter the following command in the terminal:
brew install git

2. Create a Git repository

Before using Git, you need to create a Git repository on your local computer. Follow the steps below:

  • Open a terminal.
  • Use the cd command to enter the directory where you want to create the Git repository.
  • git initInitialize a Git repository using the command. For example:
cd Documents/my-project
git init

3. Configure Git

Before using Git, you need to configure Git user information. Follow the steps below:

  • Open a terminal.
  • Use the git config command to configure Git user information.
git config --global user.name "Your Name"
git config --global user.email "[email protected]"

4. Submit the code

Committing code is one of the core functions of Git. Follow the steps below:

  • Open a terminal.
  • Use the cd command to enter the directory where the Git repository is located.
  • Use the git add command to add the modified files to the staging area.
git add file1.txt file2.txt
  • Use the git commit command to submit the changes in the temporary storage area to the warehouse.
git commit -m "Initial commit"

5. Branch management

Branching is another core feature of Git. Follow the steps below:

  • Open a terminal.
  • Use the cd command to enter the directory where the Git repository is located.
  • Use git branchthe command to view all branches.
git branch
  • Use the git checkout command to switch to other branches.
git checkout other-branch
  • Use the git merge command to merge branches.
git merge other-branch

6. Remote warehouse

It is a good practice to store code in remote repositories. Follow the steps below:

  • Create a new repository on a Git hosting site such as GitHub or Bitbucket.
  • Open a terminal.
  • Use the cd command to enter the directory where the Git repository is located.
  • Use the git remote add command to associate the local warehouse with the remote warehouse.
git remote add origin https://github.com/your-username/your-repo.git
  • Use the git push command to push the changes in the local warehouse to the remote warehouse.
git push origin main

Among them, origin is the name of the remote warehouse, and main is the name of the branch.

7. Clone warehouse

If you want to use code from a remote repository on your local computer, you can clone the repository. step:

  • Open a terminal.
  • Use the cd command to enter the directory where you want to clone the repository.
  • Use the git clone command to clone the repository.
git clone https://github.com/your-username/your-repo.git

8. Undo changes

Occasionally, it may be necessary to undo changes to the code. Follow the steps below:

  • Open a terminal.
  • Use the cd command to enter the directory where the Git repository is located.
  • Use the git status command to view modified files.
git status
  • Use the git checkout command to undo changes to files.
git checkout file1.txt

Guess you like

Origin blog.csdn.net/qq_54351538/article/details/129340963