Skip to content

Local changes

Show Changes

Show changes in your working directory.

git status

Show File Differences

Show all differences of what is changed but not staged.

git diff

Options git diff

--staged for what's staged, but not commited
parameter my-filename shows a specific file only

Handle Changes

Add Changes for Next Commit

Add all tracked and untracked files or just a specific file to your next commit (stage).

git add <file_or_directory>

Options git add

. add all tracked or untracked files
my-changed-file add a specific file only

Commit Changes

Commit your changed content as a new commit.

git commit -am "<descriptive message>"

Options git diff

-a automatically stage all tracked, modified files before the commit
-m <msg> use the given as the commit message
-am <msg> stage tracked and commit with in one step

Amend last message

Change last commit message.

git commit --amend "<descriptive message>"

Don't amend pushed commits

Rebase

Rebase the current branch onto <base>, where <base> can be a commit ID, branch name or tag.

git rebase <base>

Don't rebase pushed commits

Explicit versioning a file remove

Delete the file from repo and stage the removal for commit.

git rm <file>

Undo Changes

Undo last Changes

Discard all changes,

git reset HEAD

Unstage a file while retaining the changes.

git reset <file-name>

Options git reset

--hard automatically stage all tracked, modified files before the commit
-m <msg> use the given as the commit message
-am <msg> stage tracked and commit with in one step

Discard any local, uncommitted changes and thereby restore their last committed state.

git restore .

Revert

Use this command to undo changes to a repository's puplished commit history. The other undo-commands such as, git checkout or git reset, move the HEAD/branch ref pointers to a specified commit. The revert operation will take the specified commit, inverse its changes and creates a new & reverted commit.

git revert <SHA>

Show Commit History

Show the commit history for the currently active branch, starting with the newest.

git log

Options git log

--oneline shows only short SHA and message
--graph show branches in a graphical way
--follow <file-name> Show the commits that changed a file, even across renames

Show Git Object

Show any object in Git <SHA> in human-readable format.

git show <SHA>

Temporary commits

This is useful to temporarily store modified files (on a stash stack) in order to change branches. For example when pulling into a dirty working directory (1)

  1. Example pulling to dirty directory
    $ git pull
    ...
    file foobar not up to date, cannot merge.
    $ git stash
    $ git pull
    $ git stash pop
    

Save modified and staged local modifications away and revert the working directory.

git stash

List stack-order of stashed file changes.

git stash list

Re-apply stored changes from top of the stash stack.

git stash pop

Discard the changes from top of the stash stack.

git stash drop