Git
Renaming Master to Main
https://www.git-tower.com/learn/git/faq/git-rename-master-to-main/
# Rename the branch
git branch -m master main
# Push main to remote
git push -u origin main
# Go to your remote repository and set the default branch
# Delete the master branch on remote
git push origin --delete master
Pushing branch to remote
git push --set-upstream origin [branch]
Squash commits
When pushing a new feature, sometimes we'll squash the commit into one.
# Using interactive rebase
git log
# Count the number of commits done. In this example we use 3
git rebase -i HEAD~3
# on the interactive editor, squash except for the fist commit
Remove files from repo
https://stackoverflow.com/questions/2047465/how-do-i-delete-a-file-from-a-git-repository
If you want to remove the file from the Git repository and the filesystem, use:
git rm file1.txt
git commit -m "remove file1.txt"
But if you want to remove the file only from the Git repository and not remove it from the filesystem, use:
git rm --cached file1.txt
git commit -m "remove file1.txt"
Removing sensitive data from repo from all commits
Use git filter repo (this is Ubuntu repository)
Run the following command, replacing PATH-TO-YOUR-FILE-WITH-SENSITIVE-DATA with the path to the file you want to remove, not just its filename. These arguments will:
Force Git to process, but not check out, the entire history of every branch and tag
Remove the specified file, as well as any empty commits generated as a result
Remove some configurations, such as the remote URL, stored in the .git/config file. You may want to back up this file in advance for restoration later.
Overwrite your existing tags
$ git filter-repo --invert-paths --path PATH-TO-YOUR-FILE-WITH-SENSITIVE-DATA
Parsed 197 commits
New history written in 0.11 seconds; now repacking/cleaning...
Repacking your repo and cleaning out old unneeded objects
Enumerating objects: 210, done.
Counting objects: 100% (210/210), done.
Delta compression using up to 12 threads
Compressing objects: 100% (127/127), done.
Writing objects: 100% (210/210), done.
Building bitmaps: 100% (48/48), done.
Total 210 (delta 98), reused 144 (delta 75), pack-reused 0
Completely finished after 0.64 seconds.
Once this the file is removed, we can add it to .gitignore
We need to readd the remotes as git-filter-repo removes it
git remote add origin ---path to repo---
Next we have to force push it
git push origin --force --all
And do it for the tags as well if we have them
git push origin --force --tags
Update git submodules
If it's the first time you check-out a repo you need to use --init first:
git submodule update --init --recursive
For git 1.8.2 or above, the option --remote was added to support updating to latest tips of remote branches:
git submodule update --recursive --remote
This has the added benefit of respecting any "non default" branches specified in the .gitmodules or .git/config files (if you happen to have any, default is origin/master, in which case some of the other answers here would work as well).
For git 1.7.3 or above you can use (but the below gotchas around what update does still apply):
git submodule update --recursive
or:
git pull --recurse-submodules
if you want to pull your submodules to latest commits instead of the current commit the repo points to.
Rename author of a commit
From https://stackoverflow.com/a/28845565
The following is tested and working, unlike the linked answer. Assume for clarity of exposition that 03f482d6 is the commit whose author we are trying to replace, and 42627abe is the commit with the new author.
Checkout the commit we are trying to modify:
git checkout 03f482d6
Make the author change:
git commit --amend --author "New Author Name <New Author Email>"
Now we have a new commit with hash assumed to be 42627abe.
Checkout the original branch.
Replace the old commit with the new one locally:
git replace 03f482d6 42627abe
Rewrite all future commits based on the replacement:
git filter-branch -- --all
Remove the replacement for cleanliness:
git replace -d 03f482d6
Push the new history:
git push --force-with-lease
Checkout a specific tag
To get all the tags
git tag
git checkout tags/<tag> -b <branch>