My Git workflow

  1. Create github repo
  2. Clone repo
  3. Add file.txt to master, commit and push file to origin master
  4. Create feature branch: ‘my-branch’
  5. my-branch: work, commit locally
  6. Master: In the mean time someone else works/commits and pushes onto origin master
  7. On my-branch rebase with master to prepare for pull request
  8. On my-branch: fix conflict during rebase
  9. Create pull request via Github (base: master, compare: my-branch)
  10. On my-branch: rework changes, commit and push to origin my-branch so that pull request is updated automatically
  11. In the mean time someone else can checkout my-branch and pull from it
  12. Master: In the mean time someone else works/commits and pushes onto origin master so that github ask to resolve the conflict
  13. On my-branch: DO NOT REBASE WITH MASTER since someone could pull from origin my-branch and that would break the history showing the same commits twice
  14. On my-branch: do git MERGE master, commit and push to origin my-branch to update pull request
  15. github can do automatic merge from my-branch to master and close the pull request

Commands:

USER1 workspace:

git clone https://github.com/bmwieczorek/my-git.git
cd my-git
echo "master: created new file.txt" > file.txt
git add file.txt
git commit -m "master: created new file.txt"
git checkout -b my-branch
git branch
echo "my-branch: appended once file.txt" >> file.txt
git commit -am "my-branch: appended once file.txt"
echo "my-branch: appended twice file.txt" >> file.txt
git commit -am "my-branch: appended twice file.txt"
git checkout master
echo "master: appended file.txt" >> file.txt
git commit -m "master: appended file.txt" file.txt
git push origin master

git checkout my-branch
git rebase master
cat file.txt
cat file.txt | egrep -v '<<<|>>>|===' > file.txt # resolve conflict
cat file.txt
git add file.txt
git rebase --continue
cat file.txt
git log
git status
git push origin my-branch

# create pull request via github (base: master, bmwieczorek wants to merge 2 commits into master from my-branch)
# rework after PR

echo "my-branch: appended file.txt after PR code review " >> file.txt
git commit -am "my-branch: appended file.txt after PR code review"
git push origin my-branch
git checkout master
echo "master: appended 2x file.txt" >> file.txt
git commit -m "master: appended 2x file.txt" file.txt
git push origin master

USER2 workspace:

git clone https://github.com/bmwieczorek/my-git.git
echo "master: appended 2x file.txt" >> file.txt
git commit -m "master: appended 2x file.txt" file.txt
git push origin master
git checkout my-branch
git pull

USER1 workspace:

#GitHub pull request page shows conflict on master
# DO NOT (immediately) REBASE ANYMORE ON my-branch since someone could pull but we do not know it, so lets try one of two options:

A) merge master changes (that happened during review of pull request)

git checkout my-branch
git merge master
cat file.txt
cat file.txt | egrep -v '<<<|>>>|===' > file.txt # resolve conflict
cat file.txt
git add file.txt
git commit -am "my-branch: merged changes from master that were committed during the review of pull request of my-branch work"
git push origin my-branch
git checkout master
git merge my-branch

or
B) create a branch before rebasing (as a backup if someone pulled from my-branch to allow to recover as in option A), do the rebase master on my-branch and try to push to origin my-branch:

git checkout my-branch
git checkout -b my-branch-before-rebase
git checkout my-branch
git rebase master
cat file.txt
vim file.txt
cat file.txt
git add file.txt
git rebase --continue
git push origin my-branch
git checkout master
git merge my-branch

What will happen if someone already has pulled my-branch (asa in 11) and later we would rebase with master (as in 13) and try to push

$ git push origin my-branch
Username for 'https://github.com':
Password for 'https://bmwieczorek@github.com':
To https://github.com/bmwieczorek/my-git.git
 ! [rejected] my-branch -> my-branch (non-fast-forward)
error: failed to push some refs to 'https://github.com/bmwieczorek/my-git.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

Then there is no other option but to follow what causes problems:

$ git pull origin my-branch
From https://github.com/bmwieczorek/my-git
 * branch my-branch -> FETCH_HEAD
Auto-merging file.txt
CONFLICT (content): Merge conflict in file.txt
Automatic merge failed; fix conflicts and then commit the result.

$ cat file.txt
$ cat file.txt | egrep -v '<<<|>>>|===' > file.txt # resolve conflict
$ git add file.txt
$ git log
commit 309652ab390e39a8f6ca8a1aae9624cefb4da809 
Date: Wed Feb 3 15:16:16 2016 +0100
my-branch: appended file.txt after PR code review

commit 4995ef9527e60b6672092f3bc348ff316826cbfc
Date: Wed Feb 3 14:54:45 2016 +0100
my-branch: appended twice file.txt

commit 120b76fd4370716fca19670695d9be8765f124c6
Date: Wed Feb 3 14:54:45 2016 +0100
my-branch: appended once file.txt

commit e2835035d3c09113c5d23a54b6375925f314c0ca
Date: Wed Feb 3 15:37:42 2016 +0100
master: appended 2x file.txt

commit 36612b57f46d04b1343be1f0e8e70a8236313abe
Date: Wed Feb 3 14:54:45 2016 +0100
master: appended file.txt

commit 6bc6342dfd6f14a553ff1f6950876d95feab4901
Date: Wed Feb 3 14:54:45 2016 +0100
master: created new file.txt

$ git commit -am "my-branch: rebased, push failed, pulled, resolved, push" [my-branch d9a2360] my-branch: rebased, push failed, pulled, resolved, push
$ git push origin my-branch
To https://github.com/bmwieczorek/my-git.git
 7805d9c..d9a2360 my-branch -> my-branch

Notice how the history gets messed-up with 3 additional duplicated entries:

$ git log
commit d9a236031582cde3742f6957f70d7c287f8162b9
 Merge: 309652a 7805d9c
Date: Wed Feb 3 15:56:38 2016 +0100
my-branch: rebased, push failed, pulled, resolved, push

commit 309652ab390e39a8f6ca8a1aae9624cefb4da809
 Date: Wed Feb 3 15:16:16 2016 +0100
my-branch: appended file.txt after PR code review

commit 4995ef9527e60b6672092f3bc348ff316826cbfc
 Date: Wed Feb 3 14:54:45 2016 +0100
my-branch: appended twice file.txt

commit 120b76fd4370716fca19670695d9be8765f124c6
Date: Wed Feb 3 14:54:45 2016 +0100
my-branch: appended once file.txt

commit e2835035d3c09113c5d23a54b6375925f314c0ca
Date: Wed Feb 3 15:37:42 2016 +0100
master: appended 2x file.txt

commit 7805d9c1e0d84a4746f274bce3f869d3868a38c4
 Date: Wed Feb 3 15:16:16 2016 +0100
my-branch: appended file.txt after PR code review

commit a280c861a6ea6a59c07c11a99c440ec3a07f2f41
 Date: Wed Feb 3 14:54:45 2016 +0100
my-branch: appended twice file.txt

commit 626bd4418a3f33aa2afe7ff63665938c836a65d5
Date: Wed Feb 3 14:54:45 2016 +0100
my-branch: appended once file.txt

commit 36612b57f46d04b1343be1f0e8e70a8236313abe
Date: Wed Feb 3 14:54:45 2016 +0100
master: appended file.txt

commit 6bc6342dfd6f14a553ff1f6950876d95feab4901
Date: Wed Feb 3 14:54:45 2016 +0100
master: created new file.txt
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s