当前位置:   article > 正文

git add -p用法记录

git add -p

今天提交代码时,遇到只想提交类中部分代码的问题,之前也用过git add -p,只是简单选择hunk就可完成任务,但今天的情况有所不同,特记录如下,加深一下记忆。

  1. @@ -222,18 +219,22 @@ public class ReportImpl implements Report {
  2. }
  3. @Override
  4. - public ReportDto findById(String sn, String pw, long id) {
  5. - ReportDto result = new ReportDto();
  6. + public ReportDto findById(IdentityDto identity, long id) {
  7. + ReportDto result = null;
  8. try (Connection con = sql2o.open()) {
  9. - Report report = con.createQuery("SELECT * FROM Report WHERE Id = :Id")
  10. + Report report = con.createQuery("SELECT * FROM Report WHERE Id = :Id AND Deleted = 0 " +
  11. + "AND HasLeft = 1 AND HasRight = 1")
  12. .addParameter("Id", id)
  13. .executeAndFetchFirst(ReportImpl.class);
  14. - mapper.map(report, result);
  15. - List<? extends ReportContent> contents = con.createQuery("SELECT * FROM ReportContent " +
  16. - "WHERE Uuid = :Uuid")
  17. - .addParameter("Uuid", report.getUuid())
  18. - .executeAndFetch(ReportContentImpl.class);
  19. - setContent(result, contents);
  20. + if (report != null) {
  21. + result = new ReportDto();
  22. + mapper.map(report, result);
  23. + List<? extends ReportContent> contents = con.createQuery("SELECT * FROM ReportContent " +
  24. + "WHERE Uuid = :Uuid")
  25. + .addParameter("Uuid", report.getUuid())
  26. + .executeAndFetch(ReportContentImpl.class);
  27. + setContent(result, contents);
  28. + }
  29. }
  30. return result;
  31. }

选择s后,Split into 3 hunks.

  1. @@ -222,6 +219,6 @@
  2. }
  3. @Override
  4. - public ReportDto findById(String sn, String pw, long id) {
  5. ReportDto result = new ReportDto();
  6. + public ReportDto findById(IdentityDto identity, long id) {
  7. ReportDto result = null;
  8. try (Connection con = sql2o.open()) {
  9. Stage this hunk [y,n,q,a,d,/,K,j,J,g,e,?]?
对,这就是这里,只想提交这两行代码

  1. - public ReportDto findById(String sn, String pw, long id) {
  2. + public ReportDto findById(IdentityDto identity, long id) {
其他的修改,放在下次。看了Youtube上的视频后,发现编辑
@@ -222,6 +219,6 @@
很重要,但按照编辑窗口的说明,remove ‘+’和remove ‘-’替换成' '也没有成功,即:
  1. @Override
  2. - public ReportDto findById(String sn, String pw, long id) {
  3. ReportDto result = new ReportDto();
  4. + public ReportDto findById(IdentityDto identity, long id) {
  5. ReportDto result = null;
  6. try (Connection con = sql2o.open()) {
后来发现,需要调整为:

  1. @@ -222,6 +219,6 @@
  2. }
  3. @Override
  4. - public ReportDto findById(String sn, String pw, long id) {
  5. + public ReportDto findById(IdentityDto identity, long id) {
  6. ReportDto result = new ReportDto();
  7. try (Connection con = sql2o.open()) {
即恢复成没有修改ReportDto result = null;的样子,然后对应的修改
@@ -222,6 +219,6 @@
这里恰好也是6行的6行(至于如何计算行数,还需查看diff的相关说明),所以不用修改,但如果发生了改变,则需要修改对应的-和+的行数。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/代码探险家/article/detail/822824
推荐阅读
相关标签
  

闽ICP备14008679号