git tricks

rebase -i

  • drop
  • move
  • rename
  • merge
  • edit list of files
pick 8eb8793 Connect: reset registration form on load
pick b317be9 Connect: fix unit tests
pick da8bc4a Connect: fix add site test
pick ec1703a Connect: fix e2e tests

# Rebase d5fade4..ec1703a onto d5fade4 (4 command(s))
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

push --force-with-lease

  • check if someone else has pushed to the branch
  • reject if (s)he does 
  • allow otherwise

reflog

ec1703a HEAD@{0}: rebase -i (finish): returning to refs/heads/feature/CC-356-unify-validation-and-localization
ec1703a HEAD@{1}: rebase -i (start): checkout HEAD~4
ec1703a HEAD@{2}: rebase -i (finish): returning to refs/heads/feature/CC-356-unify-validation-and-localization
ec1703a HEAD@{3}: rebase -i (start): checkout HEAD~4
ec1703a HEAD@{4}: commit: Connect: fix e2e tests
da8bc4a HEAD@{5}: commit: Connect: fix add site test
b317be9 HEAD@{6}: commit: Connect: fix unit tests
8eb8793 HEAD@{7}: commit: Connect: reset registration form on load
d5fade4 HEAD@{8}: reset: moving to HEAD~1
ea3b549 HEAD@{9}: commit: Connect: reset registration form on load
d5fade4 HEAD@{10}: commit: Connect: handle form reset in tuple
0935c85 HEAD@{11}: commit: Connect: connect input and label
22b27af HEAD@{12}: merge develop: Merge made by the 'recursive' strategy.
58ce01b HEAD@{13}: checkout: moving from develop to feature/CC-356-unify-validation-and-localization
4bc834c HEAD@{14}: rebase finished: returning to refs/heads/develop
4bc834c HEAD@{15}: pull --rebase origin: checkout 4bc834cf1599240e21ec57858c954cb7622852b6
8b29039 HEAD@{16}: checkout: moving from feature/CC-356-unify-validation-and-localization to develop
58ce01b HEAD@{17}: commit: Connect: reset study id form when loaded
ad60a76 HEAD@{18}: checkout: moving from feature/CC-387-when-user-adds-study-and-is-redir to feature/CC-356-unify-validation-and-localization
959930e HEAD@{19}: checkout: moving from feature/CC-356-unify-validation-and-localization to feature/CC-387-when-user-adds-study-and-is-redir
ad60a76 HEAD@{20}: commit: Connect: turn on backend validation on most of the forms
b339bc1 HEAD@{21}: commit: Connect: make criteria tab looking better
e4185dd HEAD@{22}: commit: Connect: fix study select hiding
f9641f6 HEAD@{23}: commit: Connect: fix physician study tabs
7106cb2 HEAD@{24}: commit: Connect: try to fix some tests
91c615e HEAD@{25}: commit: Connect: fix form errors
0f8cf47 HEAD@{26}: commit: Connect: fix tests for password change

Log of ANY operation

  • merge
  • rebase
  • stash
  • commit
  • checkout

Undo any operation!

reset

Accidental commit?

git reset HEAD~1

Want to start from scratch?

git reset --hard

Something is really screwed up and you want to make sure your local branch is equal to remote one?

git reset --hard origin my-branch

status -s

Quick preview of changes

 M __nightwatch__/physician/tour/finish.js
 D __nightwatch__/physician/tour/selectors.js
 M __nightwatch__/physician/tour/skip.js
 M __nightwatch__/physician/tour/skipForever.js
?? __nightwatch__/physician/tour/close.js
?? __nightwatch__/physician/tour/next.js
?? __nightwatch__/physician/tour/prev.js
?? __nightwatch__/physician/tour/utils.js

Instead of

On branch feature/CC-468-physician-fix-quicktour
Your branch is behind 'origin/feature/CC-468-physician-fix-quicktour' by 1 commit, and can be fast-forwarded.
  (use "git pull" to update your local branch)
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   __nightwatch__/physician/tour/finish.js
        deleted:    __nightwatch__/physician/tour/selectors.js
        modified:   __nightwatch__/physician/tour/skip.js
        modified:   __nightwatch__/physician/tour/skipForever.js

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        __nightwatch__/physician/tour/close.js
        __nightwatch__/physician/tour/next.js
        __nightwatch__/physician/tour/prev.js
        __nightwatch__/physician/tour/utils.js

