龙空技术网

盘点一下Git常用的命令,简历上可以写上了解Git

南方葵籽 232

前言:

此时小伙伴们对“centos65基本命令大全”大概比较珍视,兄弟们都需要分析一些“centos65基本命令大全”的相关资讯。那么小编在网上收集了一些有关“centos65基本命令大全””的相关知识,希望咱们能喜欢,小伙伴们一起来了解一下吧!

前言:

作为研发岗位的工作者,未来的工作中基本都会接触到代码的版本管理和代码的更新迭代。

那么Git大家一定是需要了解的,每个公司可能都会在对应的代码库里进行代码的更新和产品的更新迭代。

今天主要给大家分享一些常用的Git命令。

除Git之外,大家可能还听说过SVN等版本管理工具,目前来看用Git的公司比较多。

下面以Linux系统为例,window也可以直接下载(这里不做叙述)

Git相关概念

Git区域

workspace:工作区staging area:暂存区/缓存区local repository:版本库或本地仓库remote repository:远程仓库

文件的状态

已提交状态(committed),表示数据已经安全的保存在本地数据库中;已修改状态(modified),表示修改了文件,但是还没有保存到数据库中;已暂存状态(staged),表示对一个已修改文件的当前版本做了标记,使之包含在下次提交的快照中。安装Git

常用的Linux系统一般️Ubuntu、Centos或者Redhat。

# Ubuntu系统# 下载Gitapt-get install git # 查看Git版本git --version# Centos/Redhat 系统yum -y install git-coregit --version

Ubuntu下的安装示意图

Git常用命令

git clone ssh/https # 从远端拉取代码git init # 初始化Git仓git add 文件/文件名 # 将工作区的文件添加到stage缓存区git status # 查看仓库的当前状态git log # 查看历史提交记录git diff # 比较文件的不同,暂存区和工作区的差异git commit -m 'message' # 将stage区的文件提交到本地仓库git reset # 回退git branch 分支名字# 新建branch分支git checkout -b 新分支名字 # 与git branch的区别是该方法可以跳转到新建的分支git merge # 合并分支git push origin 分支名 # 将本地仓库的文件推送到远端github、gitee等

下面小白通过具体的例子给大家演示一下

