Skip to content

Commit 1dfca2f

Browse files
committed
Added git diff and git cherry
Signed-off-by: Nastasie Raul-Ionut <ion_nastasie@yahoo.com>
1 parent 1cbb917 commit 1dfca2f

File tree

1 file changed

+48
-3
lines changed

1 file changed

+48
-3
lines changed

README.md

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,13 @@ For that, in the GitHub web interface for the pull request follow the steps:
170170
./gh-reset-repo.sh
171171
```
172172

173-
174173
1. Create your own commits and pull requests.
175174
Be creative.
176175

177176
Create at least one pull request with two commits.
178177
Use the `Squash and merge` merge strategy.
179178

180-
## Understand `git merge` and `git rebase`
179+
## Understanding `git merge` and `git rebase`
181180

182181
When working with multiple branches, we often need to bring changes from one branch into another.
183182
The two most common ways to do this are:
@@ -487,7 +486,6 @@ They may contain the same changes, but they are rewritten commits.
487486
* the result looks like the work happened in a straight line
488487
* there is usually no merge commit
489488

490-
491489
### Now you want to remove all the commits created during this rebase exercise.
492490

493491
After the rebase, the commits created during this exercise are still on `<your-rebase-branch-name>`, but they were rewritten on top of `main`.
@@ -527,6 +525,53 @@ If needed, delete `<your-rebase-branch-name>` as well:
527525
git branch -D <your-rebase-branch-name>
528526
```
529527

528+
## Checking your work with `git cherry` and `git diff`:
529+
530+
Let's take a quick look at two very useful commands for checking your work: `git diff` and `git cherry`.
531+
532+
First, make sure you're on the `main` branch. Use either `git status` or `git branch`. Now, let's create a new branch:
533+
``` console
534+
git checkout -b <branch-name>
535+
```
536+
537+
## `git diff` is used to see the changes you've made and what state they're in.
538+
539+
Go to `cpp-hello/hello.cpp` and change the message it prints. Now run
540+
``` console
541+
git diff
542+
```
543+
and you should see the change you've just made. `git diff` with no arguments will show you your unstaged changes (as in: changes you haven't comitted yet).
544+
545+
To see the changes you've commited but haven't pushed, use:
546+
``` console
547+
git diff --staged
548+
```
549+
It shouldn't show anything for now. Commit your change to `cpp-hello/hello.cpp` and run it again.
550+
551+
Then run `git diff`. Since your changes are now staged, it won't show anything.
552+
553+
The last use of `git diff` is to show the difference between branches. Using:
554+
``` console
555+
git diff main
556+
```
557+
will show the difference between your current work and the `main` branch. `git diff --staged main` also works.
558+
559+
## `git cherry` shows the difference in commits between two branches.
560+
561+
Try running:
562+
``` console
563+
git cherry main
564+
```
565+
You should see a hash that looks familiar. If it doesn't, try running `git diff main` again. Spoiler: it's the same commit (or at least it should be if you've done everything correctly).
566+
567+
`git cherry <second-branch>` shows you commits that your current branch has but `<second-branch>` doesn't have.
568+
569+
Remembering and recognizing hashes is hard though. Try running:
570+
``` console
571+
git cherry -v main
572+
```
573+
This way, you can also see the commit message of the commits missing from `main`.
574+
530575
## Configure Merge Strategy
531576

532577
We want to configure Rebase and merge as the only merge strategy.

0 commit comments

Comments
 (0)