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

Commonly used git command collation

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly introduces "commonly used git command finishing". In daily operation, I believe that many people have doubts about the commonly used git command finishing problems. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "commonly used git command finishing"! Next, please follow the editor to study!

Install git

To check if Git is installed, run it on the terminal:

$git versiongit version 2.27.0.rc1.windows.1

If it is not installed, follow the instructions on https://git-scm.com/downloads. Mac users can install it with brew: brew install git.

Configure git

We just need to configure something.

Git config-global user.name "Front end Xiaozhi" & & # your name git config-global user.email johndoe@example.com & & # your email git config-global init.defaultbranch main # default branch name, compatible with GitHub

You can view the current global configuration with the following command

Git config-global-list# Type ": Q" to close

Git stores the configuration in plain text. If you want to modify it directly, you can edit the global configuration directly in ~ / .gitconfig or ~ / .config / git/config.

As recommended by the command, removing-- global extends the scope of these commands to the current folder. But to test this, we need a repository.

Create a new repository

The repository is just a folder with everything we want to track. Create with the command:

Mkdir gitexample & & cd gitexample & & git init# gitexample git: (main)

This command creates a .git folder within the gitexample folder. The hidden .git folder is the version library: all local configurations and modifications are stored here.

Change

Create something in the repository:

Echo "Hello, Git" > > hello.txt

Run git status and we will see the newly created untracked files.

Git status# On branch main# # No commits yet# # Untracked files:# (use "git add..." To include in what will be committed) # hello.txt## nothing added to commit but untracked files present (use "git add" to track)

Git add.

If we don't want to add all the files, we can use the

Git add hello.txt

If you check the status of the repository now, you will see that the file has been added (also known as staged), but has not yet been submitted.

Git status# On branch main# # No commits yet# # Changes to be committed:# (use "git rm-- cached..." To unstage) # new file: hello.txt

To document these changes, let's submit it.

Git commit-m "Add hello.txt" # [main (root-commit) a07ee27] Adds hello.txt# 1 file changed, 2 insertions (+) # create mode 100644 hello.txt

Git commit-m is a short command, and you can use git commit to open the editor (mainly vim) and provide a detailed description of the submission.

Git log# Author: qq449245884 # Date: Sat Jul 17 14:57:24 2021 + 0800 qq449245884 # Add hello.txt#

Create a branch

In many cases, it is useful to have a separate initial version of the code: for example, to avoid code conflicts when testing your uncertain functionality, or when working together. This is what the git branch is all about: it begins to grow at a specific point in history.

To create a branch, run git branch NAME, switch branches, and run git checkout NAME.

Git checkout-b dev # switch to a new branch named "dev" # Switched to a new branch 'dev'# gitexample git: (dev)

We make some changes in the Hello.txt file and commit the changes:

Echo "\ nHello, Git Branch" > > hello.txt & & git commit-am "Change hello.txt"

Now, switch to the main branch:

Git checkout main & & cat hello.txt# Switched to branch 'main'# Hello, Git

As you can see, the content of the file is still the same as before. To compare branches, we can run.

Git diff dev# diff-- git a/hello.txt b/hello.txt# index 360c923..b7aec52 10064 minutes-a/hello.txt# + b/hello.txt# @-1 Hello 3 + 1 @ @ # Hello, Git#-#-Hello, Git Branch# (END) # type ": Q" to close

We make changes in the main branch:

Echo "\ nHi from Main Branch" > > hello.txt & & git commit-am "Change hello.txt from main" # [main 9b60c4b] Change hello.txt from main# 1 file changed, 2 insertions (+)

Now let's try to combine these changes.

Git merge dev# Auto-merging hello.txt# CONFLICT (content): Merge conflict in hello.txt# Automatic merge failed; fix conflicts and then commit the result.

Because the file was modified twice in the same place, we had a conflict. Look at this file.

Cat hello.txt > dev

There is another command to view the changes individually:

Git diff-ours #: q to close git diff-theirs #: q to close

You can edit the file manually and submit changes, but let's imagine that we only want one version. Let's start by aborting the merger.

Git merge-abort

