关于javascript:7分钟用事例带你掌握工作常用的-git-命令

15次阅读

共计 5301 个字符,预计需要花费 14 分钟才能阅读完成。

作者:Valeria
译者:前端小智
起源:medium

从实质上讲,Git 能够记录文本的变动,但其定义是一个版本控制系统。你有可能曾经以这种或那种形式应用了 git:因为它的分布式性质,它是代码版本控制的事实标准,与集中式的 Apache Subversion(SVN)绝对。

装置 git

要查看是否装置了 Git,在终端运行:

$ git version
git version 2.27.0.rc1.windows.1

如果没有装置,请依照 https://git-scm.com/downloads 上的阐明。
Mac 用户能够用 brew 来装置它:brew install git

配置 git

咱们只须要配置一些货色

git config --global user.name "前端小智" && # 你的名字
git config --global user.email johndoe@example.com && # 你的邮箱
git config --global init.defaultbranch main # 默认分支名称,与 GitHub 兼容

能够用上面命令查看以后的全局配置

git config --global --list
# Type ":q" to close

git 在纯文本中存储配置,如果你想间接批改,能够间接在 ~/.gitconfig~/.config/git/config中编辑全局配置。

正如命令所倡议的那样,去掉 --global 会使这些命令的适用范围扩充到以后文件夹。但要测试这一点,咱们须要一个存储库。

创立新存储库

存储库只是一个文件夹,外面有咱们想跟踪的所有货色。通过命令创立:

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

这个命令在 gitexample 文件夹内创立了一个 .git 文件夹。这个暗藏的 .git 文件夹就是版本库:所有的本地配置和批改都存储在这里。

扭转

在存储库中创立一些货色:

echo "Hello, Git" >> hello.txt

运行git status,咱们会看到新创建的未被追踪的文件。

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)

依据提醒倡议,咱们增加文件:

git addd . 

如果咱们不想要所有文件提增加能够应用

git add hello.txt 

如果你当初查看版本库的状态,你会看到文件曾经被增加了(又称staged),但还没有提交。

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

为了记录这些变动,咱们来提交它。

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 <MESSAGE> 是一个简短的命令,你能够用 git commit 关上编辑器(次要是 vim),提供具体的提交形容。

查看提交记录:

git log

# Author: qq449245884 <44924566884@qq.com>
# Date:   Sat Jul 17 14:57:24 2021 +0800
#
#    Add hello.txt
#

创立分支

在很多状况下,领有一个独立的初始代码版本是很有用的:例如,在测试你不确定的性能时,或者在一起工作时防止代码抵触。这正是 git 分支的意义所在:它从历史上的一个特定点开始成长。

要创立分支,运行git branch NAME,要切换分支,运行git checkout NAME。或者简略地

git checkout -b dev # 切换到一个名为“dev”的新分支
# Switched to a new branch 'dev'
# gitexample git:(dev)

咱们在 Hello.txt 文件中更改一些内容并提交更改:

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

当初,切换到主分支:

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

正如你所看到的,文件内容依然和原来一样。为了比拟分支,咱们能够运行。

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

咱们在主分支中进行更改:

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(+)

当初让咱们试着把这些变动合并起来。

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

因为文件在同一个中央被批改了两次,咱们就产生了抵触。看看这个文件

cat hello.txt
<<<<<<< HEAD
Hello, Git

Hi from Main Branch
=======
Hello, Git
>>>>>>> dev

还有一个命令能够独自查看更改:

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

你能够手动编辑文件并提交批改,但咱们构想一下,咱们只想要其中一个版本。咱们就从停止合并开始。

git merge --abort

并以 “theirs” 策略重新启动合并,这意味着在发生冲突时,咱们将应用传入的分支所保持的货色。

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(-)

与此策略相同的是 “ours”。将这两个改变合并在一起,须要手动编辑(或应用git mergetool)。

查看所有分支运行的列表

git branch # type :q to close
#  dev
# * main

最初,删除分支运行:

git branch -d dev
# Deleted branch dev (was 6259828).

重置分支

分支从 git 历史中的某一点开始 “ 成长 (grow)”,rebase 容许扭转这个点。咱们再创立一个分支,并在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

当初,咱们回到主分支并增加更改:

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

重置咱们在 mainstory分支所做的更改:

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

能够看到在主分支创立的新文件被增加到story 分支。

ls
# changes.txt hello.txt   story.txt

留神:不要rebase 他人可能应用过的分支,例如主分支。另外,请记住,在近程版本库上进行的每一次历史操作都须要强制这些批改失效。

近程存储库

如果你还没有,请创立一个 GitHub 账户,登录并创立一个新的空仓库(公有或公共)。

假如版本库的名字是 “example“,运行以下命令(改成你的用户名)。

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

你能够刷新页面,看到主分支的文件。要把所有本地分支推送到近程仓库,请运行。

git push --all origin

咱们在 GitHub 上编辑一些货色:只有点击任何文件和铅笔图标。增加一行你想要的任何文字,而后按 “ 提交批改 ”。

在本地运行这个命令,以取得近程的变动。

git checkout main &&
git pull

治理未提交的更改

如果你想保留你的本地批改以便当前应用,你能够应用git stash

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

当初你能够应用以下命令来查看、利用或放弃这些变动。

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 命令。

~ 完,我是刷碗智,明天礼拜六写的,要筹备去刷碗了,骨的白!


原文:https://dev.to/valeriavg/mast…

交换

文章每周继续更新,能够微信搜寻「大迁世界」第一工夫浏览和催更(比博客早一到两篇哟),本文 GitHub https://github.com/qq449245884/xiaozhi 曾经收录,整顿了很多我的文档,欢送 Star 和欠缺,大家面试能够参照考点温习,另外关注公众号,后盾回复 福利,即可看到福利,你懂的。

正文完
 0