


Take you step by step through the most commonly used git commands in 10 minutes
Essentially, Git can record changes to text, but its definition is a version control system. Chances are you've already used git in one way or another: due to its distributed nature, it's the de facto standard for code version control, as opposed to the centralized Apache Subversion (SVN).
Install git
To check if Git is installed, run in the terminal:
$ git version git version 2.27.0.rc1.windows.1
If it is not installed, follow https://git- Instructions at scm.com/downloads. Mac users can install it using brew: brew install git.
Configure git
We only need to configure a few things
git config --global user.name "前端小智" && # 你的名字 git config --global user.email johndoe@example.com && # 你的邮箱 git config --global init.defaultbranch main # 默认分支名称,与GitHub兼容
You can use the following command to view the current global configuration
git config --global --list # Type ":q" to close
git Store 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 the command suggests, removing --global will expand the scope of these commands to the current folder. But to test this we need a repository.
Create New Repository
A repository is just a folder that holds everything we want to track. Created by command:
mkdir gitexample && cd gitexample && git init # gitexample git:(main)
This command creates a .git folder within the gitexample folder. This hidden .git folder is the repository: all local configurations and modifications are stored here.
Changes
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 <file>..." to include in what will be committed) # hello.txt # # nothing added to commit but untracked files present (use "git add" to track)
According to the prompt suggestions, we add files:
git add .
If we don’t want all files to be added, we can use
git add hello.txt
If you check the status of the repository now, you will I see that the file has been added (also called staged), but it has not been submitted yet.
git status # On branch main # # No commits yet # # Changes to be committed: # (use "git rm --cached <file>..." to unstage) # new file: hello.txt
To record these changes, let's commit 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
Check commit records:
git log # Author: qq449245884 <44924566884@qq.com> # Date: Sat Jul 17 14:57:24 2021 +0800 # # Add hello.txt #
Create a branch
In many cases it is useful to have an independent version of the initial code: for example, Avoid code conflicts when testing functionality you're not sure about, or when working together. That's exactly what a git branch is: it grows from a specific point in history.
To create a branch, run git branch NAME. To switch branches, run git checkout NAME. Or simply
git checkout -b dev # 切换到一个名为“dev”的新分支 # Switched to a new branch 'dev' # gitexample git:(dev)
We change something 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 master branch:
git checkout main && cat hello.txt # Switched to branch 'main' # Hello, Git
As you can see , the file content remains the same as before. To compare branches we can run.
git diff dev # diff --git a/hello.txt b/hello.txt # index 360c923..b7aec52 100644 # --- a/hello.txt # +++ b/hello.txt # @@ -1,3 +1 @@ # Hello, Git # - # -Hello, Git Branch # (END) # type ":q" to close
We make changes in the master 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 merge 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. Take a look at this file
cat hello.txt <<<<<<< HEAD Hello, Git Hi from Main Branch ======= Hello, Git >>>>>>> dev
There is also a command to view the changes individually:
git diff --ours # :q to close git diff --theirs #:q to close
You can manually edit the file and commit the changes, but let's imagine that we only want one version of it. Let's start by aborting the merge.
git merge --abort
And restart the merge with the "theirs" strategy, which means in the event of a conflict, we will use what the incoming branch is holding on to.
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, to delete the branch run:
git branch -d dev # Deleted branch dev (was 6259828).
Reset branch
Branch from git A certain point in history begins to "grow", and rebase allows this point to be changed. Let's create another branch and add some changes to 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, we 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 in the main New files created by the branch are 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, remember that every historical operation on the remote repository needs to force these modifications to take effect.
Remote Repository
If you don't have one yet, create a GitHub account, log in and create a new empty repository (private or public).
Assuming the name of the repository is "example", run the following command (change it to your username).
git remote add origin git@github.com:USERNAME/example.git && git push -u origin main
You can refresh the page and see the files in the main branch. To push all local branches to the remote repository, run.
git push --all origin
We edit something on GitHub: just click on any file and the pencil icon. Add a line of any text you want and press "Submit Changes."
Run this command locally to get remote changes. [Recommended: Git Tutorial]
git checkout main && git pull
Manage uncommitted changes
If you want to save your local modifications for later use, you can Use git stash.
echo "Changes" >> hello.txt && git stash
Now you can use the following commands to check, apply or discard these changes.
git stash list # stash@{0}: WIP on main: 92354c8 Update changes.txt git stash pop # 应用更改 git stash drop # 撤销修改
你可以使用 stash 编号,即git stash pop 0来应用一个特定的储藏库,或者git stash drop 0来撤销。
如果你想放弃所有的本地修改,只需恢复版本库到最后提交的修改,请运行。
git restore .
管理提交的更改
一旦你创建了一个提交,这个变化就会保存在本地的git历史中。如前所述,所有影响远程历史的修改都需要git push --force。以下所有命令都要记住这一点。
我们从编辑最后的提交信息开始。
git commit --amend # type :wq to save and close # Press "i" to edit, "Esc" to stop editing
我们把一切重设到最开始怎么样?
要找到第一次提交的ID,请运行这个命令并滚动(向下箭头)到最后。
git log --abbrev-commit # commit a07ee27 # Author: Your Name <your@email.address> Date: Sun Jul 11 11:47:16 2021 +0200 Adds hello.txt (END) # type ":q" to close
现在运行这个来重置版本库,但保持所有的修改不被缓存。
git reset --soft COMMIT # e.g. a07ee27
与之相反,你也可以进行硬重置,用git reset --hard COMMIT来删除所有修改。还有几种其他的重置方式,你可以从git文档中了解到。
别名
大多数时候,你只需要使用少数几个命令(主要是checkout、add、commit、pull、push和merge),但有些命令可能是你想要“以防万一”的。
存储这些信息的一种方法是git aliases。要配置一个别名,只需在配置中设置它。例如,我经常使用的一个别名是git tree,它以树的形式打印出一个漂亮的历史日志。
git config --global alias.tree 'log --graph --decorate --pretty=oneline --abbrev-commit' # Try it with `git tree`
另一个有用的别名是删除所有合并的分支。
git config --global alias.clbr '!git branch --merged | grep -v \* | xargs git branch -D'
你可以看到它的前缀是"!",这允许我们使用任何命令,而不仅仅是git命令。
~完,我是刷碗智,今天礼拜六写的,要准备去刷碗了,骨的白!
▎作者:Valeria 译者:前端小智 来源:dev 原文:https://dev.to/valeriavg/master-git-in-7-minutes-gai
The above is the detailed content of Take you step by step through the most commonly used git commands in 10 minutes. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Git is a version control system, and GitHub is a Git-based code hosting platform. Git is used to manage code versions and supports local operations; GitHub provides online collaboration tools such as Issue tracking and PullRequest.

