一、git命令常规命令如下
在终端输入git命令,列出可用命令
These are common Git commands used in various situations:
start a working area (see also: git help tutorial)
clone Clone a repository into a new directory
init Create an empty Git repository or reinitialize an existing one
work on the current change (see also: git help everyday)
add Add file contents to the index
mv Move or rename a file, a directory, or a symlink
restore Restore working tree files
rm Remove files from the working tree and from the index
sparse-checkout Initialize and modify the sparse-checkout
examine the history and state (see also: git help revisions)
bisect Use binary search to find the commit that introduced a bug
diff Show changes between commits, commit and working tree, etc
grep Print lines matching a pattern
log Show commit logs
show Show various types of objects
status Show the working tree status
grow, mark and tweak your common history
branch List, create, or delete branches
commit Record changes to the repository
merge Join two or more development histories together
rebase Reapply commits on top of another base tip
reset Reset current HEAD to the specified state
switch Switch branches
tag Create, list, delete or verify a tag object signed with GPG
collaborate (see also: git help workflows)
fetch Download objects and refs from another repository
pull Fetch from and integrate with another repository or a local branch
push Update remote refs along with associated objects
git使用手册,输入如下某些命令,查看具体带参的使用方法,非常重要
'git help -a' and 'git help -g' list available subcommands and some
concept guides. See 'git help <command>' or 'git help <concept>'
to read about a specific subcommand or concept.
See 'git help git' for an overview of the system
Git 的基本语法
git clone #克隆项目到本地仓库
git add # 将工作区的修改提交到暂存区
git commit # 将暂存区的修改提交到当前分支
git reset # 回退到某一个版本
git pull # 从远程更新代码
git push # 将本地代码更新到远程分支上
git reflog # 查看历史命令
git status # 查看当前修改的文件
git status --untracked-files #查看所有新增文件和修改文件
git show # 查看差异log信息
git log # 查看提交历史
git revert # 回退某个修改
git rm --cached :已 add(tracked) 未 commit 的文件,使其回到未 add 状态(untracked)。
git rm -f : 从本地删除已 add 的文件。
git checkout – :有修改的文件回到 tracked 状态,对已 tracked 的文件撤销修改。
git reset HEAD :撤销 commit,回到 modified 状态。
git reset --soft HEAD^:撤销 commit,回到 tracked 状态。
git clean:删除所有 untracked 文件。
git clean -n:演习
git clean -f:删除当前目录下 untracked 文件,除过 .gitignore 里的文件或文件夹
git clean -f :指定路径下
git clean -df:删除当前目录下 untracked 文件和文件夹
git clean -xf:删除当前目录下所有 untracked 文件,不管 .gitignore
git reset --hard && git clean -f 使本地完全回退到上次 commit
开发流程
在公司,会有项目管理,创建好master创库,在gitlab,个人需要创建个人分支,用于提交代码,并申请,合并到分支上(一般会有第一个原始项目,在此基础上,衍生出很多型号,分支代码,用于不同项目),个人可以在master创建自己的分支,管理项目人员,验证没问题,会merge 合并到具体项目的分支上。
远程仓库,本地仓库,工作区之间的联系如下图,远程仓库是公司管理的gitlab,本地仓库,是自己本机建立仓库,管理文件的,ls -a命令可以查看到.git目录,这个就是本地仓库,commit提交代码时,并未提交至网络服务器,还在本机,工作区就是project那个文件夹下,工作编写文件的区域。
系统安装git ,输入指令:sudo apt-get install git
安装完成后,公司,需要配置,注册用户,密码,例如,
git config --global user.name "name"
git config --global user.email "name.com"
在Linux终端,创建新分支,首先新建工作区,目录文件夹,如:xxxx_project,进入这个xxxx_project目录下
输入如下命令
初始化本地仓库 git init //git ini 可以ls -a 看到一个.git文件夹目录,这个不能删掉,这个就是管理记录git的文件。
添加所有项目文件 git add . 或 git add filename
先提交到本地的仓库 git commit -m "你想要提交的备注信息" 注:(不是直接提交到gitlab上)
添加Gitlab远程仓库地址 git remote add origin git@192.168.10.10:youngelace/yes-web.git//git地址是master仓库的地址,主分支地址
创建新的分支 git branch sdk_2202_test
切换到新分支下 git checkout sdk_2202_test
提交到新分支到远程仓库 git push -u origin sdk_2202_test
查看所有分支 git branch -a 或git branch -r
查看提交记录 git status
二、gitlab 拉取代码,创建个人分支
但是一般不会给开发人员权限,直接提交,需要创建个人分支,然后申请合并分支,通过或合并到分支上。
公司会给一个网站,打开公司的gitlab仓库,在主master,点击clone,弹出复制git的网址,获取仓库地址
第一步:在终端新建xxxx_project工程文件夹下,输入 git clone -b sdk_test gitlab仓库master网址(上面clone的网址)
将分支sdk_test 下的代码下拉至xxxx_project本地仓库,git clone 完后,可以ls -a 看到一个.git文件夹目录,这个就是管理记录git的文件。
第二步:在sdk_test 工作区里有.git文件管理器,新建个人分区 git checkout -b sdk_test _laowang,这个时候网页gitlab,可以看到新建的个人分支,
第三步:准备提交修改,增加的问件,git add . ,如果有新增文件,git add xxxxx.c,
第四部:提交修改记录到本地仓库,个人分支上,git commit -m “标签” 如git commit -m “function:update camera json file_20230105”
第五步:推送到远程仓库sdk_test 分支上, git push -u origin sdk_test _laowang
第六步:申请合并分支,将个人分支合并到某个分支,并选中删除个人分支
提交完成后,可以看下工作区,状态,记录,输入git log ,git status,有nothing to commit, working tree clean说明提交完成,干净的
root@ubuntu:/project# git status
On branch chenguanjie/penetration_test
Your branch is up to date with ‘origin/chenguanjie/penetration_test’.
nothing to commit, working tree clean
下图是大概工作流程
三、如何撤销 git add commit
git三种状态:
Git 有三种状态,你的文件可能 处于其中之一: 已提交(committed)、已修改(modified) 和 已暂存(staged)。
• 已修改表示修改了文件,但还没保存到数据库中。
• 已暂存表示对一个已修改文件的当前版本做了标记,使之包含在下次提交的快照中。
• 已提交表示数据已经安全地保存在本地数据库中。
这会让我们的 Git 项目拥有三个阶段:工作区、暂存区以及 Git 目录。
git add 是将工作区已修改的文件提交到暂存区
git commit 是将暂存区的文件提交到Git 目录
二、add回退
1、如果执行git add之后,发现误添加了某个文件提交到了暂存区,可以通过以下命令撤回到工作区:
git reset HEAD <文件名>
2、如果想将所有暂存区的文件撤回到工作区:
git reset HEAD
三、commit回退
1、如果执行git commit之后,因为某种原因想撤销提交但仍然保留commit之前的修改,可以通过以下命令撤销提交:
将暂存区最近一次提交到Git目录的文件全部撤回到暂存区
git reset --soft HEAD^
2、如果想将git commi