Print the total number of commits per day containing a pattern in git

Problem

You want to print a summary of commits in git that contain a certain pattern (‘number’) ordered by date and with the total number of commits per day

Solution

You can use the following for the patter ‘number’

git log --pretty=format:'%h %ad | %s%d [%an]' --date=short | grep 'number' | awk '{print $2}' | sort -r | uniq -c | awk '{ print $2,$1}'

The last awk command is to swap the date with the total number

The output should be something like the following

2024-06-18 2
2024-06-17 3
2024-06-14 1
2024-06-13 1
2024-06-12 1
2024-06-11 4
2024-06-10 3

Removing git branches from local repository

Problem

You would like to remove your local branches that were used to track remote branches that no longer exists (deleted after merge).

git branch -a
branch_to_delete
remotes/origin/branch_to_delete

Solution

To delete your local branch you could use the following:

git branch -D branch_to_delete

And to to delete the remote tracking branching you could use the following:

git branch -rd origin/branch_to_delete

git apply name_of_file.patch does not work

Problem

You are given a patch file to apply to your git repository but using git apply does not work. No errors reported but no changes applied neither.

Solution

Try to use the patch command command possibly with the -p1 parameter like:

patch -p1 < path_to_the_patch_file.patch

NOTE: Make sure that you are in the right folder relative to the file path in the patch file

SSL received a record that exceeded the maximum permissible length.

Problem

You want to install rbenv, but when you are trying to clone the code with:

git clone https://github.com/rbenv/rbenv.git ~/.rbenv

you get the following error:

fatal: unable to access 'https://github.com/rbenv/rbenv.git/': SSL received a record that exceeded the maximum permissible length.

Solution

If you are behind a proxy server make sure that you add your proxy server details in your ~/.bash_profile and source the file afterwards.

So first add the details of your proxy like:

export http_proxy=http://xxx.xxx.xxx.xxx:8080
export https_proxy=https://xxx.xxx.xxx.xxx:8080

And finally source the file before running the installation again:

source ~/.bash_profile

How to display the git last commit of every file in a list with ruby

Problem

You want to display the last commit of every file that you have in a list.

Solution

Let’s say that you have the following file list in an array:

file_list = ["Gemfile", "Gemfile.lock", ".ruby-version"]

in order to be able to get the last commits of each of the files in ruby do the following:

file_list.each do |f|
  puts %x[git log -n 1 #{f}]
end

Refreshing locally remote branches that have been deleted in git

Problem

You would like to refresh your local git repository after deleting some remote branches, usually after merging them to a develop,master etc repository, so doing branch -a or branch -r would not display them anymore.

Solution

You would need to issue the following git command:

git remote prune origin

And then doing a branch -r or branch -a should not display the deleted remote branches anymore.

fatal: cannot exec ‘/tmp/…/git-ssh.sh’: Permission denied – Capistrano, Dreamhost, permission denied for git-ssh.sh

Problem

When trying to use the new Capistrano 3.x to set up your rails project in a dreamhost account, you get the following error complaining that the git-ssh.sh script copied to your account by capistrano cannot be executed as the permission is denied:

fatal: cannot exec '/tmp/example.com/git-ssh.sh': Permission denied

Solution

It seems that Dreamhost, and quite possibly other hosting providers are not allowing executables from the /tmp directory, which is where Capistrano places the git-ssh.sh script. So in order to be able to execute the script you can change the directory where the script is copied in the first place and put it in your home directory. You can do that by adding the following to the config/deploy.rb file:

set :tmp_dir, "/home/dh_user_name/tmp"

Pushing and getting the tags from a remote git repository

Problem

You want to tag your branches in a remote repository and share the tags with other users, but usually the git pull and git push do not update the tags.

Solution

According to the git documentation:

By default, the git push command doesn’t transfer tags to remote
servers. You will have to explicitly push tags to a shared server
after you have created them. This process is just like sharing remote 
branches — you can run git push origin [tagname].

and:

If you have a lot of tags that you want to push up at once, 
you can also use the --tags option to the git push command. 
This will transfer all of your tags to the remote server that 
are not already there.

But when doing either a git pull origin [repo] or git fetch origin [repo] the tag list does not seem to be updated.

In this case try the following:

git pull origin [repo] --tags

And the list should contain all the tags in the remote repository.

Get github to link your commits to your user account if you have more than one git accounts

Problem

You would like to link the commits to a github repository to your github account but you may have different git accounts (ie gitolite etc).
So you would need to specify in the config file which account you want to use.

Solution

There is a help page in github that describes this problem here, but they recommend to change your global settings which is maybe not what you want.
So you could the following to change only the current project settings:

cd ~/my_project
git config --local user.email="your_github_account_email"

Or add the following to your .git/config file inside your local repository:

[user]
  email = user_name@domain.com
  name = Firstname Surname

And to check the settings:

git config --local -l
git config --global -l

Stop github asking your username

Problem
You are working with a git repository but after first cloning the project to your localhost and trying to push changes afterwards, github is asking for you username and password.

Solution
Edit your git configuration and make sure that it uses the git protocol instead of https, so in your ~/project/.git/config file

replace:

remote.origin.url=https://github.com/project_name/repository_name.git

with:

remote.origin.url=git@github.com:project_name/repository_name.git