Git and GitHub are not the same thing. Git is a version control system, and GitHub is a Git-based code hosting platform. Git is used to manage code versions, and GitHub provides an online collaboration environment.

GitHub is not difficult to learn. 1) Master the basic knowledge: GitHub is a Git-based version control system that helps track code changes and collaborative development. 2) Understand core functions: Version control records each submission, supporting local work and remote synchronization. 3) Learn how to use: from creating a repository to push commits, to using branches and pull requests. 4) Solve common problems: such as merge conflicts and forgetting to add files. 5) Optimization practice: Use meaningful submission messages, clean up branches, and manage tasks using the project board. Through practice and community communication, GitHub’s learning curve is not steep.

On your resume, you should choose to write Git or GitHub based on your position requirements and personal experience. 1. If the position requires Git skills, highlight Git. 2. If the position values community participation, show GitHub. 3. Make sure to describe the usage experience and project cases in detail and end with a complete sentence.

Microsoft does not own Git, but owns GitHub. 1.Git is a distributed version control system created by Linus Torvaz in 2005. 2. GitHub is an online code hosting platform based on Git. It was founded in 2008 and acquired by Microsoft in 2018.

The reason for using GitHub to manage HTML projects is that it provides a platform for version control, collaborative development and presentation of works. The specific steps include: 1. Create and initialize the Git repository, 2. Add and submit HTML files, 3. Push to GitHub, 4. Use GitHubPages to deploy web pages, 5. Use GitHubActions to automate building and deployment. In addition, GitHub also supports code review, Issue and PullRequest features to help optimize and collaborate on HTML projects.

Starting from Git is more suitable for a deep understanding of version control principles, and starting from GitHub is more suitable for focusing on collaboration and code hosting. 1.Git is a distributed version control system that helps manage code version history. 2. GitHub is an online platform based on Git, providing code hosting and collaboration capabilities.

Git is an open source distributed version control system that helps developers track file changes, work together and manage code versions. Its core functions include: 1) record code modifications, 2) fallback to previous versions, 3) collaborative development, and 4) create and manage branches for parallel development.
