GIT is usually hard to learn at the beginning. Below is a common workflow with commands that we use with git:
0. Initialize to create a .git folder inside working folder
Working folder is real folder that we manipulate real files (add file, add folder, delete file, rename-move file, edit file content), it is real working tree. With .git, we have created 2 more virtual folders named staging tree and repository tree.
Now we can start to add, delete and modify files inside working tree then commit those actions as change sets into virtual trees. Each change set is called a commit and has a unique ID. A commit is a change set of what happened on files, not files on its own.
.git is also where we can add hooks (advanced usage).
To view current status of project (usually called repository but actually include all 3 trees) and current log of commits, we use:
1. Add changes into stage tree
git add file-name-here
git add .
git add folder-name-here/*
. = all files changed
* = all changed files inside a folder
"git add" is used after we add new files, edit file content... in working tree, and would like to reflect those changes into stage. "git add" helps adding those changes into stage before we commit changes from there into repository.
2. Commit changes into repository tree
git commit -m "comment-message-here"
We can also specify individual files by using file name or ./* after git commit the same as "git add" usage above.
-m and following comment are required for remembering.
If we need to commit directly from working tree to repo then:
git commit -am "commit-message-here"
3. Compare changed files
git diff --staged
git diff is useful to show changes (differences in files) between working tree and repository
git diff --staged is for differences between staged files and repository
How about working tree and staged? 🙂
4. Of delete and rename/move files that are tracked inside repository
For example, we have created some new files inside working tree and staged as well as committed those changes (changes by actions such as having added new files OR having deleted files OR having renamed/moved files) into repo. Now we would like to delete some files from repo, we indeed delete them from working tree, then we have to reflect those changes into repo.
If we do those actions by OS commands inside working tree then we have to stage those changes before "git commit". But we usually use git commands for those actions then those changes are auto added into staging tree instead.
Git command to delete: git rm file-name-here
Git command to rename/move: git mv old-file-name new-file-name
So if we use OS's deletion such as DEL key... then it will not be reflected automatically into staging, we have to "git rm" or "git mv" after that. Not need to do that, we should use git commands right away. Then we can "git commit" changes.
(to be continued)