2.4. Useful Advanced Git Commands#

2.4.1. Showing differences between versions of the repository#

To uncommitted changes in the repository use

$ git diff

It is also possible to see the uncommitted changes of a set of files

$ git diff [files]

Or between two commits

$ git diff [commit-1] [commit-2]

Or between two branches

$ git diff [branch-1] [branch-2]

To learn more about the numerous options of git diff you can have a look into those resources:

2.4.2. Selecting a sample of commits from another branch#

To apply a sample of commits from another branch onto the current one, use the following command

$ git cherry-pick [commits]

2.4.3. Committing temporarily#

Git allows to save change temporarely in a stash.
To save the current changes from the working directory, use the following command

$ git stash push -m [message]

To see the status of the current stash, use this command

$ git stash list

To show the difference between the stash entry and the commit back when the entry was created, use the command

$ git stash show stash@{[stash-id]}
# Example git show apply stash@{0}

To apply the change saved in a stash entry to the current working tree, use this command

$ git stash apply stash@{[stash-id]}
# Example git stash apply stash@{1}

To remove and entry from the stash, use the command

$ git stash drop stash@{[stash-id]}
# Example git stash drop stash@{2}

To both apply to the working tree then remove the entry from the stash, use this shortcut

$ git stash pop stash@{[stash-id]}
# Example git pop apply stash@{0}

To remove all entries from the stash, use this command

$ git stash clear

2.4.4. Editing history#

As an alternative to git merge, it is possible to combine the commits from to branch, using this command

$ git rebase [base-branch]

It is also possible to edit, squash, drop, etc. the commit history interactively, using this command

$ git rebase -i


Note: To rebase interactively, there needs to be previous commits in the history. This is the rational for previously creating an empty commit at the start of the history.
Warning: AVOID using git rebase on public history as it will rewrite the commits history.

git-rebase

source: https://www.atlassian.com/git/tutorials/merging-vs-rebasing