no changes added to commit (use "git add" and/or "git commit -a")

clean -f

On branch feature/CC-468-physician-fix-quicktour
Your branch is up-to-date with 'origin/feature/CC-468-physician-fix-quicktour'.
Untracked files:
  (use "git add <file>..." to include in what will be committed)

        01_some
        02_rubbish
        03_files
        04_like
        05_npm-error.log.124435
        06_temporary-fieles-of-each-kind
        07_porn-you-downloaded-to-repo-dir-by-accident

nothing added to commit but untracked files present (use "git add" to track)

Even better with a little script!

Cleaning

Would remove 01_some
Would remove 02_rubbish
Would remove 03_files
Would remove 04_like
Would remove 05_npm-error.log.124435
Would remove 06_temporary-fieles-of-each-kind
Would remove 07_porn-you-downloaded-to-repo-dir-by-accident

Proceed?
1) Yes  2) No   
?# 

git branch -vvv(a)

remote prune origin

* develop                                          23d2b51 [origin/develop] Merge branch 'feature/CC-558_Physician_Referring_patient_updating_textss' into develop CC-558 
referring patients new texts merge
  feature/CC-404-backend                           3d76678 [origin/feature/CC-404-backend: gone] Connect: wait for button before trying to click
  feature/CC-447                                   84b9632 Connect: fix redirection to add study screen when list is not loaded yet
  feature/CC-450-physician-fix-account-setup-setti 80ad0ed [origin/feature/CC-450-physician-fix-account-setup-setti: behind 40] Connect: update dependencies correctly
  feature/CC-468-physician-fix-quicktour           0387023 [origin/feature/CC-468-physician-fix-quicktour] Connect: fix styles for tour elements
  feature/CC-485-site-simplify-study-left-sidebar  18012f3 [origin/feature/CC-485-site-simplify-study-left-sidebar] Connect: use this.assert instead of standalone one
  feature/CC-492-site-improve-physician-right-side 4f0df87 [origin/feature/CC-492-site-improve-physician-right-side] Connect: fixes after review
  feature/CC-510-remove-underscores-from-fixtures  c9c545b [origin/feature/CC-510-remove-underscores-from-fixtures] Merged in feature/CC-494-investigator-settings-click-c
lick (pull request #240)
  feature/CC-513-merge-two-login-pages-into-one    ff354d4 [origin/feature/CC-513-merge-two-login-pages-into-one: gone] Connect: make var
  feature/CC-526-site-when-user-want-to-delete-add 814cc92 [origin/feature/CC-526-site-when-user-want-to-delete-add] Connect: use letters generator from util
  feature/CC-529-edit-site-button-is-available-whe fd0bce0 [origin/feature/CC-529-edit-site-button-is-available-whe] Connect: check if site exists before showing edit sit
e button
  feature/CC-566-physiciansite-incorrect-color-of- 2a024de [origin/feature/CC-566-physiciansite-incorrect-color-of-] Connect: make all secondary buttons neutral
  feature/CC-form-validation-tests                 6b60ce3 [origin/feature/CC-form-validation-tests] Connect: create custom command for testing field validation
  feature/cc-registersite-uifixes                  079a1a1 [origin/feature/cc-registersite-uifixes] Connect: autogenerate input id
$ git remote prune origin
Pruning origin
URL: git@bitbucket.org:clinworkconnect/apps.git
 * [pruned] origin/feature/CC-446-physician-while-referring-patient
 * [pruned] origin/feature/CC-483-physician-allow-editing-physician
 * [pruned] origin/feature/CC-485-site-simplify-study-left-sidebar
 * [pruned] origin/feature/CC-492-site-improve-physician-right-side
 * [pruned] origin/feature/CC-526-site-when-user-want-to-delete-add
 * [pruned] origin/feature/CC-529-edit-site-button-is-available-whe
 * [pruned] origin/feature/CC-536-site-allow-editing-sites-details-
 * [pruned] origin/feature/CC-558_Physician_Referring_patient_updating_textss

To prune local branches... script!

Removing branches...

feature/CC-404-backend
feature/CC-485-site-simplify-study-left-sidebar
feature/CC-492-site-improve-physician-right-side
feature/CC-513-merge-two-login-pages-into-one
feature/CC-526-site-when-user-want-to-delete-add
feature/CC-529-edit-site-button-is-available-whe

Proceed?
1) Yes  2) No   
?# 1
Removing in progress...
Deleted branch feature/CC-404-backend (was 3d76678).
Deleted branch feature/CC-485-site-simplify-study-left-sidebar (was 18012f3).
Deleted branch feature/CC-492-site-improve-physician-right-side (was 4f0df87).
Deleted branch feature/CC-513-merge-two-login-pages-into-one (was ff354d4).
Deleted branch feature/CC-526-site-when-user-want-to-delete-add (was 814cc92).
Deleted branch feature/CC-529-edit-site-button-is-available-whe (was fd0bce0).
All branches removed!

