GIT URLs etc.

This sheet is maintained at GitHub

ssh://usr@host:port/dir/prj.git

Here is another cheat sheet.
And here comes the full documentation.
How to add chmod permissions to file in GIT?
imerge: Incremental Merge
.gitignore and IntelliJ / gitignore.io

There are four data locations:

1. Local Workspace Local folder tree containig the physical files under construction.
2. Local repository Local branches and their modifications.
3. Local copy of remote repositories All data of all configured remotes (remote tracking branches), each of them according to their last fetch date.
4. The real remote repositories, possibly more than one. All data of all configured remotes, each of them according to their last fetch date.

The one and only HEAD may be detached. Then it points to the last revision checked out. Further commits do not move this HEAD.
HEAD may be attached to a local branch. Then it automatically follows all following commits of that branch.
Attaching HEAD to a remote tracking branch or a revision is not possible.
Committing changes in detached state is possible, but the changes tend to get lost because of the missing named reference.

git config

git config

Show a usage page with most important options and commands.

git config --list

Show all configuration values. If a configuration is shown several times, the latter takes precedence.

git config --list --show-origin

Show all configuration values and where they are configured.

git config --list --system

git config --list --global

git config --list --local

Show the configuration options for only one config location. local takes precedence over global, global takes precedence over system.
system: C:/Program Files/Git/etc/gitconfig
global: C:/Users/mmueller/.gitconfig
local: .git/config     (default; local repo)

Configurations are never stored in remote repositories.

git config --global core.autocrlf false

git config --global core.autocrlf input

git config --global --unset core.eol

Suppress globally any conversion of end-of-line characters on checkout or commit.

git config --global core.excludesfile "C:\Users\mmueller\.gitignore"

Use the file .gitignore in your user folder globally.

git config --global pull.rebase true

Do not merge but rebase the locally committed changes, when executing a pull and fast forward is not possible.

git config --global fetch.prune true

Modify the fetch command: When fetching, also delete locally all copies of remote branches, that do not exist on server side anymore.

git config --global init.defaultbranch main

Use main as default branch name instead of master.

git config --global color.diff.meta "blue black bold"

Fix the problem that the meta information is shown in black font. Here it is set to bold blue on black background.

git config --global pretty.mamue " %Cred %h %Cgreen %an %C(yellow) %ai %C(auto) %d %C(cyan) %s %Creset "

Define a condensed format called mamue. You use it by: --pretty=mamue

git config --global pretty.mamue2 " %Cred %h %Cgreen %ai %C(yellow) %ci %Cgreen %<(15,trunc) %an %C(yellow) %<(15,trunc) %cn %C(auto) %d %C(cyan) %s %Creset "

Define a more detailed condensed format called mamue2. You use it by: --pretty=mamue2

git config --global alias.l "log --graph --pretty=mamue2"

Define the new command l. Now git l shows informative and compact history.

git config --global alias.ll "log --graph --pretty=mamue2 --remotes --branches --tags --date-order"

Define the new command ll. Now git ll shows informative and compact history including all remote repos and branches and tags.

git config --global alias.lll "log --graph --pretty=mamue2 --remotes --branches --tags --simplify-by-decoration --date-order"

Define the new command lll. Now git lll shows informative and very compact history of all remote repos including only tagged revisions and HEADs of branches.

git init & git clone & git checkout

git init

Create a new repository in the current folder.

git clone https://example.com/sample

Clone the already existing repository to the current folder.

git checkout master

Set the contents of the working tree to the contents of branch master. Staged and unstaged local changes remain unchanged.

git checkout a1b2c3d

Set the contents of the working tree to the contents of revision a1b2c3d.

git checkout -f master

Throw away all local changes in tracked files (staged or not) but do not delete any untracked files.
-f means force. To also throw away all untracked files, do this before: git add .

Git Revisions

rev~ rev~1

Parent of revision rev. If rev is a merge result and there are two parents, then both notations reference the first parent. That is the parent, that performed the merge operation, not the other, that was merged into the first one.

rev~2 rev~~ rev~~1 rev~1~ rev~1~1

