overview
Common patterns when using git.
repositories
initializing a repository
- Go to the project’s directory
git init
- Go to GitHub and create repository
git remote add <repository name> <GitHub URL>
git add .
git commit -m "Initial commit."
git pull <name> main
– Only needed if anything was added to GitHub repository directly (like README).
git push --set-upstream <name> main
renaming repositories
git mv <old> <new>
git commit
git push
git remote rename <old> <new>
git remote set-url --add <new> <URL>
git remote set-url --delete <new> <old URL>
- GitHub >
repository > Settings > Rename Rename local directory
cleaning a remote repository
git remote prune origin
branches
common branching workflow
create a new branch and do some work
git switch --create hotfix
– Create and switch to branch hotfixgit push --set-upstream <remote> hotfix
– Create hotfix branch on remote- Do work as normal.
merge the new branch back into main
git switch -
orgit switch main
– Switch to branch maingit merge hotfix
– Merge branch hotfix into maingit branch --delete hotfix
– Delete branch hotfix locallygit push <remote> --delete hotfix
– Delete branch hotfix on remotegit commit -m "Merged hotfix into main"
git push
Renaming Branch “master” to “main”
git branch -m master main
git push -u origin main
- GitHub > Settings > Branches > master > main > Update
git branch -u origin/main main
- GitHub > Branches > master > Delete this branch.
delete a remote branch
git push -d origin <branch-name>