Basics

Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.

why version control?

  • Combine work of multiple collaborators
  • Understand changes
  • Compare and revert to earlier versions
  • Backup
  • Parallel versions
  • Document development (for other developers and yourself, not for users)

Setup

git init

Clone

git clone https://github/<username>/<repo_name>.git

Config

git config user.name "Alireza Dizaji"
git config user.email "alireza@example.com"

to make settings for all repositories in your pc use --global after git config

Commit

Commit = saved snapshot of tracked files. You can always revert to a commit! You can also compare them, share them, …

Committing in Git works in two steps. First modified or untracked files are registered for the next commit by using add . This is called staging. The staged files are then committed with commit.

commit

git add <path/to/file> # file(s) is now staged for commit
git commit -m "your commit message"

Monitoring

git status

If none of your files tracked or modified output is like this:

On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working tree clean

It shows you the files in the repository, both tracked and untracked by Git.

git diff
git diff <path/to/file>

diff shows you changes (differences) between versions. Without arguments, it shows all changes made to tracked files in the repository since the last commit.

git log
git log -3 # show last 3 commit logs

Branches

git branch # see current branch and other exist branches.
git branch -a # show remote branches too. (remote branches described in next section).

Create a new branch:

git branch dev # dev is the name of the branch

Switch to the branch using checkout :

git checkout dev

Remote

Remote repositories are versions of your project that are hosted on the Internet or network somewhere.

See what our remote is:

git remote # remote shortname
git remote -v # it also shows you the URLs

To update the local repository:

git pull

To update the remote repository:

git push -u origin master # -u tells the remote to track this branch in the future

origin and master are remote and branch names.

General workflow

git workflow

Ignore files

Often we have some files that we don't want git to automatically add or even show you as untracked. Some reasons for ignoring files:

  • large size (Ex. dataset, node_modules, etc)
  • security reasons (Ex. model weights, model architecture, etc)
  • logs (Ex. files produced by your build system)

You must create .gitignore file in root directory.

Click here for more details.