GitLab: Git alias for creating a merge request on push

Git allows you to set push options when pushing a branch to the remote.
You can use this to build an alias that automatically pushes a branch and creates a merge request for it.

Put this in your ~/.gitconfig in the [alias] section:

mr = push origin HEAD -o merge_request.create -o merge_request.draft

Now you can do git mr and a draft merge request will be created.
Target branch is your project's default branch, i.e. main or master.

To specify a different target branch, add -o merge_request.target=other-branch.

[There...

How to configure case insensitive git output

Git commands like diff use the less binary for their output representation.

I often find myself searching for strings like todo, then switching to the case-insensitive mode (-i) and re-doing my search.
Today I figured out that you can configure git to show case insensitive diffs every time:

git config --global core.pager 'less -i'

Git: auto-stashing to avoid conflicts on pull

First, decide if you want to pull with rebase. There are some implications of changing this, so think before you do it.


If you do, you can tell git to stash automatically before pulling:

git config --global rebase.autoStash true

Now on pull, git will a) stash, b) pull and c) reapply your working tree. Note that applying your changes may lead to unresolvable conflicts!

Further see a common git config with this configuration enabled.

git: How to always pull with rebase

In order to have more human readable git branches, do

  git pull --rebase

To always pull like this, write these lines to your ~/.gitconfig:

[pull]
   rebase = true

..or use this oneliner

git config --global pull.rebase true

Note that this will break if you pull from other upstream branches like

git pull origin other-branch

If you keep rebasing by default, you can get "merge pulls" like this:

git pull --no-rebase origin other-branch

Git: Diff staged changes

Saying git diff only shows unstaged changes relative to the index (or HEAD if the index is empty, alternatively any hash or branch you supplied) but leaves out files you already staged for the next commit.

To diff your added changes with HEAD, say:

git diff --cached

Effectively, this gives you the changes you will commit when you run git commit without the -a switch.

Git: Keep your repository tidy

When you're using feature branches, they will stack up if you don't delete them after the merge to master. Here's how to tidy them up.

Delete feature branches

Find already-merged branches by running

# On branch master
git branch --merged

You may safely delete each of the listed branches, because they point to commits that are contained in the history of your current branch (i.e. master).

git branch -d my/feature-branch # Delete feature branch locally
git push origin :my/feature-branch # Push *nothi...

Git: creating and deleting a tag

Git has two kind of tags:

  • annotated
  • lightweight

Annotated tags are stored as full objects in the Git database. They’re checksummed; contain the tagger name, email, and date; have a tagging message; and can be signed and verified with GNU Privacy Guard (GPG)

The following command will create a (lightweight) tag:

git tag v0.1

An annotated tag is created by:

git tag -a v0.1 -m "a special tag message"

A normal git push will not push the tag. So in order to publish your (local) tags, you have to

