this needs to be cleaned up
![]() |
(on Wikipedia)
https://git-scm.com/ [ 1 ]
A version control system of some note.
I use it, not that I know what I’m doing.
See also:
- My git repository for git scripts
- Git on Windows
-
tig (text-mode interface for git)
TODO – learn how to do change reviewing
Repository hosting websites:
—
-
2018-12-13 – 2.11.0 on Windows 10 on Windows Subsystem for Linux on Debian 9.5
- 4.4.0-17134-Microsoft #471-Microsoft Fri Dec 07 2018 x86_64 GNU/Linux
- 2018-12-13 – 2.11.0 on Devuan_ascii_2.0.0-rc_i386_dvd-1
- 2016-03-29 – 2.6.2 on Slackware 14.1
-
2016-03-26 – 1.9.1 on Lubuntu 14.04.4 LTS
- 1 Tools
- 2 Usage and troubleshooting
- 2.1 Create a new repository
- 2.2 Identifying yourself
- 2.3 Tagging
- 2.4 Download everything fresh
- 2.5 Revisionism
- 2.6 Change the “Author” entry in the changelog
- 2.7 Repository maintenance – compression / optimization
- 2.8 Actually add files
- 2.9 Undo a git add (before doing a git commit)
- 2.10 Ignoring files
- 2.11 Synchronize clone with its repository file creation times
- 2.12 View the last checked-in version
- 2.13 Compare local edits
- 2.14 Compare local edit to remote change
- 2.15 Split a directory into its own repository
- 2.16 Clone a repository from genesis to a specific commit into its own repository
- 2.17
git fsckshowsdangling tree - 2.18 example
.gitattributes - 2.19 Remove “warning: LF will be replaced by CRLF in”
- 2.20 Fix
ESC[33mandESC[mand restore colour - 2.21 Fix the default git push
- 2.22 “error: Your local changes to the following files would be overwritten by merge:”
- 2.23 readlink function not implemented
- 2.24
.git/confighasurl =with an email address as a username (contains an at (@) symbol - 2.25
git gcgives “suboptimal pack - out of memory“ - 2.26
git gcgives rename errors - 2.27 Export the repository as a zip archive
- 2.28 Ignore everything, but allow certain file extensions
- 2.29 Append a second git repository
- 2.30 Poor performance on Windows
- 3 Stop warning messages
- 4 Resources
Tools ∞
-
gitk– While rather awful, it lets you do at least basic things.
Usage and troubleshooting ∞
-
2009-01-16 – Having trouble cloning a repository? I got
chdir“permission denied” errors. I had to installgit-core.- (distribution not recorded)
git statusis your friend.-
Check out a single remote file
git checkout origin -- FILENAME
Create a new repository ∞
new project
new_git_directory='new_git_directory' \mkdir "$new_git_directory" \cd "$new_git_directory" # You can also do "git config --global" for all projects going forward \git config user.name "-" \git config user.email "-" # Compatibility with GitHub \git config init.defaultBranch main \git init
Identifying yourself ∞
Stuff will go in .git/config
If you screw this up, even after the fact, see Change the “Author” entry in the changelog.
For only the current repository ∞
git config --global user.email "you@example.com" git config --global user.name "Your Name"
For all repositories that aren’t specifically-set ∞
git config --global user.email "you@example.com" git config --global user.name "Your Name"
Tagging ∞
git tag "v1.3" git push --tags
This provides a new package to download.
Rename a tag ∞
I am told it is the following, but for me it only deleted the old tag!
OLD_TAG="some text" NEW_TAG="some text" git push origin refs/tags/"$OLD_TAG":refs/tags/"$NEW_TAG" :refs/tags/"$OLD_TAG" git tag -d "$OLD_TAG"
Download everything fresh ∞
Downloading everything fresh. Maybe after accepting a pull request.
git pull git@github.com:spiralofhope/shoes-contrib.git master
Revisionism ∞
- git rewrite history – remove filenames from specific path? – Stack Overflow
-
- The BFG is a simpler, faster alternative to git-filter-branch for cleansing bad data out of your Git repository history
- The author is retarded:
-
You can rewrite history in Git – don’t let Trump do it for real!
Trump’s administration has lied consistently, to make people give up on ever
being told the truth. Don’t give up: https://www.theguardian.com/us-news/trump-administration
Change the last commit message ∞
git commit --amend
Change an older commit message ∞
Unexplored:
https://docs.github.com/en/pull-requests/committing-changes-to-your-project/creating-and-editing-commits/changing-a-commit-message [ 2 ] [ 3 ] [ 4 ] [ 5 ]
Go back in time – checkout a previous version ∞
git log git checkout <code>
to return:
git pull origin HEAD
Roll back all files to a previous commit ∞
git reset --hard <tag/branch/commit id>
If you wish to commit that state, so remote repository also points to rolled back commit do:
git push <reponame> -f
You can also do something simple like roll back to the previous commit:
git reset --hard HEAD~1
Roll the repository back a little ∞
Screwed up a commit and want to roll back a little?
git rebase -i HEAD^^
.. where the number of carats (^) is the number you wish to roll back.
Then within the editor, delete any lines you want to excise from git’s history. Each line represents a particular commit.
Be aware that this can delete files from the disk.
Forgot to check in an old revision of a file ∞
See also https://stackoverflow.com/questions/3895453/how-do-i-make-a-git-commit-in-the-past
Well there’s no easy way. It’s probably doable but I can’t find a solution.
The closest I get is an incomplete solution: https://web.archive.org/web/20181009092446/http://blog.xkoder.com:80/2009/06/06/git-tutorial-part-ii-sharpen-you-git-fu-with-10-more-commands/
Find the commit hash before the file you want to modify, then check it out.
git checkout COMMIT_HASH
Make your changes, then commit them.
git commit -a
FIXME … then supposedly there’s a way to propagate the changes back to the master’s HEAD. Fucked if I know how. The above link doesn’t actually explain that. I am disappoint.
Go back to the present.
git checkout master
Remove all trace of a file ∞
Change the “Author” entry in the changelog ∞
For git log, change the editor field.
Renaming/deleting tags on GitHub ∞
Deleting using the GitHub UI:
- Visit your repository
-
Find the row:
Commits | Branch | Releases | Contributor, clickreleases- You will see a list of all releases.
- At the top-left,
Releases | Tags, click Tags - Click on the name of one of the releases
-
At the top-right,
Edit release | Delete
Synchroninzing your github tag deletion to your local git repository:
git tag -l | xargs git tag -d git fetch
Renaming a tag in a local git repository:
NEW_TAG_NAME=newtag OLD_TAG_NAME=oldtag git tag $NEW_TAG_NAME $OLD_TAG_NAME git tag -d $OLD_TAG_NAME git push origin :refs/tags/$OLD_TAG_NAME git push --tags
Restore a file deleted from git ∞
List all deleted files ∞
git log --diff-filter=D --summary
Un-commit a single deleted file ∞
git checkout -- filename.ext
Roll back an individual file ∞
git log filename.ext
git checkout <revision> filename.ext
For example:
git log filename.ext
commit 875465be775031545f9c7f89as8025373df63e23
Author: AUTHORNAME
Date: Sun Jul 19 01:20:11 2015 -0700
.
commit 8409d2b3aaff1276c1266748397fe3d6e9576961
Author: AUTHORNAME
Date: Thu Jul 2 10:54:37 2015 -0700
misc, theme updates, media locally cached
git checkout 4d0f80012abfe9d074db841ae36f07f0722f31c8 -- filename.ext
Why there isn’t a git checkout -1 -- filename.ext – type command is beyond me. I think maybe that’s HEAD~1 but I haven’t tested this.
I was told the right command was git checkout <filename> -1 but that didn’t work..
There is also git reset
See also:
- https://stackoverflow.com/questions/2733873/how-can-i-revert-a-single-file-to-a-previous-version [ 6 ]
- https://stackoverflow.com/questions/215718/how-can-i-reset-or-revert-a-file-to-a-specific-revision [ 7 ] [ 8 ]
-
https://thecodeartist.blogspot.com/2015/04/git-prevision-checkout-previous-version-of-file.html
- This does not work for me (git version 2.7.4)
Repository maintenance – compression / optimization ∞
I don’t understand these things, but they do stuff and things and that’s good. Probably.
git fsck
git gc
(gc will fix shit that fsck finds)
Actually add files ∞
I don’t know what fucking idiot would have git add -A not work as expected, but this seems to work:
git rm --cached -r directory git add directory
Undo a git add (before doing a git commit) ∞
git reset
Ignoring files ∞
If you want to remove all trace of a file, including from git history, see Remove all trace of a file.
https://git-scm.com/docs/gitignore
$HOME/.config/git/ignore
$GIT_DIR/info/exclude
.gitignore
example .gitignore
# Lines beginning with "#" are ignored (they are comments) # Something very specific: /directory/file # When the beginning / is absent then the pattern is relative to this .gitignore: directory/file # All files and directories from within that directory: /directory/** # Any file called "filenames" in any directory/subdirectory: /**/filenames # A file in the same place as this .gitignore: file # A file in the root of the git repository: /file # Some advanced stuff: [fF]ile ?ile
Synchronize clone with its repository file creation times ∞
https://github.com/tst2005/git-timesync/
(archived)
When you make a git clone the created file are the current date. This script allow you to change the file timestamp to the same than the repository one.
View the last checked-in version ∞
Say you’ve been editing a file and you want to look at the version of the file you last checked-in.
git show HEAD:filename.ext
Compare local edits ∞
git show HEAD:filename.ext | \wc --bytes
- Compare the change between commits
commit1="HEAD" commit2="HEAD^" filename="filename.ext" source=$( git show "$commit1":"$filename" | \wc --bytes ) target=$( git show "$commit2":"$filename" | \wc --bytes ) \echo "( $source - $target )" | \bc
Compare local edit to remote change ∞
Changes to all files ∞
git diff
-
Difference between remote and local file
git diff origin/master -- FILENAME- Ignore whitespace with
git diff -b origin/master -- FILENAME
-
Difference between two commits
- e.g. this commit and the previous one:
git diff HEAD HEAD^
- e.g. this commit and the previous one:
-
Difference between local and remote repositories
git diff master origin/master
If you use an external tool like Diffuse, then instead use git difftool.
tkdiff is fine. git difftool origin/master -- FILENAME
Changes to a specific file ∞
git diff origin/master:./ -- README.md git diff HEAD^:./ -- README.md git diff stash@{0}:./ -- README.md git diff 1A2B3C4D:./ -- README.md
Visually ∞
Not tested, vi is insane:
git difftool
It will start your diff app automatically for each changed file.
Windows: If you did not set a diff app, you can do it like in the example below(I use Winmerge):
git config --global merge.tool winmerge git config --replace --global mergetool.winmerge.cmd \ "\"C:\Program Files (x86)\WinMerge\WinMergeU.exe\" -e -u -dl \"Base\" -dr \"Mine\" \"$LOCAL\" \"$REMOTE\" \"$MERGED\"" git config --global mergetool.prompt false
Split a directory into its own repository ∞
# Be at the top level of your repository existing_repo_name="TODO" new_branch_name='TODO' new_repo_name='TODO' directory_to_split='TODO' \git subtree split --prefix="$directory_to_split" -b "$new_branch_name" \cd .. \mkdir "$new_repo_name" \cd "$new_repo_name"` \git init \git pull ../"$existing_repo_name" "$new_branch_name" \cd ../"$existing_repo_name" \rm -r "$directory_to_split" \echo "$existing_repo_name" >> .gitignore \git branch -d "$new_branch_name" # Force: # \git branch -D "$new_branch_name" \mv "$new_repo_name" "$directory_to_split" # If it bitches about being ahead of origin/main or some such, then: # git branch --unset-upstream
old
2017-08-30
folder=my_folder branch=master git filter-branch \ --prune-empty \ --subdirectory-filter "$folder" \ "$branch" \ ` # `
You may need to edit .git/config to update url or other settings.
If you intend to host this repository on GitHub, then follow their new repository creation documentation and see also GitHub’s documentation on splitting a subfolder out into a new repository.
Clone a repository from genesis to a specific commit into its own repository ∞
commit=1a96512f4b2e58aed7eaf1e0c04e17dedd194dbc directory_existing_repository=existing directory_new_repository=new user_name=username repository_name=repository git clone --no-checkout $directory_existing_repository $directory_new_repository cd new-repo-name git checkout $commit git remote remove origin git filter-branch -- --all git branch -m master main git switch main git remote add origin https://github.com/$user_name/${repository_name}.git git push -u origin main # list branches with # git branch # if you want to upload all branches: # git push -u origin --all
git fsck shows dangling tree ∞
git fsck
example
Checking object directories: 100% (256/256), done. Checking objects: 100% (9845/9845), done. dangling tree 128e5356316731484ca660822ddd4a981b142773 dangling tree b2adbe017562c32ff31f49946b18c6af23571427 dangling tree 6bb337c6e8f591529c1a628e805c6c6f9a14e0dd dangling tree 8941163d26e37602010799648002db75a0efd0b5 dangling tree b7ce9b153e11f6b123e818aa246f81a072d5689b dangling tree 6cf417ae0cf811c982171c2082d4ff2c747477aa
This was seen after I did a Split a directory into its own repository.
I’ve now worked on that repository for some time now, without addressing this.
Failed solutions:
-
git gc
Notes:
- https://git-scm.com/book/en/v2/Git-Internals-Maintenance-and-Data-Recovery
- https://stackoverflow.com/questions/16955523/git-broken-links-missing-dangling-trees
example .gitattributes ∞
*.lua filter=trimTrailingSpaces *.xml filter=trimTrailingSpaces *.toc filter=trimTrailingSpaces
Remove “warning: LF will be replaced by CRLF in” ∞
git config core.safecrlf false
Fix ESC[33m and ESC[m and restore colour ∞
git config --global core.pager "less -r"
Fix the default git push ∞
Cloned something and want to fix its default push?
git remote rm origin git remote add origin git://github.com/spiralofhope/shoes.git
.. hrm, not working right, try editing .git/config and changing the url line to something like git@github.com:spiralofhope/random-shoes.git
“error: Your local changes to the following files would be overwritten by merge:” ∞
git checkout HEAD^ FILENAME
readlink function not implemented ∞
error: readlink("path/to/filename"): Function not implemented
error: unable to index file path/to/filename
fatal: updating files failed
git config core.symlinks true instead of git config core.symlinks false got rid of the error for me. Consequently, the symlinks I created using mklink in Windows were pushed without error, but when cloning the project again, the symlinks become text files that says where the original files are located instead of working as a real symlink.
Tested 2016-07-20 with 2.9.0.windows.1 – it seems to work.
.git/config has url = with an email address as a username (contains an at (@) symbol ∞
If git push fails because you’re trying to use an email address as a username, which is set in your .git/config file, then..
Tested 2019-09-05 with Git 2.11.0 using GitHub.
Use %40 instead of the @ in your email address. E.g. if your username is email@example.com you would instead use email%40example.com like so:
[remote "origin"]
url = https://email%40example.com@github.com/githubusername/repository-name.git
git gc gives “suboptimal pack - out of memory” ∞
If doing a git gc you get:
warning: suboptimal pack - out of memory fatal: Out of memory, malloc failed (tried to allocate 125098831 bytes) error: failed to run repack
git config pack.windowMemory 256m
If you wish to avoid this problem in the future, try performing the git gc after every significant change.
git gc gives rename errors ∞
This will occur if you use sudo or if you are trying to work with the files within Linux when they are hosted on Windows. This was reproduced on a Linux guest inside of a VirtualBox Windows 10 host, and fixed by performing the command from within Windows 10 in Windows Subsystem for Linux.
Export the repository as a zip archive ∞
# Branch # e.g. master git archive --output=../zipfile.zip <BRANCH> # Commit git archive --output=../zipfile.zip <COMMIT> # Tag git archive --output=../zipfile.zip <TAG>
-
You can find the revision ID with
git log.
Ignore everything, but allow certain file extensions ∞
For .gitignore
# Ignore everything: * # Actually allow subdirectories: !/* # Actually allow these file extensions in any subdirectory: !*.ext
Test with:
\nano ./.gitignore ; \git add --dry-run --verbose **/*
Append a second git repository ∞
https://stackoverflow.com/questions/69869039/how-do-i-append-a-second-git-repository
Reportedly:
Yes it is possible, just change the origin in the local git config. Switch back to the original and push into the changes which have occurred since you left.
git config --local --edit
After you initialize the new repository and push up the current code.
Poor performance on Windows ∞
2024-08-14 on Windows 11
The Windows 11 notification panel (windows-n) has “Using an I/O intensive operation like git on your” and has a down arrow next to the day of the week to expand it. That still doesn’t show the whole phrase, but it’s likely:
Using an I/O intensive operation like git on your Windows drives will have poor performance. Consider moving your project files to the Linux file system for better performance. Click below to learn more.
The notification went away and I can’t get it back, but there was a “View Docs” button that didn’t do anything. I think clicking that dismissed the notification.
I have no idea what that’s supposed to mean or how Git would be worse. There is no “Linux file system”, does that mean Windows Subsystem for Linux 2?
Stop warning messages ∞
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in
\git config --global core.autocrlf input
Also look into using .gitattributes
Consider dos2unix or unix2dos.
(sed)
sed -i 's/\r//' filename
Resources ∞
Footnotes
- was http://git.or.cz [ ↩ ]
- was https://docs.github.com/en/github/committing-changes-to-your-project/creating-and-editing-commits/changing-a-commit-message [ ↩ ]
- was https://docs.github.com/en/github/committing-changes-to-your-project/changing-a-commit-message [ ↩ ]
- was https://docs.github.com/en/free-pro-team@latest/github/committing-changes-to-your-project/changing-a-commit-message [ ↩ ]
- was https://help.github.com/en/github/committing-changes-to-your-project/changing-a-commit-message [ ↩ ]
- was https://stackoverflow.com/questions/2733873/reverting-a-single-file-to-a-previous-version-in-git [ ↩ ]
- was https://stackoverflow.com/questions/215718/how-do-i-reset-or-revert-a-file-to-a-specific-revision [ ↩ ]
- was https://stackoverflow.com/questions/215718/how-can-i-reset-or-revert-a-file-to-a-specific-revision [ ↩ ]


More notes.. I hate git.
Restructured.
Using an email address as a username in `.git/config`.
added notes on comparing different commits (contents and sizes)
Remove “warning: LF will be replaced by CRLF in”
Mo’ git mo’ problems.
tidy
reference exporting a zip
added Export a repository at a specific commit as a zip archive
.gitignore is so stupid..