Grand-parent of revision rev. If a revision has two parents, then the first is referenced, i.e. the parent that performed the merge on that level.

rev~3 rev~~~ rev~2~ (...)

Grand-grand-parent of revision rev. If a revision has two parents, then the first is referenced, i.e. the parent that performed the merge on that level.

rev^ rev^1

Parent of revision rev. If rev is a merge result and there are two parents, then both notations reference the first parent. That is the parent, that performed the merge operation, not the other, that was merged into the first one.

Important: If you are working on Windows, you need to duplicacte the ^. A single ^ in Windows is a control character that tells the comman interpreter that the command is continued on the following line.

rev^2

Second parent of revision rev. Only valid if revision rev is a merge result, i.e. there are two parents.

Important: If you are working on Windows, you need to duplicacte the ^. A single ^ in Windows is a control character that tells the comman interpreter that the command is continued on the following line.

refs/heads/master
refs/heads/task/CRED-123
refs/tags/sprint-0.280.0
refs/remotes/origin/master

Refnames. These are examples of other valid notations of revisions.

git add

git add pathspec

Add all files that match the pathspec to the index.

git reset

Please keep in mind that there is a big difference, if you specify a path or not.

git reset 12affe

git reset mytag

git reset mybranch

Move HEAD and the current branch name to point to revision 12affe / mytag / mybranch, but do not change any files in the workspace. The difference between your unmodified workspace and the GIT status is shown as untracked files or still not staged changes. If you add and commit everything, then you are back at the starting point. So you have the chance to rewrite the GIT history by rearranging the commits.

git reset HEAD pathspec

Remove all files that match the pathspec from the index. If you omit the pathspec, then all tracked files become untracked ones, the worktree will not be modified.

git reset --hard HEAD

git reset --hard 123affe

Set the index and the working tree back to HEAD or to revision 123affe. Untracked files may remain untracked. If needed, you have to delete them manually.

git restore

git restore --staged .

Unstage all staged changes.

git restore .

Remove all not-staged modifications on tracked files. Do not touch untracked stuff.

git revert

git revert 1542affe

Create a new commit that reverts all changes of the commit 1542affe. Build the commit message from the reverted commit, prepending it by the word "Revert". 1542affe must not be a merge commit.

git revert -m 1 1542affe

Same as above, but 1542affe is a merge commit and -m 1 selects parent 1 (-m 2 would select parent 2) as target. To find out which is parent 1 and which is parent 2, you can use the command git log -1 1542affe. The output mentions both parents in line 2 after the key word "Merge". Parent 1 is mentioned first, parent 2 is mentioned second.

git revert b1..b2

Revert all the changes contained in branch b2 but not in branch b1. Create a new commit for every reverted commit. Do this reverting in reverse order. No merge commits are allowed on the path to be reverted. If you must revert a merge commit you have to specify the parent number by option -m 1 or -m 2.

git revert ^b1 b2

Revert the commit b1, then the commit b2. This means that ^b1 b2 is NOT the same as b1..b2 as it is in other cases!

git clean

git clean -n -fdx

Show what would be cleaned without the dry-run option -n.

git clean -fdx

Remove all untracked files in all subfolders (-d) and also the ignored files (-x). The force option (-f) is always required.

git status

git status

Display the status of all added, modified or removed files relative to the current working folder.

git status pathspec

Display the status of all added, modified or removed files that match the pathspec in the current folder or below.

git log

git log

Show one page of the newest changes, the newest first.

git log -25 --reverse

Show the 25 newest changes, the newest changes last.

git log --oneline

Show each change on a single line only.

git log --graph

Draw lines at the left to show the branches and merges.

git log --branches --remotes --decorate=full

Show also all branch and remote tags. Do not abbreviate them.

git log --format=full

Show more commit information.

git log --raw

Show also the files that were modified / added / removed by each commit.

git log file

Show all commits that changed file.

git log -p

Show each commit with a patch, that describes the concrete changes of this commit.

git log --stat=250

Show each commit with a list of file paths that are changed. Also show the count of changed lines. =250 is the maxwidth and may be omitted.

