[Git] A complete list of commonly used git commands (a must-read for beginners!! The most powerful and detailed explanation!!)

Table of contents

Beginners need to have a preliminary understanding of git before using it.

what is git

Common commands

1. Create a warehouse

2. Clone the remote repository

3. Query file status

4. Add files to the temporary storage area

5. Add all files to the temporary storage area

6. Submit the temporary storage area files to the locally built warehouse

7. Pull changes from the remote warehouse

8. Push changes from the local warehouse to the remote warehouse

9. Check the submission log records

10. Create a new branch

11. Switch to the specified branch

12. Merge the specified branch to the current branch

13. Delete the specified branch from the current branch

14. Create and switch to a new branch (equivalent to the combination of `git branch` and `git checkout`)


Beginners need to have a preliminary understanding of git before using it.

what is git

        Git is adistributed version control system used to manage and track changes to files. It is widely used in software development projects, but can be applied to other types of file management as well.

Git provides the following main functions:

  1. Version control: Git cantrack changes to files and record modifications to each version. You can easily view and compare differences between versions, and roll back to historical versions.

  2. Branch management: Git has a powerful branch function, you can create, switch and merge branches. This makes it easy for multiple people to collaborate on development and experimental work without affecting the mainline code.

  3. Collaboration and remote warehouse: Git supports multi-person collaborative development, and can push code to remote warehouses for sharing and backup. You can share code with team members and synchronize remote repositories with local code through pull and push.

  4. Undo and restore: Git allows you to undo submitted changes, including undoing deletion of files, undoing submitted changes, etc. Additionally, Git provides tools and commands to recover accidentally deleted data.

  5. Lightweight and efficient: Git is designed to be lightweight and efficient. It uses a mechanism called "snapshots" to save file changes, making it very fast when working on large projects.

Common commands

1. Create a warehouse

it's hot

 

        When you need to create a new Git repository, you can use the git init command. This command will create an empty Git repository in the current directory.

        By executing the git init command, your current directory becomes a Git repository. You can use other Git commands to manage, track, and commit file changes to the repository.

2. Clone the remote repository

git clone <remote warehouse address>

 

git cloneThe command will complete the following steps:

  1. First, you need to obtain the address of the target remote warehouse. This is usually a URL starting with https:// or git:// and represents the location of the repository on the remote server.

  2. Select a suitable folder locally as the clone target and open a terminal (command line tool).

  3. Run the git clone <远程仓库地址> command, replacing <远程仓库地址> with the address of the remote repository you want to clone.

  4. Git will download all files and version history from the remote repository and create a local copy of the repository corresponding to the remote repository.

  5. After cloning is completed, you can modify, submit, pull and push operations in the local warehouse, and synchronize or update with the remote warehouse.

3. Query file status

git status

        When using Git for version control, you may want to know which files have been tracked by Git after you have made some modifications, and which files have changed but have not yet been committed. To view the status of the current file, you can use the git status command.

git statusThe command will display the following file statuses:

  1. "Changes not staged for commit": These are files that are tracked by Git, but they have changed and have not been staged yet. This means that you have made changes to these files but have not yet committed them to the repository.

  2. "Changes to be committed": These are files tracked by Git that have been staged and are ready to be committed to the repository. This means that you have used the git add command to add these files to the pending submission status.

  3. "Untracked files": These are files that are not tracked by Git, meaning they have no history in the repository and are not included in version control. This may be a newly created file or a file that was ignored by .gitignore.

4. Add files to the temporary storage area

git add <filename>

 

        When using Git for version control, you may have some modified files that you want to add to the Git staging area so that they can be submitted to the warehouse later. To add files to the staging area, you can use the git add command.

  • Specifically,git add <文件名> command is used to add the specified file to the Git staging area. You need to replace <文件名> with the specific file name or file path you want to add.
  • When you run the git add command, Git will copy the specified files from the working directory to the staging area so that changes to these files can be included in subsequent commits.
  • By executing the git add command, you tell Git which files should be included in the next commit. Afterwards, you can use the git commit command to submit changes to these files to the warehouse to form a new version.

5. Add all files to the temporary storage area

git add .

6. Submit the temporary storage area files to the locally built warehouse

git commit -m "Commit instructions"

        When you make a series of modifications to the code, add these modified files to the Git staging area by using the git add command. Then, use the git commit -m "提交说明" command to submit the files in the staging area to the local warehouse, and attach a descriptive submission description.

        A commit description is a short text describing the purpose and content of your submission so that other developers or yourself can better understand its purpose. For example, you can write a description like "a bug has been fixed," "new features have been added," or "documentation has been updated."

        After executing the git commit -m "提交说明" command, Git will transfer the files in the temporary storage area to the local warehouse, and Create a unique identifier (commit ID) for this commit. In this way, you have completed a commit to the local repository and retained a snapshot of the changes you made.

7. Pull changes from the remote warehouse

