commit


Forcing user to remove trailing spaces in git

There are simple ways to force the user in removing trailing spaces before committing code in git using hooks.
Below we will present a solution that is applied at the local computer before the commit stage, which each developer needs to perform in each repository clone they own.
Note: If you would like to have the hooks on the server, you will need extra access rights to modify the hooks on the remote machine, maybe you will even need your systems administrator to configure it.

Solution

Attached you will find a file named pre-commit ([download id=”3933″]) it is a hook that get applied before the user is allowed to even commit.
That file you need to copy it (after you extract it) in the .git/hooks folder of your cloned repositories and you are done!

What this script does is simple, if there are whitespace errors, it prints the offending file names and fails.
What are considered whitespace errors is controlled by core.whitespace configuration.
By default, trailing whitespaces (including lines that solely consist of whitespaces) and a space character that is immediately followed by a tab character inside the initial indent of the line are considered whitespace errors.

In case you need to commit files that have whitespace errors, you can bypass the checks that are applied by the hooks using the --no-verify flag as follows:

git commit -m "Some informative message" --no-verify;

There are more ways to achieve this result, others are more verbose but this one is the simplest and more flexible as you can configure it using the git configuration variables.

[download id=”3933″]

#!/bin/sh
#
# This hook script verifies that there are no whitespace errors in the files to be committed

if git rev-parse --verify HEAD >/dev/null 2>&1
then
 against=HEAD
else
 # Initial commit: diff against an empty tree object
 against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi

# Redirect output to stderr.
exec 1>&2

# If there are whitespace errors, print the offending file names and fail.
exec git diff-index --check --cached $against --

[download id=”3933″]


How to undo a Git commit that was not pushed 7

To undo a Git commit that was not pushed, you are given a few major options:

  1. Undo the commit but keep all changes staged
  2. Undo the commit and unstage the changes
  3. Undo the commit and lose all changes

Method 1: Undo commit and keep all files staged

In case you just want to undo the commit and change nothing more, you can use


git reset --soft HEAD~;

This is most often used to make a few changes to your latest commit and/or fix your commit message. Leaves working tree as it was before reset.
soft does not touch the index file or the working tree at all (but resets the head to the previous commit). This leaves all your changed files Changes to be committed, as git status would put it.

Method 2: Undo commit and unstage all files

In case you want to undo the last commit and unstage all the files you can use the following


git reset HEAD~;

or


git reset --mixed HEAD~;

mixed will reset the index but not the working tree (i.e., the changed files are preserved but not marked for commit) and reports what has not been updated. This is the default action.

Method 3: Undo the commit and completely remove all changes

The following method will undo the commit and revert all changes so that your state is exactly as it was before you started making changes.


git reset --hard HEAD~;

hard resets the index and working tree. Any changes to tracked files in the working tree since the previous commit are discarded.

 

Note: In case you just want to rewrite the commit message, you could use git –amend instead.


git: How to move locally committed (but not pushed) changes to a new branch 2

Recently, we’ve been working on a certain branch, we did some changes and performed a couple of commits that were not pushed on the remote system.

There was a complication and it was decided that the local changes should not be pushed to the branch that we were working on.
Rather, they changes should go to a new branch which eventually will be merged.

As mentioned above, we already had done some changes and we already had performed the commits.

git status would give us the following:

$ git status;
On branch scanner_pdu_parser_master
Your branch is ahead of 'origin/scanner_pdu_parser_master' by 2 commits.
  (use "git push" to publish your local commits)

So, we needed to change the branch for those local commits.

Solution – Move the local commits to a new branch

First we got the name of the current branch using the command:

git branch;

Then, we switched to a new local branch

git checkout -b banana_peeler;

And, we pushed the local branch to the remote system:

git push --set-upstream origin banana_peeler;

Afterwards, we switched back to the previous branch

git checkout apple_peeler;

And reset it back to its original form, removing our local commits from it:

git reset --hard origin/apple_peeler;

Please note that the last command will delete all changes that are not committed as well.
In other words, any file you modified and did not commit or push, they will be reverted back to the original code as well.