Solution to Step 0 - START
Solution to Step 1 - Create local commit(s)
Edit the file frontend.java
and create (at least) one commit with the changes. Then check with git status
if the workspace is clean.
my-apollo $ # Edit file frontend.java at line 1 on branch main by bjoern.
my-apollo $ git commit -am "`frontend.java`: Edit file frontend.java at line 1 on branch main by bjoern. "
[main b09dd4a] : Edit file frontend.java at line 1 on branch main by bjoern.
1 file changed, 1 insertion(+), 1 deletion(-)
/bin/bash: line 1: frontend.java: command not found
And now just check if the working tree is clean
.
my-apollo $ git status
On branch main
Your branch is ahead of 'origin/main' by 1 commit.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
Solution to Step 2 - Try to push
Now try to push your changes.
my-apollo $ git push
To ../blessed-apollo.git
! [rejected] main -> main (fetch first)
error: failed to push some refs to '../blessed-apollo.git'
hint: Updates were rejected because the remote contains work that you do not
hint: have locally. This is usually caused by another repository pushing to
hint: the same ref. If you want to integrate the remote changes, use
hint: 'git pull' before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
As you can see, the push was rejected. Apparently Anja was faster and pushed her changes to blessed-apollo.git
first.
Solution to Step 3 - (optional) Analyze problem
First fetch the changes without integrating (fetch
), and let Anja’s changes be shown to you.
- Which commits did Anja make (
log
)? - What are the differences between your and Anja’s version (symmetrical
diff
)? - What changes did Anja make (asymmetrical
diff
)?
fetch
fetches the data without changing the workspace or your local branches.
my-apollo $ git fetch
From ../blessed-apollo
e9477ea..3076d39 main -> origin/main
The output shows that new commits have been fetched for origin/main
The ..
notation shows which commits have been added:
my-apollo $ git log --oneline main..origin/main
3076d39 : Edit file backend.java at line 5 on branch main by anja .
eccdd50 : Edit file backend.java at line 1 on branch main by anja .
The normal (symmetrical) diff shows all differences. Both what you did and what Anja did:”
my-apollo $ git diff --stat HEAD origin/main
backend.java | 6 ++++--
frontend.java | 2 +-
2 files changed, 5 insertions(+), 3 deletions(-)
The asymmetrical diff ...
only shows those changes that Anja made (relative to the last common ancestor):”
my-apollo $ git diff --stat HEAD...origin/main
backend.java | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
Solution to Step 4 - Integrate foreign changes
Integrate the changes with pull and then look at the commit graph.
my-apollo $ git pull
Merge made by the 'ort' strategy.
backend.java | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
Since Anja edited a different file (backend.java
) than you (frontend.java
), her changes could be integrated without any problems. You can see that a new commit has been created, which merges the branches.
my-apollo $ git log --graph --oneline
* afa9054 Merge branch 'main' of ../blessed-apollo
|\
| * 3076d39 : Edit file backend.java at line 5 on branch main by anja .
| * eccdd50 : Edit file backend.java at line 1 on branch main by anja .
* | b09dd4a : Edit file frontend.java at line 1 on branch main by bjoern.
|/
* e9477ea Created file frontend.java on branch main by anja .
* f2169d3 Created file backend.java on branch main by anja .
Attention: pull
can cause merge conflicts …
… if both sides have edited the same places. Resolving merge conflicts is the topic of a subsequent chapter.
Solution to Step 5 - Push again
my-apollo $ git push
To ../blessed-apollo.git
3076d39..afa9054 main -> main
And lo and behold: Now it works.
Attention: If someone was faster again …
… and has pushed to blessed-apollo.git, there can be another Push Reject, and we try again a pull
, then a push
, as long as it works.