rootOfGitRepo='/path/to/gitRepository' gitScript="$rootOfGitRepo/.git/hooks/pre-commit" cat << ENDOFSCRIPT > "$gitScript" #!/usr/bin/env bash cat << ENDOFMESSAGENo more commits, sorry.ENDOFMESSAGE exit 1 ENDOFSCRIPT chmod +x "$gitScript" cat "$gitScript" "$gitScript"; echo $?
#!/usr/bin/env bash workDir=$(mktemp --tmpdir -d tempDir_XXXXXXXX) zerosFile="$workDir/zeros" otherFile="$workDir/other" getDirSize() { directory=$1 message=$2 echo -en "\t==>\tDirectory size [$message] : " du -sh $1 } countOccurrencesInGitLog() { fullPathToFile=$1 trackedFile=$(basename "$1") echo -en "\t==>\tNUMBER OF OCCURRENCES OF '$trackedFile' IN 'git log' : " git log --name-status | grep -c "$trackedFile" } # init cd "$workDir" git init getDirSize "$workDir" 'Empty Git repository' # create and commit "$otherFile" echo 'this is an other file' > "$otherFile" git a "$otherFile" git co -m 'this is an other file' getDirSize "$workDir" "After creating + committing the 'other' file" # create and commit "$zerosFile" dd if=/dev/zero bs=1K count=100 of="$zerosFile" getDirSize "$workDir" "'zeros' file created but not committed yet" git a "$zerosFile" git co -m 'Lots of zeros !!! (initial commit)' getDirSize "$workDir" "'zeros' file committed" # append data to "$zerosFile" + commit for i in {1..5}; do echo "hello world ($i)" >> "$zerosFile" git a "$zerosFile" git co -m "hello world ($i)" getDirSize "$workDir" "After hello world ($i) in 'zeros' file" done # cleaning the git repo git gc getDirSize "$workDir" "After 'git gc' (1)" # untracking "$zerosFile" git rm --cached "$zerosFile" git co -m "rm --cached 'zeros'" getDirSize "$workDir" "After untracking the 'zeros' file + commit" # what about the git log (1) ? countOccurrencesInGitLog "$zerosFile" # cleaning the git repo git gc getDirSize "$workDir" "After 'git gc' (2)" # removing the 'zeros' file rm "$zerosFile" getDirSize "$workDir" "After 'rm' of 'zeros' file" # cleaning the git repo git gc getDirSize "$workDir" "After 'git gc' (3)" # what about the git log (2) ? countOccurrencesInGitLog "$zerosFile" # cleaning before leaving cd .. [ -d "$workDir" ] && rm -rf "$workDir"
Initialized empty Git repository in /tmp/tempDir_FWkkVbkH/.git/ ==> Directory size [Empty Git repository] : 35K /tmp/tempDir_FWkkVbkH [master (root-commit) 599b54f] this is an other file 1 file changed, 1 insertion(+) create mode 100644 other ==> Directory size [After creating + committing the 'other' file] : 50K /tmp/tempDir_FWkkVbkH 100+0 records in 100+0 records out 102400 bytes (102 kB, 100 KiB) copied, 0.000523495 s, 196 MB/s ==> Directory size ['zeros' file created but not committed yet] : 150K /tmp/tempDir_FWkkVbkH [master daa0106] Lots of zeros !!! (initial commit) 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 zeros ==> Directory size ['zeros' file committed] : 155K /tmp/tempDir_FWkkVbkH [master bc46b8c] hello world (1) 1 file changed, 0 insertions(+), 0 deletions(-) ==> Directory size [After hello world (1) in 'zeros' file] : 162K /tmp/tempDir_FWkkVbkH [master 8ceab4f] hello world (2) 1 file changed, 0 insertions(+), 0 deletions(-) ==> Directory size [After hello world (2) in 'zeros' file] : 168K /tmp/tempDir_FWkkVbkH [master d35a527] hello world (3) 1 file changed, 0 insertions(+), 0 deletions(-) ==> Directory size [After hello world (3) in 'zeros' file] : 174K /tmp/tempDir_FWkkVbkH [master 7db7977] hello world (4) 1 file changed, 0 insertions(+), 0 deletions(-) ==> Directory size [After hello world (4) in 'zeros' file] : 182K /tmp/tempDir_FWkkVbkH [master 6cd929a] hello world (5) 1 file changed, 0 insertions(+), 0 deletions(-) ==> Directory size [After hello world (5) in 'zeros' file] : 188K /tmp/tempDir_FWkkVbkH Counting objects: 21, done. Delta compression using up to 2 threads. Compressing objects: 100% (19/19), done. Writing objects: 100% (21/21), done. Total 21 (delta 6), reused 0 (delta 0) ==> Directory size [After 'git gc' (1)] : 153K /tmp/tempDir_FWkkVbkH rm 'zeros' [master d68e9bd] rm --cached 'zeros' 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 zeros ==> Directory size [After untracking the 'zeros' file + commit] : 156K /tmp/tempDir_FWkkVbkH ==> NUMBER OF OCCURRENCES OF 'zeros' IN 'git log' : 9 Counting objects: 22, done. Delta compression using up to 2 threads. Compressing objects: 100% (14/14), done. Writing objects: 100% (22/22), done. Total 22 (delta 6), reused 21 (delta 6) ==> Directory size [After 'git gc' (2)] : 154K /tmp/tempDir_FWkkVbkH ==> Directory size [After 'rm' of 'zeros' file] : 53K /tmp/tempDir_FWkkVbkH Counting objects: 22, done. Delta compression using up to 2 threads. Compressing objects: 100% (14/14), done. Writing objects: 100% (22/22), done. Total 22 (delta 6), reused 22 (delta 6) ==> Directory size [After 'git gc' (3)] : 53K /tmp/tempDir_FWkkVbkH ==> NUMBER OF OCCURRENCES OF 'zeros' IN 'git log' : 9
. ├── dir1 ├── dir2 │ └── myScript ├── dir3 └── .gitThis was fine until today, but now I'd like myScript to live in its own distinct repository.
Cloning into '/path/to/new/repository'...
done.
Rewrite dd39933a75594ac29d780de568e872fa22e1a480 (45/62) (1 seconds passed, remaining 0 predicted) Ref 'refs/heads/master' was rewritten Ref 'refs/remotes/origin/master' was rewritten WARNING: Ref 'refs/remotes/origin/master' is unchanged
Using the vague word "get" on purpose because I still don't know whether its a matter of pull, fetch, or anything else .
Initialized empty Git repository in /dev/shm/playingWithGit/repo/.git/ [master (root-commit) 2d79b06] initial commit (on master) 1 file changed, 1 insertion(+) create mode 100644 myFile Switched to a new branch 'branch1' [branch1 407687e] commit from 'branch1' 1 file changed, 1 insertion(+) Switched to branch 'master' Switched to a new branch 'branch2' [branch2 8680357] commit from 'branch2' 1 file changed, 1 insertion(+) Switched to branch 'master' Switched to a new branch 'branch3' [branch3 4188c2c] commit from 'branch3' 1 file changed, 1 insertion(+) Switched to branch 'master' branch1 branch2 branch3 * master
Switched to branch 'branch1' hello world hello from 'branch1' Switched to branch 'branch2' hello world hello from 'branch2' Switched to branch 'branch3' hello world hello from 'branch3' Switched to branch 'master' hello world
Cloning into '.'... done.
* master
hello world
Branch 'branch1' set up to track remote branch 'branch1' from 'origin'. Switched to a new branch 'branch1' hello world hello from 'branch1' Branch 'branch2' set up to track remote branch 'branch2' from 'origin'. Switched to a new branch 'branch2' hello world hello from 'branch2' Branch 'branch3' set up to track remote branch 'branch3' from 'origin'. Switched to a new branch 'branch3' hello world hello from 'branch3'
branch1
branch2
* branch3
master
Already on 'master' [master 28b51d1] another commit on 'master' 1 file changed, 1 insertion(+) Switched to branch 'branch1' [branch1 0ff4301] another commit on 'branch1' 1 file changed, 1 insertion(+) Switched to branch 'branch2' [branch2 fcab109] another commit on 'branch2' 1 file changed, 1 insertion(+) Switched to branch 'branch3' [branch3 fcf5830] another commit on 'branch3' 1 file changed, 1 insertion(+)
Switched to branch 'branch1' hello world hello from 'branch1' I made this commit on branch 'branch1' after the 'clone' Switched to branch 'branch2' hello world hello from 'branch2' I made this commit on branch 'branch2' after the 'clone' Switched to branch 'branch3' hello world hello from 'branch3' I made this commit on branch 'branch3' after the 'clone' Switched to branch 'master' hello world I made this commit on branch 'master' after the 'clone'
remote: Enumerating objects: 20, done. remote: Counting objects: 100% (20/20), done. remote: Compressing objects: 100% (8/8), done. remote: Total 12 (delta 0), reused 0 (delta 0) Unpacking objects: 100% (12/12), done. From /run/shm/playingWithGit/repo 4188c2c..fcf5830 branch3 -> origin/branch3 407687e..0ff4301 branch1 -> origin/branch1 8680357..fcab109 branch2 -> origin/branch2 2d79b06..28b51d1 master -> origin/master
branch1
branch2
* branch3
master
Switched to branch 'branch1' Your branch is behind 'origin/branch1' by 1 commit, and can be fast-forwarded. (use "git pull" to update your local branch) thank you, Git hello world hello from 'branch1' Switched to branch 'branch2' Your branch is behind 'origin/branch2' by 1 commit, and can be fast-forwarded. (use "git pull" to update your local branch) hello world hello from 'branch2' Switched to branch 'branch3' Your branch is behind 'origin/branch3' by 1 commit, and can be fast-forwarded. (use "git pull" to update your local branch) hello world hello from 'branch3' Switched to branch 'master' Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded. (use "git pull" to update your local branch) hello world
branch1 branch2 branch3 * master Updating 5784923..5ccb801 Fast-forward myFile | 1 + 1 file changed, 1 insertion(+) Current branch master is up to date. hello world I made this commit on branch 'master' after the 'clone'
Switched to branch 'branch1' Your branch is behind 'origin/branch1' by 1 commit, and can be fast-forwarded. As seen above (use "git pull" to update your local branch) Ok, I'll do this !
Updating b5e84db..6f16d51 Fast-forward myFile | 1 + 1 file changed, 1 insertion(+) Current branch branch1 is up to date.
hello world hello from 'branch1' I made this commit on branch 'branch1' after the 'clone'
Already on 'branch1' Your branch is up to date with 'origin/branch1'. Already up to date. Current branch branch1 is up to date. hello world hello from 'branch1' I made this commit on branch 'branch1' after the 'clone' Switched to branch 'branch2' Your branch is behind 'origin/branch2' by 1 commit, and can be fast-forwarded. (use "git pull" to update your local branch) Updating 1e4eef9..56d7474 Fast-forward myFile | 1 + 1 file changed, 1 insertion(+) Current branch branch2 is up to date. hello world hello from 'branch2' I made this commit on branch 'branch2' after the 'clone' Switched to branch 'branch3' Your branch is behind 'origin/branch3' by 1 commit, and can be fast-forwarded. (use "git pull" to update your local branch) Updating 545c032..ab21a4f Fast-forward myFile | 1 + 1 file changed, 1 insertion(+) Current branch branch3 is up to date. hello world hello from 'branch3' I made this commit on branch 'branch3' after the 'clone' Switched to branch 'master' Your branch is up to date with 'origin/master'. Already up to date. Current branch master is up to date. hello world I made this commit on branch 'master' after the 'clone'
On branch master nothing to commit (use -u to show untracked files)I expected something like :
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
* master 6f3d70e (a very interesting commit message)should be :
* master 35ca599 [origin/master] (another highly interesting commit message)
fatal: No upstream configured for branch 'master'should be :
refs/remotes/origin/master
This is because the master local branch has no upstream origin/master remote branch to be compared to.
The presence or absence of an upstream setting mainly affects whether git status can tell you if you are ahead or behind, and whether git merge and git rebase can do their job with no additional parameters. So it's basically just a convenience setting.
* master 6f3d70e [origin/master: ahead 7] (a very interesting commit message)
refs/remotes/origin/master
On branch master Your branch is ahead of 'origin/master' by 7 commits.
This is dirty1000 and is actually not recommended.
Counting objects: 6, done. Delta compression using up to 2 threads. Compressing objects: 100% (6/6), done. Writing objects: 100% (6/6), 697 bytes | 0 bytes/s, done. Total 6 (delta 4), reused 0 (delta 0) remote: error: refusing to update checked out branch: refs/heads/master remote: error: By default, updating the current branch in a non-bare repository the error remote: error: is denied, because it will make the index and work tree inconsistent remote: error: with what you pushed, and will require 'git reset --hard' to match remote: error: the work tree to HEAD. remote: error: remote: error: You can set 'receive.denyCurrentBranch' configuration variable to remote: error: 'ignore' or 'warn' in the remote repository to allow pushing into the solution remote: error: its current branch; however, this is not recommended unless you remote: error: arranged to update its work tree to match what you pushed in some remote: error: other way. remote: error: remote: error: To squelch this message and still keep the default behavior, set a workaround remote: error: 'receive.denyCurrentBranch' configuration variable to 'refuse'. To gitServer:/path/to/repository ! [remote rejected] master -> master (branch is currently checked out) error: failed to push some refs to 'stuart@gitServer:/path/to/repository'
(nothing but UNIX FAILURE
exit code)
This directive is currently unset.
ignore
With Git 2.3.0+, it is possible to safely push to a non-bare repository with :
Counting objects: 6, done. Delta compression using up to 2 threads. Compressing objects: 100% (6/6), done. Writing objects: 100% (6/6), 697 bytes | 0 bytes/s, done. Total 6 (delta 4), reused 0 (delta 0) To gitServer:/path/to/repository f5c8ce7..629a87e master -> master
git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch ./test' --prune-empty --tag-name-filter cat -- --all
Rewrite de1291f7f10da919f611358539e3d2e0acfd632f (1/9)rm 'test' Rewrite 023a969f60328055b6cab2e1a82e307760b7a308 (2/9)rm 'test' Rewrite aaffedfe16c297d02c2fee5eb72b740c91cae942 (3/9)rm 'test' Rewrite 8a766a9226e360ba0f147ae9c890cec200f958d7 (4/9)rm 'test' Rewrite 1f9bd643aa3495f46ca9470c021a56f8a0765c62 (5/9)rm 'test' Rewrite 2e22a397f2283f398038d9a44f451914c0a58f68 (6/9)rm 'test' Rewrite 15f247b46d558b057ccead7f7980c7f24b58879c (7/9)rm 'test' Rewrite e54ff82df52d8e7d02749b17e4fac4aa9386122e (8/9)rm 'test' Rewrite 5bbcaf42adac870f13295b2bbe2673807bf1e059 (9/9)rm 'test' Ref 'refs/heads/master' was rewritten Ref 'refs/remotes/origin/master' was rewritten
http://stackoverflow.com/questions/1338728/delete-commits-from-a-branch-in-git
git reset --hard commitId WARNING, this deletes all commits from the current point back to commitId. Use with care !!!
git for-each-ref --format='delete %(refname)' refs/original | git update-ref --stdin git reflog expire --expire=now --all git gc --prune=now
Before starting this procedure, make sure there are no uncommitted changes on both repositories. commit or stash them accordingly.
Stuff below could be obsolete / wrong git clone https://github.com/sni/Thruk.git git remote add --track master upstream git://github.com/Httqm/Thruk.git git branch newFeature git checkout newFeature [do some code modifications, commit] git push upstream newFeature (?)
checkout is the SVN command to get a local copy of a project from its main repository (when joining a team, for instance).
Git also has a checkout command, but it has a different meaning.
The Git equivalent of SVN checkout is git clone.