Handling changes - GradedJestRisk/git-training GitHub Wiki

In panic, look here first !

Table of Contents

Extract a file version without checkout a branch

git restore --source REFERENCE FILE, eg. git restore --source master~2 package.json

Conflicts

rebase

Git consider all files as plain text. Conflicts occurs when changes are made on the same location in a file. Git doesn't make any semantic/syntactic match, so two lines added at the end of a file, make a conflicts. Even if the changes are adding two bash functions, causing no conflict whatsoever from de developer point of view.

Example while rebasing [this], switching C with B, target being A, C, B

Steps:

  • lists file in conflict: git status
  • you'll get the rebase progress
Last commands done (2 commands done):
   pick 376a1cb Add A to file
   pick e0fa5f1 Add C to file
Next command to do (1 remaining command):
   pick ce2be51 Add B to file
You are currently rebasing branch 'topic' on 'd2ad125'.

  • and then the current conflict
Unmerged paths:
	both modified:   file.txt
  • open the file and find conflict
    • current state (before applying commit) : between <<<<<<< HEAD and =======
    • commit to be applied : after ======= and >>>>>>> e0fa5f1
<<<<<<< HEAD

=======

B
C
>>>>>>> e0fa5f1... Add C to file
  • make the change (here, switch C with B)
<<<<<<< HEAD

=======

C
B
>>>>>>> e0fa5f1... Add C to file
  • remove the markup
    • >>>>>>>
    • <<<<<<<
    • =======
  • check conflict is marked as resolved: git status
You are currently rebasing branch 'topic' on 'd2ad125'.
  (all conflicts fixed: run "git rebase --continue")
  • add to staging: git add file.txt
  • continue rebase: git rebase --continue
  • you'll be offered the opportunity to change the commit message
  • fix the next conflict
<<<<<<< HEAD
C

=======

>>>>>>> ce2be51... Add B to file
B
  • you should ends with the message Successfully rebased and updated refs/heads/topic.

If you rewrote the history

Push only if the remote hasn't changed, or you will overwrite someone else's changes, see force considered harmful.

To do so:

  • check you HAVE NOT updated remote's status (look in your CLI history for git fetch )
  • try to push git push --force-with-lease

Common scenarios

Overwrite local changes (with remote changes)

Local are already committed, but not pushed

Steps:

  • get remote changes: git fetch --all
  • overwrite local with remote: git reset --hard origin/master

rebase VS checkout

List:

  • checkout moves HEAD, but does not update branch pointer (eg. master) - it restore working directory index
  • checkout moves HEAD and updates branch pointer (eg. master) - --soft change nothing more / (no param) change index / --hard change index and working directory
Nice pictures here
⚠️ **GitHub.com Fallback** ⚠️