Git
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
cd folder
cd folder/subfolder
cd ../
cd ../another-folder/nested-folder
Create file
touch file-name.ext
Create directory
mkdir folder-name
Remove file
rm file-name.ext
rm folder
Remove folder and all its contents (!)
rm -rf folder
Create a new, empty repository
repository = filesystem * time
git 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:
git add <fileName>
To commit all staged files, use the following command:
git commit -m "<commit message>"
To check the current status:
git status
git status -s
To unstage a staged file:
git 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
git branch testing
However, we’re still at master
branch.
To switch to the new branch
git switch testing
Let’s modify something and do another commit.
Switch back to master
branch
git switch master
When you make another commit on master
You can merge changes of testing
branch into master
with:
git merge testing
Finally, you can delete a branch with
git 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.