本章主要介绍一下代码管理,在最后有常用的git指令,可以档资料收藏一下。
上一篇文章,我写了一份用蜂鸣器来模拟摩斯码的代码,现在需要思考一个问题,如果后续要对它进行不断的修改,扩展不同的版本,那怎样才能保证不会出现版本混乱的情况呢?答案是GitHub。
提示:以下是本篇文章正文内容,下面案例可供参考
GitHub是一个面向开源及私有软件项目的托管平台,因为只支持Git作为唯一的版本库格式进行托管,故名GitHub。我们可以从上面找到很多作者的开源项目,同时,你也可以把自己的代码上传上去开源给小伙伴,大家一起对其进行维护。国内也有和其类似的gitee和coding代码托管平台,其指令基本一致。国内的资源相对较少,毕竟github面向全世界,但是国内相对来说也更安全稳定。2022年github就因为政治因素把俄罗斯地区的用户全给封掉了。不过即使不上传代码,只是在本地应用,git也是一个很不错的工具。
下载方式百度搜索就行,我的电脑已经安装好了就不再画蛇添足了。Github的指令有很多,在此列举一些经常用到的简单应用指令。
代码如下(示例):
ss562@LAPTOP-JI5K6GEM MINGW32 /d/学习/项目实战/莫斯电码机
$ git init
Initialized empty Git repository in D:/瀛︿範/椤圭洰瀹炴垬/鑾柉鐢电爜鏈?.git
可以看到初始化以后会出现一个.git文件,注意以后你要操作该项目,要在和这个.Git同目录下进行git指令操作,否则会出错。
将该目录下所有文件添加到暂定区。当然你也可以git add [文件名]单独添加一个文件。
将暂定区的文件提交到本地仓库。并且可以为该版本写一下注释。
创建一个分支,并且切换到该分支上去。Git branch [分支名]也能创建分支,但创建完成后不会直接切到新分支上去。
列举出本地分支和远程分支。
切换到已经创建好的分支。如图,又切换回了master分支下。
如图,这是蜂鸣器试验中对摩斯码数据的处理,我觉得这个地方写的不太好,这些\0,1,2,3什么的看着不直观,我想改成宏定义。这里分别对app.h新添加宏定义,main.c修改为宏定义。
接着我们需要进行git指令操作。
如图,通过Git add . 添加,然后Git commit -m 添加注释,注释尽量详细一些,这是留给以后的信息,写的时候偷懒,到时候查起来会后悔自己为啥当初没写明白。接着我们用Git log查阅一下。可以看到第二个版本以及注释都在这里显示了。
$ git init
$ git init [project-name]
$ git clone [url]
$ git config --list
$ git config -e [–global]
$ git config [–global] user.name “[name]”
$ git config [–global] user.email “[email address]”
$ git add [file1] [file2] …
$ git add [dir]
$ git add .
$ git add -p
$ git rm [file1] [file2] …
$ git rm --cached [file]
$ git mv [file-original] [file-renamed]
$ git commit -m [message]
$ git commit [file1] [file2] … -m [message]
$ git commit -a
$ git commit -v
$ git commit --amend -m [message]
$ git commit --amend [file1] [file2] …
$ git branch
$ git branch -r
$ git branch -a
$ git branch [branch-name]
$ git checkout -b [branch]
$ git branch [branch] [commit]
$ git branch --track [branch] [remote-branch]
$ git checkout [branch-name]
$ git checkout -
$ git branch --set-upstream [branch] [remote-branch]
$ git merge [branch]
$ git cherry-pick [commit]
$ git branch -d [branch-name]
$ git push origin --delete [branch-name]
$ git branch -dr [remote/branch]
标签
$ git tag
$ git tag [tag]
$ git tag [tag] [commit]
$ git tag -d [tag]
$ git push origin :refs/tags/[tagName]
$ git show [tag]
$ git push [remote] [tag]
$ git push [remote] --tags
$ git checkout -b [branch] [tag]
查看信息
$ git status
$ git log
$ git log --stat
$ git log -S [keyword]
$ git log [tag] HEAD --pretty=format:%s
$ git log [tag] HEAD --grep feature
$ git log --follow [file]
$ git whatchanged [file]
$ git log -p [file]
$ git log -5 --pretty --oneline
$ git shortlog -sn
$ git blame [file]
$ git diff
$ git diff --cached [file]
$ git diff HEAD
$ git diff [first-branch]…[second-branch]
$ git diff --shortstat “@{0 day ago}”
$ git show [commit]
$ git show --name-only [commit]
$ git show [commit]:[filename]
$ git reflog
远程同步
$ git fetch [remote]
$ git remote -v
$ git remote show [remote]
$ git remote add [shortname] [url]
$ git pull [remote] [branch]
$ git push [remote] [branch]
$ git push [remote] --force
$ git push [remote] --all
$ git checkout [file]
$ git checkout [commit] [file]
$ git checkout .
$ git reset [file]
$ git reset --hard
$ git reset [commit]
$ git reset --hard [commit]
$ git reset --keep [commit]
$ git revert [commit]
$ git stash
$ git stash pop
Git 的指令有很多,常用的也就是上面列举的这一些,可以背一下,记不住也没关系,随时百度,或者来这篇博客里查找也行,我们最终的目的是对代码进行好的管理,养成良好的习惯,这样未来不会因为乱七八糟的版本,“屎山”一般的代码而头疼。
上一篇:Java 守护线程