Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the git commands for Linux administrators and developers

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "what are the git commands for Linux administrators and developers", which are easy to understand and well-organized, hoping to help you solve your doubts? let the editor lead you to study and learn what git commands are applicable to Linux administrators and developers.

Git is an open source distributed version control system that allows developers to track changes in their source code. It is a tool widely used by open source developers. Although it is designed to coordinate programming tasks, Git can effectively track any set of files. Moreover, it was developed by Linus Torvalds, the person behind the Linux kernel. So if you are an open source developer who adds functionality to your software over time, or is working with multiple partners to develop cutting-edge enterprise products, Git may be an ideal tracking system for your work. Join us to learn some basic git commands that will greatly simplify your development cycle.

Git CLI provides a large number of git commands to make software development easier for developers. For the convenience of readers, our editors have outlined some useful commands. So keep reading and find them at your own pace.

1. Configure user Profil

You can configure the git profile using the git config command. You can at least set up a user name and email address. Git allows users to configure these policies globally or based on projects. Use the following command to set the user and email address for each repository.

Linuxidc@ubuntu:~/www.linuxidc.com$ git config user.name "linuxidc" linuxidc@ubuntu:~/www.linuxidc.com$ git config user.email root@linuxidc.net

Add the-global option to set these policies globally.

Linuxidc@ubuntu:~/www.linuxidc.com$ git config-global user.name "linuxidc" linuxidc@ubuntu:~/www.linuxidc.com$ git config-global user.email root@linuxidc.net

two。 Initialize the Git repository

The git repository or simple repo is the root directory of your open source project. It contains subdirectories of source files, objects, headers and tags, etc. You can easily initialize git repo using the following command.

Linuxidc@ubuntu:~/www.linuxidc.com$ git init

The empty Git repository has been initialized in the

/ home/linuxidc/www.linuxidc.com/.git/

This is one of the most common git commands in your life. Now you can start to add the source file and modify it as needed.

3. Add project files

Using git to add files to an existing project is very easy. You can easily add all modified files / directories to the tracking system using the git add command. Take a quick look at the following example to see how it works.

Git add file linuxidc@ubuntu:~/www.linuxidc.com$ git add *

When you issue the git add command, it adds all files from the current working directory to the project index. You can specify a specific file as described in the first example. The second example adds all PHP files to the index. Git marks it as staging.

4. Verify added files

You can use the git status command to verify the files that will be temporarily saved during the next commit. It displays all new or changed files.

Linuxidc@ubuntu:~/www.linuxidc.com$ git status

When you want to view the details, run the command above. It will display a summary list of all files to be submitted.

5. Commit changes to the repository

When you commit changes, git takes a snapshot of the code base. This is how git tracks changes and provides version control. You need to use the git commit command for this.

Linuxidc@ubuntu:~/www.linuxidc.com$ git commit

When you run the above command, git asks you to enter some information, such as adding a description. It invokes the default Linux editor that you set up during git installation. Use the following command to avoid this misconduct.

Linuxidc@ubuntu:~/www.linuxidc.com$ git commit-m "Linuxidc Commit"

Therefore, if you use the-m option, you can add a description directly.

6. Show Log

You can view the log as long as you want to see the changes made to the repository. Just use the git log command on the Linux terminal to do this.

Git log git log-file linuxidc@ubuntu:~/www.linuxidc.com$ git log commit 482b4bebada278b29001338411de7c42743065d5 (HEAD-> master) Author: linuxidc Date: Thu Apr 30 14:21:46 2020 + 0800 Linuxidc Commit

The first example displays general information about git submissions. If you only want to see the changes in a specific file, use the second command. You can also add more options, such as the-log-size option, or even use regular expressions to search for submissions.

Submit and display the log.

7. Validate the project branch

The git branch represents a separate development line in your project. You can easily check the current branch using the git branch command. It shows that you are developing new features or modifying the currently active branch of older features.

Linuxidc@ubuntu:~/www.linuxidc.com$ git branch * master

* the master output will mark the current branch with an asterisk (*).

8. Reset project branch

You can easily reset the current repository and working directory to a known state. The git reset command adjusts the HEAD reference to a specific commit and updates the index accordingly to match that particular commit.

Linuxidc@ubuntu:~/www.linuxidc.com$ git reset

Use the following command to perform a soft reset on your current branch.

Linuxidc@ubuntu:~/www.linuxidc.com$ git reset-soft

You can also perform a hard reset in a similar manner. Just replace-soft with difficult options, as demonstrated in the following example.

Linuxidc@ubuntu:~/www.linuxidc.com$ git reset-hard

