Git Notes

Post tags: | git |

End of line handling

End of line handling
        Configure Git on OS X or Linux to properly handle line endings
        git config --global core.autocrlf input
        Configure Git on Windows to properly handle line endings
        git config --global core.autocrlf true
        For Mac
        git config core.whitespace cr-at-eol

git submodules

Create a submodule
        git submodule add -b master src/modules/trackchanges
        git submodule update --init
Update a submodule
        git submodule update --remote

Revert uncommitted changes including files and folders?

Revert uncommitted changes including files and folders?
         --- Revert changes to modified files.
        git reset --hard
         --- Remove all untracked files and directories.
        git clean -fd

Duplicating a repository

To create a duplicate of a repository without forking, you need to run a special clone command against the original repository and mirror-push to the new one.

In the following cases, the repository you’re trying to push to–like exampleuser/new-repository or exampleuser/mirrored–should already exist on GitHub. See “Creating a new repository” for more information. Mirroring a repository

To make an exact duplicate, you need to perform both a bare-clone and a mirror-push.

Open up the command line, and type these commands:
        git clone --bare
         --- Make a bare clone of the repository
        cd old-repository.git
        git push --mirror
         --- Mirror-push to the new repository
        cd ..
        rm -rf old-repository.git
         --- Remove our temporary local repository

If you want to mirror a repository in another location, including getting updates from the original, you can clone a mirror and periodically push the changes.

git clone --mirror
         --- Make a bare mirrored clone of the repository
        cd repository-to-mirror.git
        git remote set-url --push origin
         --- Set the push location to your mirror

As with a bare clone, a mirrored clone includes all remote branches and tags, but all local references will be overwritten each time you fetch, so it will always be the same as the original repository. Setting the URL for pushes simplifies pushing to your mirror. To update your mirror, fetch updates and push, which could be automated by running a cron job.

fetch updates and push
        git fetch -p origin
        git push --mirror

List files that have changed between 2 commits

git diff --name-only SHA1 SHA2

Add to .gitignore and then remove matching files Remove files in .gitignore from version control

If you have a file like xyz which is under version control, then even if you add an entry for the file in .gitignore, git won’t automatically remove it from version control. If you have just one file you can do that yourself with

git rm --cached xyz

However, what if you have hundreds of different files which you need to remove? The recipe is

git commit -m "commit message"

to commit the current situation, then remove all the files from git’s index and then add them back again with

git rm -r --cached .
        git add .
        git commit -m ".gitignore is now working"

Workflow expositions