Why use Git + Gerrit + Jenkins for code version control

Trouble:

  • Often without a good base version to release and develop, update the code and found that it does not work;
  • Painstakingly modified code does not make it into the repository;
  • It’s not my bug that requires my time;
  • The more people on the development team, the more difficult the integration…

Benefits:

  • Version history trace (git);
  • Code check before submission, bad code does not contaminate the code base (gerrit&jenkins);
  • Quickly locate problems;
  • Convenient management of multiple projects;
  • Facilitate teamwork…

Git + Gerrit + Jenkins: Flexible, fast, powerful and easy to use.

Git commands and status

Git Community Book

This article selected the most practical, most commonly used skills, convenient for everyone to quickly master, improve work efficiency.

Git branch and Git commit

Think of Branch as an assembly line, and think of COMMIT as lego bricks.

The Branch pipeline is made up of one commit block at a time.


Update the code

Instead of git pull, git fetch + git rebase

Git pull creates git merge commit.

What is merge Commit?

When two branches merge in Git, we need to make a mark. This is the point where we merge, like two ropes tied together:

This result is the merge commit generated when Git merge.

Merge Commit why do I hate it?

If it is an important branch merge, we will need a Merge COMMIT as witness. It’s like when you say your vows you need a witness.

But you don’t need witnesses to talk, or you’ll be too tired.

Since your local branch is not important, you don’t need to leave a witness when merging with the main master.

The mainline as long as your commit doesn’t want the whole branch.

Solutions:

    git fetch origin
    git rebase origin/master
Copy the code

Git rebase moves your commit to the top of the mainline.

What if a conflict occurs during rebase?

  1. git statusSee which file conflicts? (SRC/Java /a.java conflicts)
  2. Open the file and resolve the conflict
  3. git add src/java/a.java
  4. git rebase --continue

submit

  • Self check before submission: What did you just change
    git status
    git diff
Copy the code

Git commands can add paths to files or directories, and these commands are no exception.

Such as:

Git status. View the modified files in the current directory.

Git diff SRC/Java /franny.java

  • submit
	git commit
	git push origin HEAD:refs/for/master
Copy the code

Git commit. Commit the modified files in the current directory.

Git commit SRC/Java /a.java SRC/Java /b.java

Refs /for means to submit to Gerrit and go through the code review process. Only after being reviewed by someone with authority will the code base be closed to you.

Refs /heads and Refs /tags are submitted directly to the code base, not to mention the general public.


Restore files

Common scenarios:

Git commit file1 file2 file3 git commit file1 file2 file3 Too much trouble.

Solutions:

Git checkout SRC/Java /a.java

Then git commit -a commits all files at once.


Merging multiple commits

Common scenarios:

Oops, my changes committed two commits, and I want to merge them into a commit and push them.

$ git log
commit bbb6ca1ddca3b7b59f299c5b9e6bb0c8dc965793
Author: Zhao Fengyi <[email protected]>
Date:   Tue Aug 23 20:10:49 2016 +0800

    this is my 3rd commit

commit 72fa1768fe5db08323394fe1f4a3fb8d71b7c712
Author: Zhao Fengyi <[email protected]>
Date:   Tue Aug 23 20:10:09 2016 +0800

    this is my 2nd commit

commit 87ee65f7ff23afacbbe98c62965b243557e3a3e0
Author: Zhao Fengyi <[email protected]>
Date:   Tue Aug 23 20:09:55 2016 +0800

    this is my 1st commit

Copy the code

That is, I want to combine the next two commits into one.

There are three solutions:

  • The first way
Git add. <the 1st commit> git commit -am"blabla"Submit all submitted documents at onceCopy the code

Specific execution process:

$ git status
On branch master
nothing to commit, working directory clean
Copy the code
$ git reset 87ee65f7ff23afacbbe98c62965b243557e3a3e0
Copy the code
$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

	b.java
	c.java