log

if you like colorful trees...

like this

or this

or that

git log --graph --pretty=format:"%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset" --abbrev-commit --date=relative

log --follow

track changes of given file

$ git log --follow package.json

commit 91d00768300e331bea4169f51a2ecde492cf8685
Author: Eryk Napierała <eryk.piast@gmail.com>
Date:   Fri Sep 30 10:11:32 2016 +0000

    Connect: fix webpack exit code

commit 971c384afb4e407afa7ee021588d3d0a6c7f3614
Author: przemyslaw <przemyslaw.jan.pietrzak@gmail.com>
Date:   Thu Sep 29 15:39:32 2016 +0200

    freeze new lib

commit 23553b7f3a2d8d0b92b1a106f04ca80013927b3f
Author: przemyslaw <przemyslaw.jan.pietrzak@gmail.com>
Date:   Thu Sep 29 15:04:40 2016 +0200

    fix redirect after set persona data

commit 17542887874e83badb4f548f303b27a90ac27280
Author: p.olejak <p.olejak@clinwork.com>
Date:   Mon Sep 19 09:41:21 2016 +0200

    Unmodifying package.json changes

commit b49681c425c7feda632a5b8d4eab4f8c48cf3215
Author: p.olejak <p.olejak@clinwork.com>
Date:   Fri Sep 16 11:10:17 2016 +0200

    eslint

git clone --single-branch --depth=1 

$ git clone git://github.com/django/django.git
Cloning into 'django'...
remote: Counting objects: 369928, done.
remote: Compressing objects: 100% (21/21), done.
remote: Total 369928 (delta 5), reused 0 (delta 0), pack-reused 369907
Receiving objects: 100% (369928/369928), 141.83 MiB | 209.00 KiB/s, done.
Resolving deltas: 100% (269558/269558), done.
Checking connectivity... done.
$ git clone --single-branch --depth=1 git://github.com/django/django.git                                             
Cloning into 'django'...
remote: Counting objects: 7783, done.
remote: Compressing objects: 100% (4847/4847), done.
remote: Total 7783 (delta 1893), reused 5618 (delta 1724), pack-reused 0
Receiving objects: 100% (7783/7783), 8.68 MiB | 260.00 KiB/s, done.
Resolving deltas: 100% (1893/1893), done.
Checking connectivity... done.

git checkout -p

$ git diff file.txt

It's a content of the file

- asdasdasdadasdad
+ It's a good change

- And this is not
+ dassadadaadasda dsad
$ git checkout -p file.txt

y - discard this hunk from worktree
n - do not discard this hunk from worktree
q - quit; do not discard this hunk or any of the remaining ones
a - discard this hunk and all later hunks in the file
d - do not discard this hunk or any of the later hunks in the file
g - select a hunk to go to
/ - search for a hunk matching the given regex
j - leave this hunk undecided, see next undecided hunk
J - leave this hunk undecided, see next hunk
k - leave this hunk undecided, see previous undecided hunk
K - leave this hunk undecided, see previous hunk
s - split the current hunk into smaller hunks
e - manually edit the current hunk
? - print help
@@ -xxx,y +xxx,z @@

It's a content of the file

- asdasdasdadasdad
+ It's a good change

Discard this hunk from worktree [y,n,q,a,d,/,K,j,J,g,e,?]?
$ git diff file.txt

It's a content of the file

- asdasdasdadasdad
+ It's a good change

Discover git

By Eryk Napierała

Discover git

  • 1,764