What is Version Control?
- A Version Control System (VCS)/Revision Control System is software that keeps track of changes made to a file/set of files over time
- very popular in tracking changes made to source code
- Version control is modelled around the concept of a change set - grouping of files that together represent a change to the system (e.g. new feature implemented)
- Git is designed around several core concepts:
- Repository (repo) - location of canonical/main version of source code
- Working Directory - copy of repository, where you will make changes before saving them in the repository
- Staging Area - logical collection of changes from the working directory that you want to collect and work on together
- A repository can be local or remote:
- Local - where you might store projects you don’t need to share with anyone else
- Remote repository - setup on a central server, where multiple users can access it, e.g. GitHub, GitLab
- Git works by operating on a set of files (change set) -
git add
files in working directory to add to change set, git commit
to save changeset to local repository, use git push
and git pull
to sync local and remote repositories
- Git is a distributed version control system - each user maintains their own repository, eliminating the need for any one master copy which is required by other version control systems
git help
- help manual
git help add
- get help for specific command
Basics
- To place a project under version control using Git, run
git init
from project root directory
- creates
.git
directory in root directory - contains all info git needs to track files in project
- See status of tracked/untracked files with
git status
- to add files to be tracked, use
git add
- once all changes that are part of a changeset are staged, we can commit them i.e.
git commit -m "<message>"
- To check commit history -
git log
- output is displayed in reverse chronological order by default
--reverse
- shows commits in reverse chronological order
-n <number
- last n
--since=<date>
- commits since a time
--until=<date>
- commits until a date
--author="username"
- check author
git diff
- compare versions in repository to those in working directory
- running with
--staged
flag will compare modifications between files in staging area and the repository
- running with
--color-words
will show the same diff output by comparing line by line versions, but will colour code
- To untrack/remove a file, you can remove it from the working directory or simply use
git rm <file>
- to move/rename a file, simply use
git mv <oldname> <newname>
- To clean up the project directory, use
git clean
- when run with
-n
option, clean will show which files are untracked and can be rmeoved
- when run with
-f
option, Git will go ahead and remove these untracked files from your project directory