nothing added to commit but untracked files present (use "git add" to track)
Copy the code
$ git add . ; git commit -am "merge 2 commits together"
[master 5115542] merge 2 commits together
 2 files changed, 24 insertions(+)
 create mode 100644 src/java/b.java
 create mode 100644 src/java/c.java
Copy the code
$ git log
commit 5115542312503fc0422d48e531d12ee506384cd4
Author: Zhao Fengyi <[email protected]>
Date:   Tue Aug 23 20:16:22 2016 +0800

    merge 2 commits together

commit 87ee65f7ff23afacbbe98c62965b243557e3a3e0
Author: Zhao Fengyi <[email protected]>
Date:   Tue Aug 23 20:09:55 2016 +0800

    this is my 1st commit

Copy the code
  • The second way
git checkout <the 1st commit>
git merge --squash <the 2nd commit> <the 3rd commit>
git commit -a
Copy the code

Specific execution process:

$ git checkout 87ee65f7ff23afacbbe98c62965b243557e3a3e0
Note: checking out '87ee65f7ff23afacbbe98c62965b243557e3a3e0'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b new_branch_name

HEAD is now at 87ee65f... this is my first commit
Copy the code
$ git merge 72fa1768fe5db08323394fe1f4a3fb8d71b7c712 bbb6ca1ddca3b7b59f299c5b9e6bb0c8dc965793 --squash Updating 87ee65f.. bbb6ca1 Fast-forward Squash commit -- not updating HEAD src/java/b.java | 12 ++++++++++++ src/java/c.java | 12 ++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 src/java/b.java create mode 100644 src/java/c.javaCopy the code
$ git status
HEAD detached at 87ee65f
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	new file:   b.java
	new file:   c.java
Copy the code
$ git commit -a
[detached HEAD 0212114] Squashed commit of the following:
 2 files changed, 24 insertions(+)
 create mode 100644 src/java/b.java
 create mode 100644 src/java/c.java
Copy the code
$ git log
commit 021211462df14dae5084842b85c0122222dcc453
Author: Zhao Fengyi <[email protected]>
Date:   Tue Aug 23 20:26:32 2016 +0800

    Squashed commit of the following:
    
    commit bbb6ca1ddca3b7b59f299c5b9e6bb0c8dc965793
    Author: Zhao Fengyi <[email protected]>
    Date:   Tue Aug 23 20:10:49 2016 +0800
    
        this is my 3rd commit
    
    commit 72fa1768fe5db08323394fe1f4a3fb8d71b7c712
    Author: Zhao Fengyi <[email protected]>
    Date:   Tue Aug 23 20:10:09 2016 +0800
    
        this is my 2nd commit

commit 87ee65f7ff23afacbbe98c62965b243557e3a3e0
Author: Zhao Fengyi <[email protected]>
Date:   Tue Aug 23 20:09:55 2016 +0800

    this is my 1st commit

Copy the code
  • The third way (recommended) :
Modified some files git commit-am for a problem"blabla"Some changes have been made to git commit for the same problem-a --amend

Copy the code

Look for differences between versions

Common scenarios:

This bug is not in the last version, how does this version appear? I need to see what’s changed between the last version and this one.

Solutions:

  • Rough version command (applicable to submission between versions is not much, <10 bar)
git log<branchA/tagA/commitA>.. <branchB/tagB/commitB> -- <path>Copy the code

Specific execution process:

src/main/java/com/xxx/yyy/ui/activity$ git logOrigin/v7.7.. Origin/v7.8 - commit aaaaaaaaaaaaaaaaaaaaaaaaaaaa Author: Y < [email protected] > Date: Tue Aug 23 18:12:33 2016 +0800 [Requirements development]...... Change-Id: ...... Commit BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB Author: l < [email protected] > Date: Tue Aug 23 15:26:21 2016 + 0800 bug fixed 】 【... Commit CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC Author: G < [email protected] > Date: Tue Aug 23 16:32:20 2016 + 0800 demand - from a third party to jump into... Change-Id: ...... commit ddddddddddddddddddddddddddddddddddd Author: S <[email protected]> Date: Tue Aug 23 10:12:26 2016 +0800Copy the code