git log --shortstat

Show each commit with an additional line, that summarizes the count of modified files, added lines and deleted lines.

git log affe567 ^^bcd1234 ^^tag42

Show revision affe567 and all ancestor revisions, but except the revision bcd1234 and its ancestors and except the revision of tag tag42 and all of its ancestors. On Linux a single ^ is sufficent, on Windows you need ^^.

git log affe..cafe

Show revision cafe and all ancestor revisions, but except the revision affe and its ancestors.

git log affe..

git log affe.. HEAD~3

git log affe..HEAD HEAD~3

Show all revisions in the current branch, but except the revision affe and its ancestors. If no revision follows .. immediately, then HEAD is implicitely added. HEAD~3 has no effect here because this rev is included in HEAD.

git log --grep="DR-360"

Show all revisions, whose commit messages contain DR-360 in the current branch.

git log --oneline --simplify-by-decoration

Show all revisions of the current branch as one line each containing a branch name or a tag.

git log --graph --oneline --simplify-by-decoration --branches --tags

Show all known branches and tags and their relations.

git log --no-walk rev1 rev2 rev3

Show only the three given revisions but not their anchestors.

git l master origin/master branch1 origin/branch1 --

Show the history of all four branches together.

git -P l -3

Show the youngest three revisions without using the pager (-P). Helpful if the pager is less.

git diff

git diff

Show unstaged changes.

git diff --cached

git diff --staged

Show the changes that you have staged, but not committed so far.

git diff 0ffe..affe --numstat

List the full paths of the changed files between revisions 0ffe and affe. Prepend each line by the count of lines added and removed to that file. A modified line is counted as one added and one removed line.

git diff 0ffe..affe --stat=1000,500 --compact-summary

List the full paths of the changed files between revisions 0ffe and affe. Append a histogram of the amount of changes at the end of each line.
1000 and 500 are just large numbers for the maximum line length and maximum file path length, that hopefully fit all situations.
--compact-summary just indicates after the file paths if a file is new etc.

git tag

git tag 0.0.0

Create a local tag on HEAD named 0.0.0

git tag -d 0.0.0

Delete the local tag named 0.0.0

git push origin 0.0.0

Push the tag 0.0.0 to the remote repo.

git push origin --delete 0.0.0

Delete the tag 0.0.0 from the remote repo.

With git pull only tags are pulled that do not exist locally yet.
Local tags will neither be updated nor dropped by git pull, so tags may remain different between the repositories even after git pull.
You have to drop all local tags before you do a git pull, if you want to be sure that you have exactly the same tags as on remote.

git remote

git remote

Show a list of existing remotes.

git remote -v

Show a list of existing remotes together with their fetch and push URLs.

git remote add remote URL

Add a remote with the name remote based on the URL URL. This was already a valid URL to a local repo: c:/Tmp/zeroMQtest/zeroMQtest/.git

git remote add origin https://github.com/mm65de/myproj.git

After creating the project C:\dev\proj\github\myproj with IntelliJ and committing all files to the local branch main and creating the private and empty project myproj via the GitHub website, the command above connects this local git repo with the GitHub project. The next command should be git push origin main to push all local changes to GitHub.

git remote remove remote

Remove the remote with the name remote.

git remote prune -n origin

git remote prune –dry-run origin

Show which remote branches do not exist on origin anymore and will be locally deleted, if you omit the option -n or --dry-run.

git remote prune origin

Delete locally all copies of remote branches, that do not exist anymore in the repo origin on server side.

git fetch & git pull & git push

git fetch

Fetch all changes and even new branches from the repo origin. Do not move any tags. Therefore there is no kind of any merge. Only fetch, pull and push work with remote repos. All other commands work on the local repo only, even if they show "remote" tags.

git fetch --all

Fetch all changes from all remote repos, not only from origin.

git fetch --prune

Same as fetch, but delete locally all copies of remote branches, that do not exist anymore in the repo origin on server side.

git fetch origin branch1:branch1

