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.
Open Git Bash and try the following commands.
bashcd foldercd folder/subfoldercd ../cd ../another-folder/nested-folder
bashrm file-name.extrm folder
Remove folder and all its contents (!)
bashrm -rf folder
repository = filesystem * time
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>
When you create a git repo, by default you have one branch, and it is called
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
To switch to the new branch
bashgit checkout testing
Let's modify something and do another commit.
Switch back to
bashgit checkout master
When you make another commit on master
You can merge changes of
testing branch into
bashgit merge testing
Finally, you can delete a branch with
bashgit branch -D testing
Images in this section are adapted from Pro Git
Fork and Create a PR to this repo.