Git

description: How to use Git

lang: ENG

Commits

Create a commit

git commit

Create a commit wit a message

git commit -m "<message>"

Create a commit as a fixup

git commit --fixup=<commit>

Create a commit with a sign-off

git commit -s

Signing a commit with a GPG key

git commit -S [KEYID]

Amend a previous commit

git commit --amend

Branch

List branches

git branch [--all]

Create a branch

git branch <name> [origin branch]

Delete a branch without forcing

git branch -d <name>

Delete a branch with forcing

git branch -D <name>

Switch to a branch

git switch <name>

Switch to a new branch

git switch -c <name> [origin]

Switch to a branch with a detached head

git switch -d <commit or branch>

Tags

There is two types of tags:

Create an annotated tag

git tag -a [tag]

Create an annotated tag

git tag -a [tag]

Create a lightweight tag

git tag [tag]

Replace a tag (--force)

git tag -f [tag]

Delete a tag

git tag -d [tag]

Rebase

Rebase a branch

git rebase <rebase onto> [from]

Rebase in interactive mode (optionnaly with fixup squashing)

git rebase --interactive  [--autosquash] [range]

Rewrite commit while rebasing

git rebase --interactive [range]
# For each stage
git reset HEAD^
git commit ...

Continue a rebase

git rebase --continue

Abort a rebase

git rebase --abort

Rebase conflicts

First, it is better to use diff3 to get the parent content and see what the issue is. In a rebase, you are trying to apply the commits from a branch as patches into a target branch. The conflict happens when the branch has been modified on both branches. All the following examples are coming from codeinthehole.

  1. Show conflicting commit
git rebase --show-current-patch
# On git < 2.17 do
# git am --show-current-patch
  1. Use the REBASE_HEAD pseudo ref to show various stuff:
git show REBASE_HEAD # View the current commit
git rev-parse REBASE_HEAD # Show the sha of the commit
  1. Check the diff with the target branch:
git diff REBASE_HEAD...<target_branch> -- <FILEPATH>
  1. Check the log history:
git log REBASE_HEAD..<target_branch> -- <FILEPATH>
  1. Resolve conflict directly between master and example
<<<<<<<< HEAD
I like apples and pears
|||||||| merged common ancestors
I like apples
========
I love apples
>>>>>>> working-branch

If you want to keep the change from the target branch (HEAD), you can use:

git checkout --ours -- <FILEPATH>

If you want to keep the one of the branch you are rebasing (working-branch), you can use

git checkout --theirs -- <FILEPATH>

Cherry pick

Cherry pick one commit

git cherry-pick <commit>

Remote

Add a remote

git remote add <name> <url>

Remove a remote

git remote remove <name>

Update a remote

git remote set-url <name> <new_url>

Prune branches from a remote

git remote prune <remote>

Patch

Produce a patch

git format-patch <branch or commit range> --stdout > <name>.patch

Use a patch

git am <name>.patch

Co Authored patch

Co-authored-by: author-name <[email protected]>

Resources