Version Control System
Git is a version control system. A version control system is a piece of software that helps the developers on a software team work together and also archives a complete history of their work.
There are three basic goals of a version control system:
- people able to work simultaneously, not serially.
- when people are working at the same time, their changes are not conflicting each other.
- archive every version of everything that has ever existed — ever. And who did it. And when. And why.
Using Git
Using command line
Open Git Bash and try the following commands.
Change directory
bashcd foldercd folder/subfoldercd ../cd ../another-folder/nested-folder
Create file
bashtouch file-name.ext
Create directory
bashmkdir folder-name
Remove file
bashrm file-name.extrm folder
Remove folder and all its contents (!)
bashrm -rf folder
Create a new, empty repository
repository = filesystem * time
bashgit init
The three states
Git has three main states that your files can reside in: committed, modified, and staged.
- Committed: the file is safely stored in repository
- Modified: the file is changed but not committed to repository yet
- Staged: the file is marked to go into your next commit snapshot
The diagram below illustrated the 3 states:
The basic workflow goes something like this:
- You modify files in your working tree.
- You selectively stage just those changes you want to be part of your next commit, which adds only those changes to the staging area.
- You do a commit, which takes the files as they are in the staging area and stores that snapshot permanently to your Git directory.
To stage your change, we use the following command:
bashgit add <fileName>
To commit all staged files, use the following command:
bashgit commit -m "<commit message>"
To check the current status:
bashgit statusgit status -s
To unstage a staged file:
bashgit reset HEAD <file-name>
Branching
When you create a git repo, by default you have one branch, and it is called master
.
However, if multiple people working on same files together, their changes may conflict each other, and making everyone's life difficult.
Branching means you diverge from the main line of development and continue to do work without messing with that main line.
To create a new branch
bashgit branch testing
However, we're still at master
branch.
To switch to the new branch
bashgit checkout testing
Let's modify something and do another commit.
Switch back to master
branch
bashgit checkout master
When you make another commit on master
You can merge changes of testing
branch into master
with:
bashgit merge testing
Finally, you can delete a branch with
bashgit branch -D testing
Images in this section are adapted from Pro Git
Using GitHub
Create a Repository
Pulling and Pushing from Repository
Forking and Pull Request
Fork and Create a PR to this repo.