9. Add a new branch

Adding new branches allows you to work on newer features independently. You can easily add branches using the git branch command. Just add the branch name, as shown below.

Linuxidc@ubuntu:~/www.linuxidc.com$ git branch new-linuxidc

Verify that the addition was successful by issuing the git branch command. It should show the newly created branch, called new-linuxidc. However, you cannot add multiple branches with the same branch name. It will lead to fatal errors.

10. Switch between branches

You can easily switch between branches of a project using the checkout command. It is one of the most common git commands you use during software development. Skim through the next example to see how it works.

Linuxidc@ubuntu:~/www.linuxidc.com$ git checkout new-linuxidc

This command informs you that the branch has been successfully switched. You can also verify this using the git branch command shown earlier.

11. Delete project branch

After successfully developing new features, you want them to be added to the git master branch. After you have done this, you can delete the branch completely. The-D option of the git command makes it easy to do this.

Linuxidc@ubuntu:~/www.linuxidc.com$ git checkout master

Switch to branch 'master'

Linuxidc@ubuntu:~/www.linuxidc.com$ git branch-D new-linuxidc

Branch new-linuxidc (formerly 482b4be) has been deleted.

C4488c.webp "target=" _ blank ">

You need to leave the branch before you can successfully delete it. Otherwise, git will throw an error.

twelve。 Check for differences between submissions, trees, and files

The git diff command allows us to view changes in two multiple files (the working tree and the index tree) between commits and between blob objects. It is one of the most basic git commands used to track changes to the code base.

Git diff git diff new-linuxidc master

The first example shows the changes between the working tree and the index tree. The second example shows the changes between the master branch and the new-linuxidc branch.

13. Merge two branches

You can easily merge two different development branches using the git merge command. It merges two branches into one unified branch. You can use the git merge command for a variety of purposes. Take a look at the following examples to see how to use them. Git merge fixes new-linuxidc

Git merge fixes new-linuxidc git merge-s ours obsolete git merge-- no-commit main

Git merge-- the first example of no-commit main combines the new functionality of the two branches and fixes them to create a branch. The second example uses our strategy to merge the obsol] branch into the current development branch. The final example merges the branch main into the current branch, but disables autocommit.

14. Restore an existing submission

Sometimes, you may decide that certain submissions are no longer needed. In this case, it is better to restore these commits than to modify the branch completely. The git revert command allows us to do this.

Git revert 482b4bebada278b29001338411de7c42743065d5 git revert HEAD~3

The first example restores the changes introduced by the commit ID 482b4bebada278b29001338411de7c42743065d5. The second example repeats the fourth last commit in HEAD and executes a new commit.

15. Hide the working directory

You can temporarily save the current state of the working directory somewhere and return to that directory later if you need it. This is called hiding in git terminology. It simply stores the status and index of the working directory so that you can work with the new content.

Git stash

Developers usually use this command when they are in a mess. It allows them to store untidy workflows and resolve them later. Use the Storage list command to view the storage list.

Git stash list

16. Clone warehouse

One of the best things about open source is that you can use other people's code like other people's code. Git makes it easy to download existing projects using the git clone command. Look at the illustration below to see how it works in real life.

Git clone git clone git://example.com/git.git/ test-dir

This downloads the project to the test-dir directory of the system.

17. Extract new updates

Real-life projects are developing all the time. Suppose you cloned a repo from the remote repository earlier. What will you do when developers update new features to the repository? It is inconvenient to clone the same repo to the local machine over and over again. The git pull command avoids this situation.

Git pull

This command updates the local version of the project with any new updates made by the collaborator. Remember to change the working directory to the project directory before getting the latest updates.

18. Push your updates

After you have finished processing the update, you can add it to the remote repository by adding it. The difference between git push and git commit is that when you commit some changes, they are added to the local repository rather than to the remote repository.

Git push

This command adds your updates to the remote repository of the project. Typically, you will use push and pull to collaborate with remote developers. Therefore, it is important to master them perfectly.

19. Show remote repository

The git remote command allows us to easily manage a set of trace repositories from a Linux terminal. You can use it to clone only some selected branches.

Git remote git remote-verbose

The first example shows all remote repositories that are currently configured. Adding the-verbose flag will show us more information about this information.

20. Connect to a remote warehouse

You can set up a remote warehouse to connect the local warehouse to the remote server. This allows you to push local changes directly to the remote server.

$git remote add origin

The face command adds "origin" to the server as a remote name. You can discover the server URL by browsing the Source subtab of the GitHub repository.

The above is all the contents of the article "what are the git commands for Linux administrators and developers?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report