I must do more work to control change, and git helps me along the way. Here are my most basic uses of git as I organize commits in sequence.
- git add file.js
- To add a new file, or all changes in an existing file to the index
- git add -p file.js
- To add part of the changes in an existing file to the index
- git mv a/file1.js b/file2.js
- To move a file and add the move to the index
- git add .
- To add all unstaged files
- git commit
- To commit the index
- git commit -m "a message"
- To commit the index with a message
- git commit -a
- To add all unstaged files to the index and commit the index
- git diff
- To compare all unstaged (changed) files to the index (staged/added)
- git diff --cached
- To compare all files in the index (added) to the last commit (HEAD)
- git diff HEAD
- To compare all unstaged files to the last commit
- git diff --no-index file1.js file2.js
- To compare two files
- git diff HEAD~2:full/path/to/file1.js HEAD:full/path/to/file2.js
- To compare files from separate branches
With all these changes and differences, it's still possible I might want to undo something. The following commands may help.
- git commit --amend
- To rewrite the last commit to include the new index and a new message
- git revert HEAD
- To create a new commit that reverses the changes just committed.
- git reset --soft HEAD~
To reset the HEAD and the current branch to the previous commit
Moves the branch pointer but does not delete the index, working directory nor any commit
git reset HEAD
- git reset --mixed HEAD
To reset the current branch and index to the currently attached HEAD without changing the working directory
(undo for git add/rm (staging operations))
If the HEAD is detached, the branch pointer is also modified.
git reset commit
- git reset --mixed commit
- To reset the HEAD, current branch and index to all match a specific commit without changing the working directory
- git reset --hard HEAD
- To reset the current branch, index and working directory to the latest commit
git branch newBranch
git reset --hard HEAD~3
git checkout newBranch
- To move the last (for example 3) commits from the current branch to a new branch
- git help reset
- For more ideas about how to undo...