This is the beginnings of a 'cheat sheet' for commonly used git commands.
Branching model: http://nvie.com/posts/a-successful-git-branching-model/
Handling merge conflicts: http://www.gitguys.com/topics/merging-with-a-conflict-conflicts-and-resolutions/
Using the 'rebase' command: https://www.atlassian.com/git/tutorials/merging-vs-rebasing/workflow-walkthrough
How to undo (almost) anything with Git: https://github.com/blog/2019-how-to-undo-almost-anything-with-git
Squash several Git commits into a single commit: http://makandracards.com/makandra/527-squash-several-git-commits-into-a-single-commit
http://wildlyinaccurate.com/a-hackers-guide-to-git/
https://git-scm.com/docs/git-reset
http://www.alexkras.com/19-git-tips-for-everyday-use/
Web view with gitphp
Reverting a commit
- In the ordinary case you want to generate a new commit that reverts the changes with
git revert <commit>
If you haven't pushed your commit yet, then there are a few options.
Do not do this if you have already pushed your commit!
- Amend the existing commit with
git commit --amend
.
- Revert the most recent commit:
git reset HEAD~
If you have pushed the commit upstream, but if there are no other users of the brach
- you can do some surgery on it and rewind the history by
git reset --hard <commit>
followed by git push origin -f
To undo changes on your local repo, and set your branch to exactly match the remote branch: git fetch origin
followed by git reset --hard origin/master
Branching
- List all branches:
git branch
- Switch to a branch:
git checkout <branch>
- Create a new local branch with
git branch <branch>
- Add branch to the remote repository:
git push origin <branch>
- Delete a local branch with
git branch -d <branch>
- Delete a local branch even if it has unmerged changes (throws them away),
git branch -D <branch>
- Delete a remote branch with
git push origin --delete <branch>
- Rename the current branch
git branch -m <branch>
- Compare two branches, from the HEAD of the first branch to the HEAD of the second
git diff branch1..branch2
- Compare commits on two branches
git log branch1..branch2
- Compare the change in a branch2 from the common ancestor of branch1 and branch2
git diff branch1...branch2
- Compare a specific file between two branches
git diff master..feature -- <file>
- To rename a branch,
git branch -m old_branchname new_branchname
followed by git push origin :old_branchname new_branchname
Might also need git push --set-upstream origin new_branchname