git pull

        When you execute the git pull command, Git will automatically pull the latest code from the remote repository and merge it into the current branch. If conflicts arise, you need to resolve them before the merge can be successful.

        Pulling changes from the remote warehouse can ensure that your local warehouse is synchronized with the remote warehouse and obtains the latest work results of other team members, making it easier for you to continue working or collaborate with team members on development.

8. Push changes from the local warehouse to the remote warehouse

git push

        When you have made a series of code modifications in the local warehouse and have submitted these modifications to the local warehouse through the git commit command, you can use git push Command to push these commits to the remote repository.

        When executing the git push command, Git will push the commits in the local warehouse to the remote warehouse associated with it. In this way, other team members can get your latest code and collaborate on development.

It should be noted that before pushing, you need to ensure that your local warehouse and the remote warehouse are synchronized. If the remote repository has other people's commits and your local repository is not updated, a conflict will occur when executing the git push command. At this time, you need to pull the changes from the remote warehouse first (using the git pull command), resolve possible conflicts, and then push them.

9. Check the submission log records

git log

After executegit log command, Git will output all submission records of the current branch in reverse order of submission time. Each commit record contains the following information:

  • Commit hash: A unique identifier used to distinguish different commits.
  • Author: The name and email address of the submitter.
  • Submission date (date): Submission timestamp.
  • Commit message: Descriptive text attached to a submission to explain the purpose and content of the submission.

With thegit log command, complete information for each submission will be displayed by default. If you want to see only abbreviated commit information, you can use the --oneline option, such as git log --oneline.

10. Create a new branch

git branch <branch name>

        Creating a new branch means creating a new code branch in a Git project that is independent of the current branch. Independent development work can be carried out on this branch without affecting the code of other branches.

To create a new branch, use thegit branch command, followed by the desired branch name. For example, execute the following command to create a new branch named "new-branch":

git branch new-branch

        This will create a new branch pointer at the current location, but will not switch to this new branch. You are still stuck on the current branch.

        By creating a new branch, you can develop, experiment, or test on the new branch without affecting other branches. Afterwards, you can always switch to this new branch and continue working on it.

        ​​​​​Note: Creating a new branch only creates a new pointer and does not copy any code or files. The new branch initially points to the same commit as the current branch. The differences between the new branch and the current branch are only generated after the commit operation.

 

11. Switch to the specified branch

git checkout <branch name>

    git checkout <分支名>This command is used to switch to the specified branch in Git. Just like you take different roads or paths when traveling, switching branches allows you to work in different directions in your code development.

        Suppose you have a master branch (usually master or main), and a branch named feature development branch. When you want to work on new features, you can use the git checkout feature command to switch to the feature branch. This means that changes you make on this branch from now on will not affect the main branch.

        By switching to a specific branch, you can switch between different code branches as needed to perform multiple tasks or experimental development work at the same time. This allows team members to work independently on their branches in parallel without affecting each other.

12. Merge the specified branch to the current branch

git merge <branch name>

        Suppose you have a master branch (usually master or main), and a branch named feature development branch. Some new features or modifications were developed on the feature branch. When you think these changes are ready and want to integrate them into the main branch, you can use the git merge feature command.

        After executing this command, Git will apply the modifications on the feature branch to the current branch, so that the current branch contains the changes from feature The latest code changes for the branch. This is equivalent to merging the modifications of the two branches together, so that the current branch has the updated content of the other branches.

        By merging branches, you can combine code changes in different branches to ensure that the results of different development efforts are collected in one place. This is important for team collaboration and feature integration, as everyone can develop independently on their own branch and merge it into the shared master branch when needed.

13. Delete the specified branch from the current branch

git branch -d <branch name>

        Suppose you have a development branch named feature. When you think that the work of the branch has been completed and there is no need to continue to keep the branch, you can use git branch -d featureCommand to delete it.

        After executing this command, Git will delete thefeature branch and its related commit records from your local warehouse. However, please note that this command will only delete branches that have been merged into other branches. If it has not been merged yet, you need to use the -D parameter (i.e. git branch -D <分支名>) to delete the branch.

        By deleting branches that are no longer needed, you can simplify the branch structure in your code repository and reduce branch clutter. This improves the team's development efficiency and ensures that the code repository remains clean and easy to manage.

14. Create and switch to a new branch (equivalent to the combination of `git branch` and `git checkout`)

git checkout -b <branch name>

        Suppose you want to create a new branch named feature and switch your work from the current branch to this new branch. You can use thegit checkout -b feature command.

        After executing this command, Git will create a new branch based on the current branch, named feature, and then immediately switch to this new branch. This is equivalent to opening up a new development path in your code repository. You can work independently on this new branch without affecting other branches.

        Through this command, you can easily create and switch to new branches to work in parallel on different development tasks or features. This is very useful in team collaboration and feature development, because everyone can work independently on their own branch without disturbing the work of others.

Guess you like

Origin blog.csdn.net/miles067/article/details/132631368