Git
Enforce LF for line endings
Using LF for line endings, even if you might develop on Windows, will support easy cross-platform, cross-IDE development. This can help in particular when you're using WSL on Windows.
Global git setting git config --global core.autocrlf input or edit %USERPROFILE%\.gifconfig:
[core]
autocrlf = input
If you want to force LF line endings for other users of your repository, create a .gitattributes file in your repository base:
* text=auto eol=lf
As a one-time conversion, you can normalize line endings for files that have already been committed previously:
git add --renormalize .
git commit -m "Normalize all the line endings"
Optional: Create an .editorconfig file:
[*]
end_of_line = lf
insert_final_newline = true
Create and checkout a new branch
git checkout -b <branch>
Delete branch locally and remotely
git branch -d <branch> # Delete local branch
git push -d <remote> <branch> # Delete remote branch
git branch -dr <remote>/<branch> # Delete local remote-tracking branch
git fetch <remote> -p # Delete all local remote-tracking branches (--prune)
Create an empty branch
git checkout --orphan <branch>
git reset --hard
Clear the working directory
git rm --cached -rf .
Rename master to main
git branch -M main
Import local git repository to GitHub
Create new, empty repository in GitHub e.g. https://github.com/username/Repo.git
Line 2 simultaneously renames
mastertomainto follow new best practices
git remote add origin https://github.com/username/Repo.git
git branch -M main
git push -u origin main
Reset last commit
git reset --hard HEAD^
Create a tag
git tag -a v1.5 -m "Tag description" -s
Remove -s if you don't want to sign your tag
Push tags
When you create a local tag, it isn't pushed by default, so use --tags:
git push origin --tags
Get the “current” tag
git describe --tags --abbrev=0
Fetch latest version of branch without checking out
git fetch <upstream> <branch>:<branch>
Fetching submodules
git submodule init
git submodule update
Ownership
You might get this warning from git, and the Git panel in Visual Studio may not show that you're in a Git repository:
fatal: unsafe repository ('C:/path/to/repo' is owned by someone else)
This could happen if the repository was checked out by a different user on your machine. You can take ownership (from an elevated Administrator terminal)
takeown /R /F 'C:/path/to/repo'
Alternatively, you can trust the repo:
git config --global --add safe.directory C:/path/to/repo
Subtrees
Subtrees are an alternative to modules.
git subtree add --prefix relative-subtree-path https://github.com/username/repo-name.gif main --squash
Then to update the subtree:
git subtree pull --prefix relative-subtree-path https://github.com/username/repo-name.gif main --squash