Disadvantages are too much, typesetting is not compact, it takes half a day to find a message.

  • Refined commands (compact, easy to see)
git log<branchA/tagA/commitA>.. <branchB/tagB/commitB> --color --graph --pretty=format:'%Cred%h%Creset -%s %Cgreen(%cr) %C(bold blue)<%an>%Creset' -- <path>

Copy the code

Specific execution process:

src/main/java/com/xxx/yyy/ui/activity$ git logOrigin/v7.7.. Origin/v7.8 - color - graph - pretty = format:'%Cred%h%Creset -%s %Cgreen(%cr) %C(bold blue)<%an>%Creset'* a30AF7c -[requirements development]... (16 hours ago) <Y> * cacb3f5 - [bug fixed]... (18 hours ago) < L > * B332D79 - Demand - Jump from third party... (18 hours ago) <G> * 0385105 -[feature optimization] Remove useless xxx.java (18 hours ago) <S>Copy the code

Who should take the pot

Common scenarios:

Yeah, that’s the line of code that changed it. Look who changed it.

Solutions:

Git Studio does the same thing:

More advanced requirements:

Git Blame only sees who added the line. What if you know some code was deleted and want to see who deleted it?

Solutions:

git log -p -- <file>

Copy the code

Search the print for deleted lines.

Specific execution process:

src/main/java/com/xxx/yyy/ui/view/message$ git log-p -- lllview.java search xxxManager\.getyyy\(\)\.playCopy the code

Convenient configuration

As someone familiar with Git, I usually only need one line of command to submit the code

 git f && git rh && git rbm && git sa && git pm

Copy the code

To make it easy to operate, do some upfront configuration and you can do it once and for all.

  • Abbreviations alias

Git has so many commands that it can be long and cumbersome to type, and even more annoying if you make a mistake.

Open ~/.gitConfig and add the following abbreviations

[alias]
	co = checkout
	cp = cherry-pick
	br = branch
	st = status
	l1 = log -1
	pm = push origin HEAD:refs/for/master
	ap = apply
	cm = commit
	rbm = rebase origin/master
	f = fetch origin
	rh = reset --hard
	sa = stash apply --0

Copy the code
  • Git commits to git with the same newline character:
git config --global core.autocrlf true
git config --global core.autocrlf true

Copy the code
  • Temporarily store changes that do not need to be committed

For example, gradle Project Sync takes too long and I use a local server instead:

DistributionUrl \ = HTTP: / / 10. X.Y.Z: 8080 / download/gradle - 2.12 - all. ZipCopy the code

This change does not need to be committed, but is required for local development.

We usually store changes that don’t need to be committed locally with git Stash.

Git commit Commit the changes that need to be committed locally.

In this way, the changes to be committed are separated from the changes that don’t need to be committed, so that the code doesn’t have to be separated every time it commits.


Git commit: Git commit: Git commit: Git commit: Git commit

 git f && git stash && git rbm && git pm && git sa

Copy the code
The command Detailed instructions
git f Git Fetch Origin Gets the latest code status from the Git server
git rh Git reset –hard Clears local code that doesn’t need to be committed
git rbm Git rebase origin/master
git sa Git stash apply –0 restores temporary changes
git pm Git push origin HEAD:refs/for/master push gerrit

Then move on to the next COMMIT


other

Sometimes there are thorny issues.

  • For example: beforegit commitMade one submission and then made a bunch of themgit checkoutThe commit could not be found because of some other operation.
Vi. Git /logs/HEAD or git reflogCopy the code

Can view your operation history. Have checked out from where, commit what, rebase where… It’s all clear.

  • For example, if you resolve a conflict and then realize that it shouldn’t be resolved this way, you want to resolve it in a different way. But Git will automatically remember your previous solution and fix it for you, so you won’t have a chance to intervene.
rm -rf .git/rr-cache/*

Copy the code

Let Git forget about my previous solution, which gives me a chance to step in and fix it again.


Do you agree or disagree? Do you agree or disagree? Blush:

Written with StackEdit.