Branches
Rename a branch
- Rename the local branch
git branch -m oldname newname
- Push the renamed branch
git push origin newname
- Delete the remote branch
git push origin --delete oldname
Create a new, empty branch
git checkout --orphan BRANCHNAME
git rm -rf .
Rewriting history
Delete pushed commits
Never do this if other people are using the repo!
If you wish to delete all commits after a certain one:
- Get the hash of the last commit you want to keep:
git log -p
- Do a hard reset to that commit
git reset --hard 2221651
- Do a force push to delete the remote commits
Always specify the branch if you have more than one branch!
git push --force
Delete a file completely from git history
https://help.github.com/articles/remove-sensitive-data/#using-filter-branch
cd /path/to/repo
git filter-branch --force --index-filter \
'git rm --cached --ignore-unmatch PATH-TO-YOUR-FILE-WITH-SENSITIVE-DATA' \
--prune-empty --tag-name-filter cat -- --all
git push origin --force --all
git push origin --force --tags
Merging
Merge specific files from one branch to another, without history
http://stackoverflow.com/a/1355990/399105
Workflows
Sample feature branch workflow
These instructions are based on a feature branch workflow: https://www.atlassian.com/git/tutorials/comparing-workflows/feature-branch-workflow
Rebasing instead of merging is used on the feature branch for cleaner project history: https://www.atlassian.com/git/tutorials/merging-vs-rebasing
- Pull the latest updates to the repo
cd /path/to/repo git pull
- Create a new branch named after the feature
git checkout -b FEATURE-NAME
Ex:
git checkout -b feature-add-logout
- Do work in the new branch, committing as needed
git add /path/to/file git commit
- Push work periodically to the repo
git push -u origin FEATURE-NAME
Ex:
git push -u origin feature-add-logout
(The first push will start tracking the feature branch. After than, you can just use
git push
) -
Discuss changes with other users of the repository
- Prepare feature branch for merging
git checkout master git pull git checkout FEATURE-NAME git pull git rebase master
- Merge feature branch
- To merge your branch and keep all commits:
git checkout master git merge FEATURE-NAME git push
- To add work to the main branch as one commit (losing all of the individual commit logs):
git checkout master git merge --squash FEATURE-NAME git commit -m "Commit message for your feature" git push
- To merge your branch and keep all commits:
- If you’re finished with the feature branch, you can delete it
- Delete the branch locally
git branch -d FEATURE-NAME
- Delete the branch remotely
git push origin --delete FEATURE-NAME
- Delete the branch locally