Pagine

Monday, 12 June 2017

How to resolve some git errors

This post will help the readers a lot because the errors mentioned can occur very frequently.

Remote origin already exists

This error occurs when you already have a remote repository specified and the remote origin removed and added. Example:
$ git remote rm origin
$ git remote add origin https://gitserver.com/scm/myproject.git

Git push fails with rejected error

This error occurs because you didn't execute git pull before git push. Example:
$ git pull
$ git push

Git push fails caused by "fatal: The remote end hung up unexpectedly"

This one is common and you should check whether your remote URL is correct and Git has access to the remote repository. I suggest add (if not already setted) the origin; else set the correct origin.

Restoring a changed file to its last committed state

Run git checkout followed by the filename and you will lose your changes. However, this will be restored as a clean copy of the file. Example:
$ git checkout fileName

Unstaging a file

Did you run git add too soon? Run git reset HEAD followed by the filename to unstage it:
$ git reset HEAD fileName

How to fix the most recent commit message

The –amend option will edit a commit message:
$ git commit --amend
The editor will open to edit the last message.

Reset the most recent commit

There are two ways to do this: with and without your changes.
1) Without losing changes:
$ git reset HEAD~1
2) By losing changes:
$ git reset --hard HEAD~1

I found a bug after releasing the product but it was in the commit that I did a long time ago

In this case, you should not use git reset because it rewrites the history and the product is already released. Therefore, you should make a commit that reverts the buggy commit and pushes it to
share with your colleagues:
$ git revert 17cc9433abcf23c42aef6b9aad24b5fcfc353f7c

There are many garbage files in the working directory. How to delete them?

In this situation, the files are not maintained by Git, so you have to use git clean:
1) To check the files that will be removed
$ git clean -n
2) Now remove them:
$ git clean -f

I think I made a mistake while resolving conflicted files. How do I restore it to the state just after git merge?

To do this, you can use git checkout:
$ git checkout --merge fileName

Gitindex file is corrupt

No need to worry. This one is rare but can be annoying! Git will display the error as "bad index file sha1 signature: fatal: index file corrupt". You just have to remove the backup index file; remove it, and then reset the repository:
$ mv .git/index .git/indexOLD

$ git reset

Git refuses to start a merge/pull command.

The typical error messages look like this:
  • Error: The myfile.txt entry is not up to date; it cannot be merged.
  • Error: The myfile.txt entry will be overwritten by the merge command.
To resolve this, perform the following steps:
1. Stash the changes or throw them out:
$ git stash save "a message"
$ git checkout myfile.txt
2. Check that the changes are staged:
$ git status
3. Bring the changes from the remote repository:
$ git pull
4. Repopulate if you made a stash:
$ git stash pop

Friday, 9 June 2017

How to use git bisect

The git bisect command allows you to run a binary search through the commit history to find a commit that has an issue. For example, you pulled the last commits and the website isn't working anymore. You know that before the last pull everything was okay. So you have to find the commit ID before it crashes and the last ID after the pull:
$ git bisect start
$ git bisect bad <commit_id_after_pull>
$ git bisect good <commit_id_before_pull>
Now, the bisecting loop begins and Git will check for an alternative commit.
Reset the given commit and tell Git whether the website is working:
$git bisect <bad_commit_id>
Git will search again and again to find which commit crashed the website:
$ git show <commit_id>
Instead of trying to locate a bug inside all your files, you have a shortened list of files.

How to reset a git commit via bash script 

If you don't want to reset to a given commit and test your software, you can create a bash script and tell Git to use it. The script has to return 0 if the condition is fulfilled, and nonzero if it isn't.
check_file.sh

#!/bin/bash
FILE=$1
If [ -f $FILE];
then
    exit 0;
else
    exit 1;
fi
Now it's time to run git bisect to specify the last 10 commits:
$ git bisect start HEAD HEAD~10
$ git bisect run ./check_file.sh MyFile.java

Bisect algorithm

The algorithm used by git bisect always returns the commit that is at the middle position of the array.
In the following diagram, you will see how the algorithm found the good commit in three steps:
  1. The Commit6 option is selected by Git. After checking it, you tell Git that it's good.
  2. Git reduces the array because if Commit6 is good, then every commit between Commit1 and Commit6 is good too.
  3. Then Git asks you to test Commit9, and you say that it's good too.
  4. So the error can only be inside Commit10 and Commit11.

Thursday, 8 June 2017

How to solving merge conflits with git

