Working with Git#

Start working on a project#

Start your own project#

$ git init MY_PROJECT

creates a new, local git repository.


if the project name is given, Git creates a new directory and initializes it.

If no project name is given, the current directory is initialised.

Work on a project#

$ git clone PROJECT_URL

downloads a project with all branches and the entire history from the remote repository.


indicates the number of commits to be downloaded.


specifies the name of the remote branch to be downloaded.

Work on a project#

$ git status

shows the status of the current branch in the working directory with new, changed and files already marked for commit.

$ git add FILE

adds a file to the stage area.


adds parts of a file to the stage area.


the changes to be adopted can be edited in the standard editor.

$ git diff FILE

shows differences between work and stage areas, for example:

$ git diff docs/productive/git/work.rst
diff --git a/docs/productive/git/work.rst b/docs/productive/git/work.rst
index e2a5ea6..fd84434 100644
--- a/docs/productive/git/work.rst
+++ b/docs/productive/git/work.rst
@@ -46,7 +46,7 @@

 :samp:`$ git diff {FILE}`
-    shows differences between work and stage areas.
+    shows differences between work and stage areas, for example:

index e2a5ea6..fd84434 100644 displays some internal Git metadata that you will probably never need. The numbers correspond to the hash identifiers of the git object versions.

The rest of the output is a list of diff chunks whose header is enclosed in @@ symbols. Each chunk shows changes made in a file. In our example, 7 lines were extracted starting at line 46 and 7 lines were added starting at line 46.

By default, git diff performs the comparison against HEAD. If you use git diff HEAD docs/productive/git/work.rst in the example above, it will have the same effect.

git diff can be passed Git references. Besides HEAD, some other examples of references are tags and branch names, for example git diff MAIN..FEATURE_BRANCH. The dot operator in this example indicates that the diff input is the tips of the two branches. The same effect occurs if the dots are omitted and a space is used between the branches. In addition, there is a three-dot operator: git diff MAIN...FEATURE_BRANCH, which initiates a diff where the first input parameter MAIN is changed so that the reference is the common ancestor of MAIN and FEATURE.

Every commit in Git has a commit ID, which you can get by running git log. You can then also pass this commit ID to git diff:

$ git log --pretty=oneline
af1a395a08221ffa83b46f562b6823cf044a108c (HEAD -> main, origin/main, origin/HEAD) :memo: Add some git diff examples
d650de52306b63b93e92bba4f15be95eddfea425 :memo: Add „Debug .gitignore files“ to git docs

$ git diff af1a395a08221ffa83b46f562b6823cf044a108c d650de52306b63b93e92bba4f15be95eddfea425
--staged, --cached

shows differences between the stage area and the repository.


shows the changed words.

$ git restore FILE

changes files in the working directory to a state previously known to Git. By default, Git checks out HEAD, the last commit of the current branch.


In Git < 2.23, git restore is not yet available. In this case you still need to use git checkout:

$ git checkout FILE

$ git commit

make a new commit with the added changes.

-m 'Commit message'

write a commit message directly in the command line.

--dry-run --short

shows what would be committed with the status in short format.

$ git reset [--hard|--soft] [target-reference]

resets the history to an earlier commit.

$ git rm FILE

removes a file from the work and stage areas.

$ git stash

moves the current changes from the work area to the stash.

To be able to distinguish your hidden changes as well as possible, the following two options are recommended:

-p or --patch

allows you to partially hide changes, for example:

$ git stash -p
diff --git a/docs/productive/git/work.rst b/docs/productive/git/work.rst
index cff338e..1988ab2 100644
--- a/docs/productive/git/work.rst
+++ b/docs/productive/git/work.rst
@@ -83,7 +83,16 @@
         lists the hidden changes.
-        shows the changes in the hidden files.
+        shows the changes in the hidden files, for example

(1/1) Stash this hunk [y,n,q,a,d,e,?]? y

With ? you get a complete list of options. The most common are:




Hide this change


Do not apply this change


All changes already selected will be hidden


Apply this and all subsequent changes


Edit this change manually




creates a branch from hidden files, for example:

$ git stash branch stash-example stash@{0}
On branch stash-example
Changes marked for commit:
  (use "git restore --staged <file>..." to remove from staging area).
    new file: docs/productive/git/work.rst

Changes not marked for commit:
  (use "git add <file>..." to mark the changes for commit).
  (use "git restore <file>..." to discard the changes in the working directory)
    changed: docs/productive/git/index.rst

stash@{0} (6565fdd1cc7dff9e0e6a575e3e20402e3881a82e) gelöscht

adds a message to the changes.


hides unversioned files.


lists the hidden changes.


shows the changes in the hidden files.


transfer the changes from the hiding place to the work area and empty the hiding place, for example:

git stash pop stash@{2}

empties a specific stash, for example:

$ git stash drop stash@{0}
stash@{0} (defcf56541b74a1ccfc59bc0a821adf0b39eaaba) deleted

delete all your hiding places.