Showing posts with label Git. Show all posts
Showing posts with label Git. Show all posts

Thursday, August 22, 2019

Git error: cannot spawn .git/hooks/post-commit: No such file or directory


This error could happen when on the Windows.

error: cannot spawn .git/hooks/post-commit: No such file or directory

To fix it, simply add this as the first line of the file .git/hooks/post-commit:

#!/bin/sh


Friday, May 17, 2019

Git: rename a branch


$ git branch -m oldbranch newbranch

If newbranch already exists and you want to force the remove, use option -M.

Wednesday, March 20, 2019

Git: undo the most recent commits


Use the command git revert to revert several most recent commits.

For example, we have a file name onefile with 4 commits.

$ git log
commit 5a289042bb5d90c2780b0c55a34879f6853cc495
Author: JL <jl@solezero.com>
Date:   Wed Mar 20 19:04:07 2019 -0400

    D4

commit cc2c4931fc2c0422c26557801104d2924a568617
Author: JL <jl@solezero.com>
Date:   Wed Mar 20 19:03:54 2019 -0400

    C3

commit a0c17ecd71a595e3ae4fee92f72fc50d8797e898
Author: JL <jl@solezero.com>
Date:   Wed Mar 20 19:03:31 2019 -0400

    B2

commit 4f0f011379740c959cac1da2355f56c6de8b5523
Author: JL <jl@solezero.com>
Date:   Wed Mar 20 19:03:13 2019 -0400

    A1


If we want to remove the 2 most recent commits and go back to commit a0c17ecd71a595e3ae4fee92f72fc50d8797e898 (with comments B2), we can run the command:

$ git revert --no-edit a0c17ecd71a595e3ae4fee92f72fc50d8797e898..HEAD

With the option --no-edit, we are not prompted to input comments for every commit being reverted.

New commits are automatically added to revert the last 2 changes.

$ git log
commit 1a705ec2d7fffa5ebb57724372f41ccd3e228a5c
Author: JL <jl@solezero.com>
Date:   Wed Mar 20 19:19:23 2019 -0400

    Revert "C3"

    This reverts commit cc2c4931fc2c0422c26557801104d2924a568617.

commit 9e34a2cfac47034ecc28a1f0f71d60d413bc6055
Author: JL <jl@solezero.com>
Date:   Wed Mar 20 19:19:23 2019 -0400

    Revert "D4"

    This reverts commit 5a289042bb5d90c2780b0c55a34879f6853cc495.

commit 5a289042bb5d90c2780b0c55a34879f6853cc495
Author: JL <jl@solezero.com>
Date:   Wed Mar 20 19:04:07 2019 -0400

    D4

commit cc2c4931fc2c0422c26557801104d2924a568617
Author: JL <jl@solezero.com>
Date:   Wed Mar 20 19:03:54 2019 -0400

    C3

commit a0c17ecd71a595e3ae4fee92f72fc50d8797e898
Author: JL <jl@solezero.com>
Date:   Wed Mar 20 19:03:31 2019 -0400

    B2

commit 4f0f011379740c959cac1da2355f56c6de8b5523
Author: JL <jl@solezero.com>
Date:   Wed Mar 20 19:03:13 2019 -0400

    A1


We can verify that the current version is the same as the commit a0c17ecd71a595e3ae4fee92f72fc50d8797e898 (B2)

$ git diff a0c17ecd71a595e3ae4fee92f72fc50d8797e898


Wednesday, January 9, 2013

Git: to discard changes


If the changes in the working directory are not committed, we can run:
  $ git checkout -b BranchToBeDiscarded
  $ git add .
  $ git commit -a -m "commit the changes to an abandoned branch"
  $ git checkout master

The first three commands check in the changes in the working directory to a temporary branch. That makes the working directory clean so that we can use the last command to update the working directory by the HEAD of the master branch.

If you know confidently that the changes are not wanted, you can run:
  $ git checkout -- .
which permanently purge the changes.

If the changes in the working directory are committed, we can run:
  $ git revert <last-commit>

There is also a dangerous way of doing it if you want to remove the last commit completely. You can run:
  $ git reset <the-commit-to-be-the-new-HEAD>
and any commits after that one will gone.

Tuesday, January 3, 2012

Git for Windows


I was looking for a version control system that runs on Windows recently. Git seemed to be a very good candidate. It is a distributed system which works very well and naturally on a standalone machine. The Windows flavor is called msysgit. I downloaded the portable version of it. It is packed by 7zip and does not require installation. Just extract the files and folders to a new folder you want. Is that what people call Green Software? I love that. No need for the Administrator privilege and the worry of messing up the Windows system.

After the extraction, there are two batch files. One is for the Linux users and the other is for the Windows users. Running the batch for the Linux users opens up a terminal with a Shell-like working environment. You can even use Vim in it. That is amazing. It is not obviously how to access the files on the harddisk. I played with it for a moment and found out cd /c would bring you to the root of disk C:.

Then you go to where your source code locates and run git init to set up a repository. Git creates a hidden directory .git there. It holds all the version information for your project. By running git add and then git commit, you have done the first submission of the project. git diff can find out what have been changed. But Git also has a strange taste to provide another command git whatchanged to show the differences between commits.
 
Get This <