Changing Git history¶
Check Changes from Last Commit¶
git diff
git add -p
git rm
git mv
git checkout [-p] <file>
git restore <file>
git reset [-p]
git restore --staged <file>
git stash
git stash apply
git checkout -b new_branch HEAD~2
git reset --hard HEAD~3 # Go back in time, throwing away changes
git reset --soft HEAD~3 # Set HEAD to point to an earlier commit
git reset --hard # Wipe out differences in the working tree
git stash
git checkout -b new-branch HEAD~3 # head back in time!
git stash # because it's always a good thing to do
git reset --hard HEAD~3 # go back in time
git reset --hard HEAD@{1} # oops, that was a mistake, undo it!
git stash apply # and bring back my working tree changes
git commit --amend
git revert <commit tag>
Best Practices for Collaboration¶
- Always synchronize your branches before starting any work on your own.
- Make changes that are self-contained.
- Avoid having very large changes that modify a lot of different things. (Try to make changes as small as possible as long as they're self-contained.)
- When working on a big change, it makes sense to have a separate feature branch.
- Regularly merge changes made on the master branch back onto the feature branch.
- Have the latest version of the project in the master branch, and the stable version of the project on a separate branch.
- You shouldn't rebase changes that have been pushed to remote repos.
- Write commit messages.
- Only edit the same branch when necessary after submitting a pull request.
git remote -v
git rebase [-i]
git push [-f] [-u] origin branch_name
code improvement: clear/meaningful variable names GitHub
Configuration¶
git config [--global]