If you’ve staged more files than you intended with a ‘git add .’, if you haven’t committed yet you can selectively unstage files with:
git rm --cached path/filename
If you want to unstage multiple files in a folder, use the recursive flag
git rm --cached -r foldername/
If you’ve committed a number of git commits using a wrong user.name or user.email value, you can re-write previous commits with:
git filter-branch --commit-filter '
if [ "$GIT_COMMITTER_NAME" = "Old Name" ];
git commit-tree "$@";
git commit-tree "$@";
Note that this will re-write all commits on the current branch matching the if condition which may or may not be what you’re looking for, so be careful. push the changes back to your remote origin as needed.
This is based on answers to this question here.
By default, the git install on my MacOS 11.5 creates new projects with a ‘master’ branch, but GitHub which I push to for my remote projects has been creating new projects with a default branch of ‘main’ for some time now.
To move a master branch to main locally, set it as the default branch and then push to GitHub I use the following steps (taken from multiple places but mainly from this article):
Create a new main branch from master locally:
git branch -m master main
Add remote GitHub repo:
git remote add github https://your-repo-url
Pull remote changes from remote main branch
git pull github main
Set upstream branch to track main
git branch --set-upstream-to github/main
Push local to github main
git push github main
If you have changes in the new remote project (like a readme) that don’t exist locally yet and you get the error: “fatal: refusing to merge unrelated histories” then pull with the allow unrelated histories option:
git pull github master --allow-unrelated-histories
Now:’git push’ and you should be good.
For my local development these steps are enough for what I need, but if you have a master in your remote repo too and want to delete it there are additional steps in the article linked above.
Tags are useful to mark specific versions or releases of your code in a repo on a given branch. To tag everything on the current branch, use:
git tag new-tag-name
To push a new tag to a remote repo, you can either push all tags (which is not recommended):
git push remote-name --tags
or push a specific tag:
git push remote-name tag-name
More example in this SO question.