And restart the merge with the "theirs" policy, which means that in the event of a conflict, we will use what the incoming branch insists on.

Git merge-X theirs dev# Auto-merging hello.txt# Merge made by the 'recursive' strategy.# hello.txt | 5 +-# 1 file changed, 1 insertion (+), 4 deletions (-)

The opposite of this strategy is "ours". Merging these two changes together requires manual editing (or using git mergetool).

View a list of all branch runs

Git branch # type: Q to close# dev# * main

Finally, delete the branch run:

Branches begin to "grow" at some point in git history, and rebase allows you to change this point. Let's create another branch and add some changes to the hello.txt.

Git checkout-b story & & echo "Once upon a time there was a file" > > story.txt & & git add story.txt & & git commit-m "Add story.txt" # Switched to a new branch 'story'# [story eb996b8] Add story.txt# 1 file changed, 1 insertion (+) # create mode 100644 story.txt

Now, let's go back to the main branch and add the changes:

Git checkout main & echo "Other changes" > > changes.txt & & git add changes.txt & & git commit-m "Add changes.txt"

Reset the changes we made in the main to story branch:

Git checkout story & & git rebase main# Successfully rebased and updated refs/heads/story.

You can see that the new file created in the main branch is added to the story branch.

Ls# changes.txt hello.txt story.txt

Note: do not rebase branches that others may have used, such as the main branch. Also, keep in mind that every historical operation on the remote version library needs to force these changes into effect.

Remote repository

If you haven't already, please create a GitHub account, log in and create a new empty repository (private or public).

Assuming that the name of the version library is "example", run the following command (change it to your user name).

Git remote add origin git@github.com:USERNAME/example.git & & git push-u origin main

You can refresh the page and see the files of the main branch. To push all local branches to the remote warehouse, run.

Git push-all origin

We edit something on GitHub: just click on any file or pencil icon. Add any text you want, and then press "submit changes".

Run this command locally to get remote changes. [recommended: Git tutorial]

Git checkout main & & git pull

If you want to save your local changes for later use, you can use git stash.

Echo "Changes" > > hello.txt & & git stash

Now you can use the following command to check, apply, or discard these changes.

Git stash list# stash@ {0}: WIP on main: 92354c8 Update changes.txtgit stash pop # apply changes git stash drop # undo modifications

You can use the stash number, git stash pop 0, to apply a specific repository, or git stash drop 0 to undo.

If you want to discard all local changes, just restore the version library to the last submitted changes, please run.

Git restore.

Manage submitted changes

Once you have created a submission, the change is saved in the local git history. As mentioned earlier, all changes that affect remote history require git push-- force. Keep this in mind in all of the following commands.

Let's start by editing the final submission information.

Git commit-- amend # type: wq to save and close# Press "I" to edit, "Esc" to stop editing

What do you say we reset everything to the beginning?

To find the ID that was submitted for the first time, run this command and scroll (down arrow) to the end.

Git log-- abbrev-commit# commit a07ee27# Author: Your Name Date: Sun Jul 11 11:47:16 2021 + 0200 Adds hello.txt (END) # type ": Q" to close

Run this now to reset the version library, but keep all changes uncached.

Git reset-- soft COMMIT # e.g. A07ee27

In contrast, you can also do a hard reset and use git reset-- hard COMMIT to delete all changes. There are several other ways to reset, as you can see in the git documentation.

Alias

Most of the time, you only need to use a few commands (mainly checkout, add, commit, pull, push, and merge), but some commands may be "just in case".

One way to store this information is git aliases. To configure an alias, simply set it in the configuration. For example, one alias I often use is git tree, which prints out a beautiful history log in the form of a tree.

Git config-- global alias.tree 'log-- graph-- decorate-- pretty=oneline-- abbrev-commit'# Try it with `git tree`

Another useful alias is to delete all merged branches.

Git config-- global alias.clbr'! git branch-- merged | grep-v\ * | xargs git branch-D'

You can see that it is prefixed with "!", which allows us to use any command, not just the git command.

At this point, the study of "commonly used git command arrangement" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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

Internet Technology

Wechat

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

12
Report