git push --tags
`...

Bundler: Packaging gems into the git repository (offline installation)

Installing gems on a server that has no access to the internet (especially rubygems.org) requires to bundle the gems into the repository itself. This requires to adjust the bundle config in the repository.

  1. Execute the following commands to configure bundler:
bundle config set --local path vendor
bundle config set --local disable_shared_gems true

Note

For Bundler < 2 you have to omit the "set": bundle config --local name value.
See here: [https://bundler.io/v1.17/man/bundle-config.1.html](https://bundler.io/v1.17/man...

How to get the git history of a file that does not exist anymore

If you want to see the git history of a project file, that doesn't exist anymore, the normal git log <path_to_file> won't work. You have to add certain flags to make it work:

git log --all --full-history -- <path_to_file>

How to: "git log" with renamed files

While renaming a file sometimes feels like "dropping its history", that is not true: Just use git log --follow on renamed files to access their full history.

Given a file "bar" that was previously named "foo":

touch foo
git add foo
git commit -m "Add foo"
mv foo bar
git add bar
git commit -m "Rename foo to bar"

git log bar

commit adc8e6a05b65355359c4e4618d6af0ed8f8b7f14 (HEAD -> git-follow)
Author: Michael Leimstaedtner <makmic@makandra.de>
Date:   Wed May 12 08:49:37 2021 +0200

    Rename foo to bar

git lo...

Git: How to add changes matching a regular expression

When you have many changes, and you want to spread them across different commits, here is a way to stage all changes matching a given regular expression for a single commit.

Example

Consider the following git diff output.

diff --git a/file1.rb b/file1.rb
index 806ca88..36d536b 100644
--- a/file1.rb
+++ b/file1.rb
@@ -1,7 +1,5 @@
-# Here is a useless comment.
-# It will be removed.
 class File1
-  def foo
+  def bar
     # ...
   end
 end
diff --git a/file2.rb b/file2.rb
index 550e1c6..600f4e3 100644
--- a/file2.rb
+++ b/file2...

Did you know 'tig status' ?

It's like a GUI for the famous git add [-p].

Select files with the up/down-keys and hit

  • u for staging/unstaging the whole file
  • Enter for showing the diff of a file
    • j and k to navigate in the diff
    • u again to stage/unstage chunks
    • 1 to stage/unstage only lines
    • \ to split large chunks
  • F5 to refresh the view

Git: What to do when "git log" does not show commits that touch a file

Let's say you have commits that change a file (and looking at the commit details show you the changes, etc). Now, when you do a git log you see them, but when you say git log that.file these commits don't show up? This is for you.

The reason is that the file got deleted deleted/re-added/renamed (any or all of those).

Instead of ...

git log master -- some.file

... you need to say:

git log --follow master -- some.file

Then your commits will show up.

Note that tig also understands --follow.


Kudos to Julien...

How to not leave trailing whitespace (using your editor or Git)

There is no reason to leave trailing whitespace characters in your project's files, so don't add any.

A git diff --check will tell you if there are any and you should not commit when you see them. So go ahead and switch your editor/IDE to automatically remove them for you.
Below are a few instructions on how to get them removed by your favorite IDE or editor.

Note that except for RubyMine, the following changes will remove trailing white-space on all lines, not only those that you changed.
While this should not be a problem if your proje...

Git: Merge a single commit from another branch

This is called "cherry-picking".

git cherry-pick commit-sha1

Note that since branches are nothing but commit pointers, cherry-picking the latest commit of a branch is as simple as

git cherry-pick my-feature-branch

Be aware that cherry-picking will make a copy of the picked commit, with its own hash. If you merge the branch later, the commit will appear in a history a second time (probably without a diff since there was nothing left to do).

Also see our advice for [cherry picking to production branches](https://makandraca...

Git: Search for text in all branches

To find a version containing the regular expression foo in the history of any branch:

git grep foo $(git rev-list --all)

You may also limit the search to a file extension, e.g. Ruby files (.rb) like this:

git grep foo $(git rev-list --all) -- *.rb

Updated: Git: removing feature branch on merge

  • Resolve @{-1} to actual branch name. (Happens when merging "-".)

Git: How to get a useful diff when renaming files

tldr; Use git diff -M or git diff --find-renames when you've moved a few files around.

Usage

$ git diff --help
  Options:
    -M[<n>], --find-renames[=<n>]
      Detect renames. If n is specified, it is a threshold on the similarity index
       (i.e. amount of addition/deletions compared to the file’s size). For example,
       -M90% means Git should consider a delete/add pair to be a rename if more than
       90% of the file hasn’t changed. Without a % sign, the number is to be read as
       a fraction, with a decimal point...

How to set git user and email per directory

I am using git at several places: at work, at university, and at home. I want an own git user/email for each of those places, but I don't want to care setting the values each time I create or clone a new repository.

Git allows for setting user/email per repository, globally and system-wide, but I need a fourth dimension: per directory. By setting git environment variables using ondir, I managed to get exactly what I need.

  1. Install ondir (on a Mac: brew install ondir, other OSs need to install it from the linked page).
  2. Put these ...

Git blame: How to ignore white-space modifications

When doing a git blame, git will blame the person who added or removed white space in a line (e.g. by indenting), not the person who originally wrote the code.

Say git blame -w to ignore such white-space changes. You want this. \
Note that you can also use it when diffing: git diff -w.

Example

Consider this method, created by a user in commit d47bf443:

def hello
  'world'
end

^

$ git blame foo
d47bf443 (Arne Hartherz 2012-12-19 14:44:38 +0100 1) def hello
d47bf443 (Arne Hartherz 2012-12-19 14:44:38 +0100 2...

Git: Change author of a commit

Using git rebase can be painful but luckily you can resort to cheating with git reset and committing anew.
Now what if you wanted to rebase commits of other people and still wish them to be the authors of their code? Easy: make them the author of a commit you made.

When you have freshly staged changes that are ready to be committed, just use the --author switch:

git commit -m "Hello Universe" --author="Philip J Fry <someone@example.com>"

If...

Capistrano 3: How to deploy when a firewall blocks your git repo

Sometimes, through some firewall or proxy misconfiguration, you might have to deploy to a server that cannot access the git repository.

Solution 1: HTTP Proxy (this is the preferred fix)

SSH can be tunneled over an HTTP Proxy. For example, when the repo is on github, use this:

  1. Install socat

  2. Add a ~/.ssh/config on the target server(s) with permission 0600 and this content:

    Host github.com ssh.github.com
      User git
      Hostname ssh.github.com
      Port 443
      ProxyCommand socat - PROXY:<your proxyhost>:%h:%p,...
    

Git: When committing, check the diff

When committing, you should always check the diff of your changes so you don't include any leftovers or irrelevant/bad changes. Any time spent on that is well invested as it saves you rejected merge requests or any mess you need to clean up later.

While you can just use git diff or git diff --cached (to diff staged changes), you can also have Git include the changes as a comment below the commit message:

git commit -v

That will open up the usual commit "file" in your preferred text editor, but it will include a diff below the s...

How to fix: "git diff" does not show umlauts (or other non-ASCII characters) properly

When using git diff, you might encounter weird characters where umlauts (or any other UTF-8) characters should be. It looks like this:

R<C3><BC>ckg<C3><A4>ngig # should be "Rückgängig"

However, not Git is to blame but the less command that Git uses to page your diff output.

You need to tell less to use UTF-8 (otherwise it tries to convert multibyte chars like "ü" which is represented by 2 bytes C3 and BC).

$ LESSCHARSET=UT...