Git Started with Git
An introductory guide to using Github
What is Git?
Git is a decentralized version control system. With its distributed nature, Git can scale immensely.
An example can be seen with TensorFlow. In 2015, this software library was initialized by the Google Brain Team and in 2017, version 1.0.0 was released to the public and since then, it has gained massive traction. As of today, TensorFlow has over two thousand contributors, 57 thousand commits, 75 thousand forks, and 49 branches. Currently, TensorFlow is considered to be one of the most popular open source platform for machine learning.
There are many advantages for using Git. 1) It is free. Its open source property makes Git popular resulting in an actively engaging community and prevalent up-to-date resources. 2) It is relatively local. Although a few commands can require a network connection, most operations in Git are done on a local server allowing for faster and more secure operations. 3) It alleviates conflict. Git offers great conflict resolution as it tracks incompatible disputes between multiple contributors with an audit trail.
Git has three main components. Repository, commits, and branches. A repository is the collection of folders and its respective files being managed along with its entire history. A commit is a record of the repository and its state at a specific time. A branch is a series of commits in the timeline. Supplementary branches can also be created as a parallel version of its existing branch.
Local Git States
The three states in Git are modified, staged, and committed. These are usually represented with the working directory, staging area, and local/remote repository. The working directory contains all the files and folders, local/remote repository contains the commit history of the saved changes to the Git repository, and the staging area (also known as index), is the step in between working directory and local repo.
- Modified — file has been changed but not yet committed
- Staged — modified file has been distinguished in its current version to advance to the next commit snapshot
- Committed — data that is recorded
Merge Types
As mentioned above, the master branch is the timeline of commits and additional branches can be created to isolate updates separate from the original master branch. This separate branch can then be merged into the master branch to add the respective updates. These are the following types of merges:
Fast-forward merge. When no additional updates have been committed to the master branch, Git will apply all branched commits from the separate branch to the parent branch, as if it was never initially branched off. This can be disabled and be rebased by abandoning some commits and creating new ones called non-fast-forward merge.
Automatic branch merge. When there are no conflicting changes detected between the branches, Git preserves both timelines and creates a new automatic merge commit on destination of the parent branch.
Manual branch merge. When there are conflicts that Git cannot automatically resolve, a manual merge is required. Git enters a merge state where all conflicts must be resolved and the changes are saved as a merge commit.
Setting up
Configuration
On your command line configure your username and email. Simply change the italicized code to match your desired input.
git config --global user.name "name"
git config --global user.email "email"
To test to see if it is properly configured, use the following commands:
git config user.name
git config user.email
Integration
Integrate Git with your text editor. Personally, I like using atom, but any text editor would be fine.
git config --global core.editor "atom -w"
To test to see if this is properly integrated:
git config --global -e
This should open your .gitconfig file on your installed text editor.
Common Git Operations
git init
— create a new local repositorygit status
— list all new or modified files to be committedgit add
— add file to staging areagit commit
— add file from staging area to local repositorygit clone
— download a project and its entire version historygit diff
— show files’ differences to be stagedgit log
— list version history for current branchgit rm
— delete file from the working directorygit reset
— undo commits and change back to a specified onegit push
— upload all local branch commits to remote repositorygit pull
— fetch from and integrate with another repo or a local branch
Common Git Flows
Access existing repository
- Download and checkout a remote repository.
git clone url
This will clone a remote repository into a subdirectory.
Create a new repository
- Create repo in current directory.
git init
After the folder is created, you can use thels
command to see the created repository. - Stage file(s) to commit.
git add filename
for a specific file.git add -A
for all files (new, modified, and deleted).git add .
for new and modified files only.
This step moves the file from the working directory to the staging area. - Create a commit from the staged files.
git commit -m "message"
Use this message as a way to keep record of the changes made. - Add remote to list of tracked repos.
git remote add remote_name url
- Send changes to remote repo.
git push origin master
Change “master” to the desired branch you want to push your changes to.
Work on repository
- Get the latest version of the repo.
git pull
This will fetch and merge changes from the remote repository. - Stage file(s) to commit.
git add filename
for a specific file.git add -A
for all files (new, modified, and deleted).git add .
for new and modified files only.
This step moves the file from the working directory to the staging area. - Create a commit from the staged files.
git commit -m "message"
Use this message as a way to keep record of the changes made. - Send changes to back from local repo to the original remote repo.
git push
Tip
It is good practice to use the
git status
command to see the status of the working tree and all new or modified files to be committed.
Conclusion
Git is a useful version control tool for software development. I’ve only covered the basics but there are many other applications of Git for software development. I would recommend looking through the official documentation for more material or this cheat sheet for easy referencing.