When you are working with several branches, a conflict will probably occur while merging them. It appears if two commits from different branches modify the same content and git isn't able to merge them. If it occurs, Git will mark the conflict and you have to resolve it.
For example, Developer1 modified the myfile.txt on a feature branch and Developer2 has to edit it on another branch. When Developer2 merges the two branches, the conflict occurs.
Git will tell you to edit the file to resolve the conflict. In this file, you will find the following:
<<<<<<< HEAD
Changes from Developer2
=======
Changes from Developer1
>>>>>>> 5df2fcdc89e0a33fcf8cbbbbd89cb8eabbdb61b7
The <<<<<<< characters indicate the start of the merge conflict, the ====== characters indicate the break points used for comparison, and >>>>>>> indicate the end of the conflict.
To resolve a conflict, you have to analyze the differences between the two changes and merge them manually. Don't forget to delete the signs added by Git. After resolving it, simply commit the changes.
If your merge conflict is too complicated to resolve because you can't easily find the differences, Git provides a useful tool to help you.
The "git diff" and "git mergetool" help you to find differences. Other tools to visually compare and merge files are:

Wednesday, 7 June 2017

How to configure git alias

Git allows you to use aliases for command lines. The alias concept is similar to linux. Setup git alias is very fast: in your $HOME edit the .gitconfig file and add the following text:
[alias]
    l = log --pretty=format:"%C(yellow)%h\\ %ad%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate --date=short
Now if you run the following command:
$ git l
see the output of alias "l". Now you are ready to create a new customer alias or add other aliases available in web.

Examples of aliase are:

    l = log --pretty=format:"%C(yellow)%h\\ %ad%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate --date=short
    a = add    
    ap = add -p
    c = commit --verbose
    cm = commit -m
    cam = commit -a -m
    d = diff
    ds = diff --stat
    dc = diff --cached
    s = status -s
    co = checkout
    cob = checkout -b
    cmp = "!f() { git add -A && git commit -m "$@" && git push; }; f"
    la = "!git config -l | grep alias | cut -c 7-"

Tuesday, 6 June 2017

How to rewrite commit history with git

Someone accidentally commits a huge binary file with a thoughtless git add ., and you want to remove it everywhere. Perhaps you accidentally committed a file that contained a password, and you want to make your project open source. filter-branch is the tool you probably want to use to scrub your entire history. The --tree-filter option runs the specified command after each checkout of the project and then recommits the results. 

Examples:

1) To remove a file named mypasswords.txt from your entire history, you can use the --tree-filter option to filter-branch: 
$ git filter-branch --tree-filter 'rm -f mypasswords.txt' HEAD
Rewrite 802a096d79b9b5872544a6b35d6a52cbdf55274a (21/21)
Ref 'refs/heads/master' was rewritten 
2) If you want to remove all accidentally committed editor backup files, you can run something like
$ git filter-branch --tree-filter 'rm -f *~' HEAD.

--index-filter option

The --index-filter is another option similar to the tree filter but does not check out the tree, which makes it much faster.
$ git filter-branch --index-filter 'git rm --cached --ignore-unmatch myfilename.txt' HEAD
The --ignore-unmatch option is needed because "git rm" will fail if the file is absent from the tree.

Monday, 5 June 2017

Canceling a commit with git

The git revert command allows you to cancel your last unpushed commit. The type of cancel is only logical because Git doesn't drop phisically the commit but it creates a new commit that executes the opposite of your commit. A pushed commit is irreversible, so you cannot change it.

Example

The following example illustrates how to remove last pushed commit. First step, let's have a look at the last commits:
$ git log
commit dc2d9f3deda6f49902c1879b7b861fac51683865
Author: infoamodomio <infoamodomio@infoamodomio.com>
Date:   Thu May 25 12:19:01 2017 +0200
    Added a new file
Second step, We want to cancel the dc2d9f… commit:
$ git revert dc2d9f
Canceling this commit isn't necessary to enter the full commit ID, but just the first characters. Git will find it, but you will have to enter at least six characters to be sure that there isn't another commit that starts with the same characters.

Thursday, 1 June 2017

How to edit a commit with git

It is possible three actions to edit a commit:
  1. Change the comment;
  2. Add one or more files in a commit;
  3. Remove one or more files in a commit.

Examples

1) If you want to edit the description of your last commit, use the following line: 
 $ git commit --amend
2) Let's suppose that your last commit contains buggy code; you can specify that your changes on the file are part of the last commit:
 $ git add file.txt
 $ git commit -v -amend
3) Now I want to remove a file I included accidentally in the last commit (because this file deserves a new commit):
 $ git reset HEAD^1 file.txt
 $ git commit --amend -v
 $ git commit -v file.txt