January 8, 2017

Version Control with Git

What is Version Control?

Version Control is a program that tracks changes of files over time by virtue of version or revision which is a running numbering system.

Consider this scenario :

When you started you project and save it the first time, that is version #1.
When you worked on the project again and save it for the second time, that is version #2. And, so on.

Version Control allow you to see the differences between each revision, when it was made and who did it. It also allow you to have a backup (you normally save your project to a remote server). Also, when you mess up, you can also easily get back to a previous working version.



Learn the Lingo!

Most version control systems have similar concepts. Lets go with Git.

Terms

1. Remote Repository (repo) : The remote server which we stored our files.

2. Local repo/Working Copy : Our local directory of files, where you make changes.

3. Head: The latest revision in the repo.

4. Origin : The "main" location of the files. Typically, on remote repo. But can also be on your computer.


Commands

1. git init : Make the current directory as a local repo. (local repo is the origin. no remote repo linked at this point)

2. git add : Put files into local repo for the first time, i.e. begin tracking it with Version Control.
This is called "staging" or put the file on "stage".

3. git clone : Clone a remote repo to a local directory with origin pointing to the remote repo.
Doesn't require git init. Also called checkout code from remote repo.

4. git status : Show files that are tracked in local repo.
Typically, will show files that are newly added, modified or untracked files.

5. git log : Show log of changes with their revision.

6. git revert : Revert local repo to previous revision state.

7. git commit : Commit all changes (or all files in staging) to local repo.

8. git push : Push your last commit to origin (typically, remote repo).
Also called checkin your code to remote repo.

9. git pull : Keep your local repo up to date with remote repo aka get updates from remote repo.
Obviously, also called update or sync.

10. git diff : view the differences between current codes (if changes are made) and the previous revision.

11. git merge : apply the changes from one file to another.
Typically, when you try to push a file, another developer has also pushed for some changes on the file after your last update with remote repo (so you haven't got his changes yet)

12. git rm : remove the file from tracking.
 After commited, the file then becomes "untracked" if you use "git status".