Fetch the newest revisions of the branch branch1 in the repo origin to your local copy of repo origin. Then fast forward this branch to the newest revision, if this can be done via fast forward. Report the operation as "rejected" if a fast forward is not possible, because of already committed local changes. For this operation your current branch must not be branch1.

git pull

Same as git fetch followed by git merge FETCH_HEAD. Incorporate changes from remote repo into the current branch. If the merge is successful, then there is an automatic commit.

git pull --no-commit

Don't perform any autocommit, if the merge succeeds.

git push

Update the remote branch according to the local one with the same name.

git push --all

Update all remote branches according to the local ones.

git push origin mybranch

Push my new branch mybranch to the remote repository origin, but do not set it as upstream branch.

git push --set-upstream origin mybranch

Push my new branch mybranch to the remote repository origin, and also set it as the upstream branch of my local branch.
This is the mostly used approach to publish new branches to remote repositories.

git push origin --delete feature/CRED-11585

Remove the remote branch feature/CRED-11585 from the remote repository origin.

git push ... --dryrun --verbose

Report in detail what the push without the --dryrun would do.

git branch

git branch

git branch --list

List all local branches.

git branch --list -v

List all local branches, their last commit comment and hash and optionally how much it differs from the remote tracking branch.

git branch --list -vv

Same as -v, but also show the remote tracking branch name.

git branch -r

List all remote branches.

git branch -r --list *CRED-111*

List all remote branches whose name contains the substring "CRED-111".

git branch -a

List all local and remote branches.

git branch --merged

List all branches that are merged into the current branch.

git branch --no-merged

List all branches that have not yet been merged into the current branch.

git branch feature/CRED-11585

Create the local branch feature/CRED-11585. To create it also in the remote repository, you need to use a git push command with the option --set-upstream.

git branch --set-upstream-to=origin/main main

Set origin/main as upstream branch of local branch main.

git branch -d feature/CRED-11585

Remove the local branch feature/CRED-11585.

git branch -D feature/CRED-11585

Remove the local branch feature/CRED-11585, even if it is not merged to master.

git branch -D -r origin/feature/CRED-11585

Remove the local copy of the remote branch origin/feature/CRED-11585 from the local repo.
If you want to remove the branch from the remote server, you have to use an according push command.

git branch -f mybranch 123affe

Set the branch mybranch to the revision 123affe, even if the branch mybranch already exists. If the revision is omitted, use HEAD instead.

git merge

git merge task/CRED-123

Merge the changes of branch task/CRED-123 into the current branch.

git merge --ff-only task/CRED-123

Merge the changes of branch task/CRED-123 into the current branch only if this can be done via a fast forward operation without creating an additional commit.

git stash

git stash

git stash push

Stash staged files away. Unstaged files remain untouched.

git stash push -u

Stash all the workspace modifications away (staged and unstaged). Workspace becomes clean HEAD.

git stash -m "my stash message"

Provide a special stash description instead of the automatically generated one.

git stash list

List the zero based stack of stashed change sets. With parameter -p you see modified lines.

git stash show

Show the list of the staged files in last stash set.

git stash show -u 2

Show the list of third youngest (2) stash set including also its unstaged (-u) files, if any.

git stash show -p 2

Show the modifications (-p) in the third youngest (2) stash set.

git stash apply 2

Merge the third youngest (2) stash set (potentially including also unstaged files) into the workspace.

git stash pop

Merge the youngest stash set into the workspace and remove the set from the stash stack.

git stash drop 2

Remove the third youngest (2) stash set from the stash stack.

$ git stash drop stash@{1}
Dropped stash@{1} (af8fdeee49)
$ git stash store -m"NewName" af8fdeee49

Rename the second stash (i.e. stash@{1}) to NewName via dropping and recreating it based on its former hash value.

git rebase

git rebase master

Set the current branch to the HEAD revision of master, then re-apply all the changes of the current branch, since it was branched from master.

How to restore linear git history after nonlinear merge

git cherry-pick

git cherry-pick ^b1 b2

git cherry-pick b1..b2

Pick all the changes contained in branch b2 but not in branch b1 and merge them to the branch of the current HEAD.