$ root@daimajiedexiaobai:~# mkdir Gitdemo$ root@daimajiedexiaobai:~# cd Gitdemo$ root@daimajiedexiaobai:~/Gitdemo# git clone git@gitee.com:code-to-xiaobai/gitdemo.gitCloning into 'gitdemo'...remote: Enumerating objects: 4, done.remote: Counting objects: 100% (4/4), done.remote: Compressing objects: 100% (4/4), done.remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0Receiving objects: 100% (4/4), done.$ root@daimajiedexiaobai:~/Gitdemo# lsgitdemo$ root@daimajiedexiaobai:~/Gitdemo# cd gitdemo$ root@daimajiedexiaobai:~/Gitdemo/gitdemo# echo "代码界的小白" >> a.text$ root@daimajiedexiaobai:~/Gitdemo/gitdemo# cat a.text代码界的小白$ root@daimajiedexiaobai:~/Gitdemo/gitdemo# git add a.text$ root@daimajiedexiaobai:~/Gitdemo/gitdemo# git statusOn branch masterYour branch is up to date with 'origin/master'.Changes to be committed:  (use "git restore --staged <file>..." to unstage)root@daimajiedexiaobai:~/Gitdemo/gitdemo# git commit -m "This is first commit by 代码界的小白" [master 64ce096] This is first commit by 代码界的小白 1 file changed, 1 insertion(+) create mode 100644 a.textgit commit -m "This is first commit by 代码界的小白"root@daimajiedexiaobai:~/Gitdemoroot@daimajiedexiaobai:~/Gitdemo/gitdemo# git statusOn branch masterYour branch is ahead of 'origin/master' by 1 commit.  (use "git push" to publish your local commits)nothing to commit, working tree clean$ root@daimajiedexiaobai:~/Gitdemo/gitdemo# git logcommit 64ce096391032901526cb1a484982300791ee009 (HEAD -> master)Author: 小白 <zz123456wzq@163.com>Date:   Sat Aug 6 18:59:29 2022 +0800    This is first commit by 代码界的小白commit 36e2eef6ca2636bfda1efca16ceb0b57c65c0e4a (origin/master, origin/HEAD)Author: 小白 <zz123456wzq@163.com>Date:   Sat Aug 6 10:53:16 2022 +0000    Initial commit$ root@daimajiedexiaobai:~/Gitdemo/gitdemo# mkdir demo$ root@daimajiedexiaobai:~/Gitdemo/gitdemo# cd demo$ root@daimajiedexiaobai:~/Gitdemo/gitdemo/demo# echo "祝福大家顺利上岸" >> b.text$ root@daimajiedexiaobai:~/Gitdemo/gitdemo/demo# cat b.text祝福大家顺利上岸$ root@daimajiedexiaobai:~/Gitdemo/gitdemo/demo# git add b.text$ root@daimajiedexiaobai:~/Gitdemo/gitdemo/demo# git commit -m 'This is second Commit by 代码界的小白' [master a131dcc] This is second Commit by 代码界的小白 1 file changed, 1 insertion(+) create mode 100644 demo/b.text$ root@daimajiedexiaobai:~/Gitdemo/gitdemo/demo# git logcommit a131dcceda9827efbfd2dbeaca939a399f5f9427 (HEAD -> master)Author: 小白 <zz123456wzq@163.com>Date:   Sat Aug 6 19:07:05 2022 +0800    This is second Commit by 代码界的小白commit 64ce096391032901526cb1a484982300791ee009Author: 小白 <zz123456wzq@163.com>Date:   Sat Aug 6 18:59:29 2022 +0800    This is first commit by 代码界的小白commit 36e2eef6ca2636bfda1efca16ceb0b57c65c0e4a (origin/master, origin/HEAD)Author: 小白 <zz123456wzq@163.com>Date:   Sat Aug 6 10:53:16 2022 +0000    Initial commit$ root@daimajiedexiaobai:~/Gitdemo/gitdemo/demo# git push origin master Enumerating objects: 8, done.Counting objects: 100% (8/8), done.Compressing objects: 100% (4/4), done.Writing objects: 100% (7/7), 683 bytes | 683.00 KiB/s, done.Total 7 (delta 1), reused 0 (delta 0), pack-reused 0remote: Powered by GITEE.COM [GNK-6.4]To gitee.com:code-to-xiaobai/gitdemo.git   36e2eef..a131dcc  master -> master$ root@daimajiedexiaobai:~/Gitdemo/gitdemo# git checkout  -b newbranch  # 新拉一个分支Switched to a new branch 'newbranch'$ root@daimajiedexiaobai:~/Gitdemo/gitdemo# git branck -agit: 'branck' is not a git command. See 'git --help'.The most similar command is branch$ root@daimajiedexiaobai:~/Gitdemo/gitdemo# git branch -a  master # 查看都有哪些分支 分支前带*表示当前位于此分支* newbranch  remotes/origin/HEAD -> origin/master  remotes/origin/master$ root@daimajiedexiaobai:~/Gitdemo/gitdemo# echo "祝所有朋友顺利签约" >> c.text$ root@daimajiedexiaobai:~/Gitdemo/gitdemo# cat c.text祝所有朋友顺利签约$ root@daimajiedexiaobai:~/Gitdemo/gitdemo# git add c.text$ root@daimajiedexiaobai:~/Gitdemo/gitdemo# git statusOn branch newbranchChanges to be committed:  (use "git restore --staged <file>..." to unstage) new file:   c.text$ root@daimajiedexiaobai:~/Gitdemo/gitdemo# git logcommit a131dcceda9827efbfd2dbeaca939a399f5f9427 (HEAD -> newbranch, origin/master, origin/HEAD, master)Author: 小白 <zz123456wzq@163.com>Date:   Sat Aug 6 19:07:05 2022 +0800    This is second Commit by 代码界的小白commit 64ce096391032901526cb1a484982300791ee009Author: 小白 <zz123456wzq@163.com>Date:   Sat Aug 6 18:59:29 2022 +0800    This is first commit by 代码界的小白commit 36e2eef6ca2636bfda1efca16ceb0b57c65c0e4aAuthor: 小白 <zz123456wzq@163.com>Date:   Sat Aug 6 10:53:16 2022 +0000    Initial commit$ root@daimajiedexiaobai:~/Gitdemo/gitdemo# git commit -m "This is third Commit by 代码界的小白"[newbranch 9221245] This is third Commit by 代码界的小白 1 file changed, 1 insertion(+) create mode 100644 c.text$ root@daimajiedexiaobai:~/Gitdemo/gitdemo# git logcommit 922124515e8de6ea5ca0a18e8544ef4a467bc39b (HEAD -> newbranch)Author: 小白 <zz123456wzq@163.com>Date:   Sat Aug 6 19:12:54 2022 +0800    This is third Commit by 代码界的小白commit a131dcceda9827efbfd2dbeaca939a399f5f9427 (origin/master, origin/HEAD, master)Author: 小白 <zz123456wzq@163.com>Date:   Sat Aug 6 19:07:05 2022 +0800    This is second Commit by 代码界的小白commit 64ce096391032901526cb1a484982300791ee009Author: 小白 <zz123456wzq@163.com>Date:   Sat Aug 6 18:59:29 2022 +0800    This is first commit by 代码界的小白commit 36e2eef6ca2636bfda1efca16ceb0b57c65c0e4aAuthor: 小白 <zz123456wzq@163.com>Date:   Sat Aug 6 10:53:16 2022 +0000    Initial commit$ root@daimajiedexiaobai:~/Gitdemo/gitdemo# git push orgin newbranchfatal: 'orgin' does not appear to be a git repositoryfatal: Could not read from remote repository.Please make sure you have the correct access rightsand the repository exists.$ root@daimajiedexiaobai:~/Gitdemo/gitdemo# git push origin newbranchEnumerating objects: 4, done.Counting objects: 100% (4/4), done.Compressing objects: 100% (2/2), done.Writing objects: 100% (3/3), 346 bytes | 346.00 KiB/s, done.Total 3 (delta 1), reused 0 (delta 0), pack-reused 0remote: Powered by GITEE.COM [GNK-6.4]remote: Create a pull request for 'newbranch' on Gitee by visiting:remote:      gitee.com:code-to-xiaobai/gitdemo.git * [new branch]      newbranch -> newbranch$ root@daimajiedexiaobai:~/Gitdemo/gitdemo# vim a.text$ root@daimajiedexiaobai:~/Gitdemo/gitdemo# git checkout master # 转换到主分支 M a.textSwitched to branch 'master'Your branch is up to date with 'origin/master'.$ root@daimajiedexiaobai:~/Gitdemo/gitdemo# git branch -a * master  newbranch  remotes/origin/HEAD -> origin/master  remotes/origin/master  remotes/origin/newbranch$ root@daimajiedexiaobai:~/Gitdemo/gitdemo# git merger newbranch mastergit: 'merger' is not a git command. See 'git --help'.The most similar command is merge$ root@daimajiedexiaobai:~/Gitdemo/gitdemo# git merge newbranch masterUpdating a131dcc..9221245Fast-forward c.text | 1 + 1 file changed, 1 insertion(+) create mode 100644 c.text$ root@daimajiedexiaobai:~/Gitdemo/gitdemo# git merge newbranch masterAlready up to date.$ root@daimajiedexiaobai:~/Gitdemo/gitdemo# git branch newbranchfatal: A branch named 'newbranch' already exists.$ root@daimajiedexiaobai:~/Gitdemo/gitdemo# git checkout newbranchM a.textSwitched to branch 'newbranch'$ root@daimajiedexiaobai:~/Gitdemo/gitdemo# echo "代码界的小白 test" >> d.md$ root@daimajiedexiaobai:~/Gitdemo/gitdemo# cd d.md-bash: cd: d.md: Not a directory$ root@daimajiedexiaobai:~/Gitdemo/gitdemo# cat d.md代码界的小白 test$ root@daimajiedexiaobai:~/Gitdemo/gitdemo# git add d.md$ root@daimajiedexiaobai:~/Gitdemo/gitdemo# git statusOn branch newbranchChanges to be committed:  (use "git restore --staged <file>..." to unstage) new file:   d.mdChanges not staged for commit:  (use "git add <file>..." to update what will be committed)  (use "git restore <file>..." to discard changes in working directory) modified:   a.text$ root@daimajiedexiaobai:~/Gitdemo/gitdemo# git commit -m 'Commit by 代码界的小白'[newbranch 388548f] Commit by 代码界的小白 1 file changed, 1 insertion(+) create mode 100644 d.md$ root@daimajiedexiaobai:~/Gitdemo/gitdemo# git logcommit 388548fde08827ec3f3447288007edb64d55c4da (HEAD -> newbranch)Author: 小白 <zz123456wzq@163.com>Date:   Sat Aug 6 19:23:00 2022 +0800    Commit by 代码界的小白commit 922124515e8de6ea5ca0a18e8544ef4a467bc39b (origin/newbranch, master)Author: 小白 <zz123456wzq@163.com>Date:   Sat Aug 6 19:12:54 2022 +0800    This is third Commit by 代码界的小白:commit 388548fde08827ec3f3447288007edb64d55c4da (HEAD -> newbranch)Author: 小白 <zz123456wzq@163.com>Date:   Sat Aug 6 19:23:00 2022 +0800    Commit by 代码界的小白commit 922124515e8de6ea5ca0a18e8544ef4a467bc39b (origin/newbranch, master)Author: 小白 <zz123456wzq@163.com>Date:   Sat Aug 6 19:12:54 2022 +0800    This is third Commit by 代码界的小白:...skipping...commit 388548fde08827ec3f3447288007edb64d55c4da (HEAD -> newbranch)Author: 小白 <zz123456wzq@163.com>Date:   Sat Aug 6 19:23:00 2022 +0800    Commit by 代码界的小白commit 922124515e8de6ea5ca0a18e8544ef4a467bc39b (origin/newbranch, master)Author: 小白 <zz123456wzq@163.com>Date:   Sat Aug 6 19:12:54 2022 +0800    This is third Commit by 代码界的小白commit a131dcceda9827efbfd2dbeaca939a399f5f9427 (origin/master, origin/HEAD)Author: 小白 <zz123456wzq@163.com>Date:   Sat Aug 6 19:07:05 2022 +0800    This is second Commit by 代码界的小白commit 64ce096391032901526cb1a484982300791ee009Author: 小白 <zz123456wzq@163.com>Date:   Sat Aug 6 18:59:29 2022 +0800    This is first commit by 代码界的小白commit 36e2eef6ca2636bfda1efca16ceb0b57c65c0e4aAuthor: 小白 <zz123456wzq@163.com>Date:   Sat Aug 6 10:53:16 2022 +0000commit 388548fde08827ec3f3447288007edb64d55c4da (HEAD -> newbranch)Author: 小白 <zz123456wzq@163.com>Date:   Sat Aug 6 19:23:00 2022 +0800    Commit by 代码界的小白commit 922124515e8de6ea5ca0a18e8544ef4a467bc39b (origin/newbranch, master)Author: 小白 <zz123456wzq@163.com>Date:   Sat Aug 6 19:12:54 2022 +0800    This is third Commit by 代码界的小白commit a131dcceda9827efbfd2dbeaca939a399f5f9427 (origin/master, origin/HEAD)Author: 小白 <zz123456wzq@163.com>Date:   Sat Aug 6 19:07:05 2022 +0800    This is second Commit by 代码界的小白commit 64ce096391032901526cb1a484982300791ee009Author: 小白 <zz123456wzq@163.com>Date:   Sat Aug 6 18:59:29 2022 +0800    This is first commit by 代码界的小白commit 36e2eef6ca2636bfda1efca16ceb0b57c65c0e4aAuthor: 小白 <zz123456wzq@163.com>Date:   Sat Aug 6 10:53:16 2022 +0000:[1]+  Stopped                 git log^Z$ root@daimajiedexiaobai:~/Gitdemo/gitdemo# git push origin newbranch # 提交到远端newbranch分支喜爱Enumerating objects: 4, done.Counting objects: 100% (4/4), done.Compressing objects: 100% (2/2), done.Writing objects: 100% (3/3), 316 bytes | 316.00 KiB/s, done.Total 3 (delta 1), reused 0 (delta 0), pack-reused 0remote: Powered by GITEE.COM [GNK-6.4]To gitee.com:code-to-xiaobai/gitdemo.git   9221245..388548f  newbranch -> newbranch$ root@daimajiedexiaobai:~/Gitdemo/gitdemo# git merge newbranch masterAlready up to date.$ root@daimajiedexiaobai:~/Gitdemo/gitdemo# git branch -a  master* newbranch  remotes/origin/HEAD -> origin/master  remotes/origin/master  remotes/origin/newbranch$ root@daimajiedexiaobai:~/Gitdemo/gitdemo# git checkout masterM a.textSwitched to branch 'master'Your branch is ahead of 'origin/master' by 1 commit.  (use "git push" to publish your local commits)$ root@daimajiedexiaobai:~/Gitdemo/gitdemo# git push origin masterTotal 0 (delta 0), reused 0 (delta 0), pack-reused 0remote: Powered by GITEE.COM [GNK-6.4]To gitee.com:code-to-xiaobai/gitdemo.git   a131dcc..9221245  master -> master$ root@daimajiedexiaobai:~/Gitdemo/gitdemo# git merge newbranch masterUpdating 9221245..388548fFast-forward d.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 d.md$ root@daimajiedexiaobai:~/Gitdemo/gitdemo# 

下面是Gitee的变化图

push之前

push之后

拉取新分支newbranch

总结

总的来说,Git的主要命令不太多,大家可以尝试自己去操作一下。

本文是基于Linux去实现的,window也可以去操作。

大家可以熟悉一下Git常用的命令,一些常见的概念,如果可以写到简历上,也算是一个不错的技术栈。

原文链接:

标签: #centos65基本命令大全