Posted by & filed under Dev Workflow. Edit: 26 July 2014

... (second part after "Common GIT commands").

During using git, there would be times when we need to revert actions or get "backup" files from 1 of 3 trees into another tree, this is for undo/revert some changes that we made into files unintentionally (or deliberately but not need anymore).

return undefined;
Copyright: nyuhuhuu

1. Checkout file from repo to working folder after we have made some wrong/unexpectedly changes with a working file.

git checkout -- file-name-here

-- is a safe way to distinguish file-name-here in repo from a whole branch with same name, branch is another usage of git.

2. Reset a staged file (un-stage) after we staged a file into staging tree, used when we would like to remove that file from staging.

git reset HEAD file-name-here

3. Amend last commit (most recent only) when we decided that there would be some more changes with last commit, OR more simpler we only need to use a new message for the commit.

git commit --amend -m "(new-)message-here"

4. Checkout files within a commit to stage

git checkout -- commit-id-here

-- is for safety. Commit-id can be found as an unique id assigned to commit, we use it to refer to commit we need, it should be at least 8-10 first characters of full SHA id.

Because this checkouts files into staging, so we can use "git reset..." for a file then that file would be cleaned from staging. And as we can see, not any changes would be made for working tree with this "git checkout" command, just staging.

5. Revert a commit, used when we need to revert all changes within a commit.

This is not very useful but could be used for a single last commit or a simple commit with some minor changes over a few, not so many, files.

git revert commit-id-here

git revert commit-id-here --in

With --in, changes are reverted into staging tree for us to commit and add message manually. If not, commit is auto and we can only modify message.

6. Reset to a specific point based on commit id, including status of 3 trees

git reset soft: just HEAD pointer is changed

git reset (mixed): (default) HEAD and staging are changed

git reset hard: HEAD and both working tree and staging are changed

(to be continued)



Comments are closed.