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>