Git 用法总结

本文总结了Git在实际项目开发中的应用技巧,包括如何创建及配置Git仓库、分支管理、提交代码、生成和应用补丁等关键操作流程。适用于团队协作和日常开发场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


 从应用实战的角度对Git的用法做个小结,这里并不涉及git的工作原理:

1. Git Repo/Branch的创建方法

  1). 直接从server段Copy一个repo到本地,作为开发的code base

       git clone https://github.com/***.git

 2). 在服务器端创建一个新的repo,方便多人合作开发

   step1 . server端搭建http服务器:/var/www/html (或者更加复杂的gitlab)
   step2 . 创建空的repo:  cd /var/www/html/; git init --bare testRepo.git
   step3. 添加第一个patch: git add README.txt;  git commit -m "first commit"
  
 3) 把local branch推送到 remote server: 
    step1.将server的project git作为remote的source,命名为origin
              git remote add origin_1 https://github.com/***.git
    step2. 将当前repo的branch推送到server的repo中去,
            git push origin_1 master; origin_1表示remote server的地址,master表示remote server上的master branch。此处省略了本地local 分支的名称

2. Git repo基本配置:

       git config --golbal user.name "your name"
       git config --global user.email "your email"
       git config --global core.editor vim              //默认编辑器
       git config --global commit.template $HOME/.gitmessage.txt。//commit message 默认格式,例如 signed-off-by: Li, Bala <Bala.a.li@intel.com>
       git config --global color.ui true    //默认颜色
       有些信息在生成commit的过程中会用到,可以把这些写到脚本里面,机器启动后自行加载

3. Branch常见操作:

   1). 查看Local已有的branch
         git branch;   如果想看remote branch,加上 -r参数;  同步remote: git remote update
   2). 创建一个新Local branch: 
         git branch new_branch, 从当前工作branch上复制出一个新的branch,命名为new_branch。
   3). 切换到另外一个Local branch
         git checkout branch_name
   4). 创建新的branch 去跟踪remote server branch, 方便以后patch pull和push
        git checkout -b local_branch_name origin/remote_branch_name  
   5). 强制用本地分支覆盖远程分支(或者提交patch,不用-f)。
        git push origin 本地分支名:远程分支名 -f  
   6). 删除远程分支
        git push origin :远程分支                  

4. 提交代码(日常开发步骤如下)

  1. git fetch
  2. git rebase
  3. 解决冲突
  4. git add 冲突文件
  5. git rebase –continue
  6. git push

5. 单个Patch操作

 1). 生成commit过程
       git add file.c (如果要去掉行尾换行,请在vim文件时候执行:% s/\s\+$//g)
       git commit -s -m //编写commit 信息, -s表示自动签名, -m后面是commit message
 2).  修改commit message
         git commit --amend
 3). 保存patch
       git format-patch -i //i表示要dump的patch 个数

       如果只是简单临时保存和打回patch只需用:
          git diff &> diff.patch
          patch -p1 < diff.patch
 4). 应用patch
       git am ***.patch 
      如果打patch失败了,继续执行下面步骤:
         git apply ***.patch --reject
         这个过程会产生.rej文件,按照该文件去修改patch没打上的文件
         edit files
         git add file.c

         git am --resolved


6. 修改做好的提交的patch

  1.    git rebase  -i comment_id

  2.    edit

  3.    git rebase --resolved

  4.    git rebase --continue

7. 查看patch log

  1.  git log 查看这个项目的log纪录

  2.  git log --pretty=oneline file.c //查看和某文件相关的所有log信息,每个只显示一行

  3.  git blame file.c//显示文件的没一行的最新的log

     后面两操作常用于追踪文件的修改过程

8. 发送patch mail

       git format-patch --subject-prefix="version **" -1
       //把patch发送到mailist, 所有订阅该mailist都会受到这个patch,方便patch review

      git send-email  --smtp-server=smtp.intel.com --to=libva@lists.freedesktop.org  *.patch

9. 查看已经添加但尚未commit的内容

   git diff --cached

10. 查看git历史操作记录

         git reflog 可以查看所有分支的所有操作记录(包括(包括commit和reset的操作),包括已经被删除的commit记录,git log则不能察看已经删除了的commit记录

具体一个例子,假设有三个commit, git st:

commit3: add test3.c

commit2: add test2.c

commit1: add test1.c

如果执行git reset --hard HEAD~1则 删除了commit3,如果发现删除错误了,需要恢复commit3,这个时候就要使用git reflog

 HEAD@{0}: HEAD~1: updating HEAD

63ee781 HEAD@{1}: commit: test3:q

红色加粗的即是被删除了的 commit3,运行git log则没有这一行记录

可以使用git reset --hard 63ee781将红色记录删除,则恢复了cmmit3,运行git log后可以看到:

commit3: add test3.c

commit2: add test2.c

commit1: add test1.c

这里也可以使用另外一种方法来实现:git cherry-pick 63ee7

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值