Git

10 min. read

Git is a distributed version control system for tracking changes in code. It allows multiple developers to work on the same codebase simultaneously.

Introduction to Git

Git is a distributed version control system, created in April 2005 by Linus Torvalds, creator of Linux, which still drives the development of Linux.

Git was based on BitKeeper SCM.

Basics of Git

Create a new repository locally:

1
2
3
4
5
```

Checkout a repository:

``` bash

Workflow
3 main trees
Working Directory
Holds the actual files
Index
Acts as a staging area
HEAD
Points to the last commit you have made
Working Directory -> add -> Index –> commit –> HEAD

Add files to Index (Staging)

1
2
3
```
or to add all files
``` bash

Commit changes:

1
2
3
4
5
6
7
8
```
This file commit the changes to HEAD (locally)

Pushing Changes
To send your changes to the remote server:


``` bash
1
Add server to local git

Configure Git for the first time

1
2
git config --global user.name "NAME"
git config --global user.email "EMAIL"

Git Commands

Here is an exhaustive list of Git commands with brief descriptions of what they do:

  1. git init - Initialize a new Git repository.
  2. git clone - Create a local copy of a remote repository.
  3. git add - Add files to the staging area.
  4. git mv - Move or rename a file or directory.
  5. git commit - Record changes to the repository.
  6. git rm - Remove files from the working directory and Git repository.
  7. git status - Show the status of the working directory.
  8. git log - Show commit logs.
  9. git diff - Show changes between commits, branches, etc.
  10. git tag - Create, list, delete or verify a tag object signed with GPG.
  11. git branch - List, create, or delete branches.
  12. git checkout - Switch branches or restore working tree files.
  13. git merge - Combine changes from different branches.
  14. git stash - Temporarily save changes that are not ready to be committed.
  15. git reset - Unstage changes or revert commits.
  16. git revert - Create new commits that undo changes made in previous commits.
  17. git remote - Manage set of tracked repositories.
  18. git fetch - Download objects and refs from another repository.
  19. git pull - Download changes from a remote repository to the local repository.
  20. git push - Upload local repository content to a remote repository.
  21. git config - Configure Git settings.
  22. git grep - Print lines matching a pattern.
  23. git bisect - Find the commit that introduced a bug.
  24. git blame - Show who changed what and when in a file.
  25. git rebase - Reapply commits on top of another base tip.
  26. git cherry-pick - Apply the changes introduced by some existing commits.
  27. git format-patch - Prepare patches for e-mail submission.
  28. git am - Apply patches created by git format-patch.
  29. git submodule - Initialize, update or inspect submodules.
  30. git worktree - Manage multiple working trees.
  31. git reflog - Show a log of all changes to Git references.
  32. git archive - Create an archive of files from a named tree.
  33. git revert - Revert some existing commits.
  34. git rebase -i - Interactive rebase.
  35. git merge-base - Find as good common ancestors as possible for a merge.
  36. git rev-parse - Parse revision (or other objects) identifiers.
  37. git rev-list - List commit objects in reverse chronological order.
  38. git rev-set - Manipulate revision identifiers.
  39. git pull –rebase - Fetch and merge changes on the remote branch.
  40. git remote add - Add a new remote repository.
  41. git remote rm - Remove a remote repository.
  42. git remote show - Show information about a remote repository.
  43. git remote update - Fetch updates from a remote repository.
  44. git remote rename - Rename a remote repository.
  45. git remote set-url - Change the URL of a remote repository.
  46. git fsck - Verify the connectivity and validity of the Git objects.
  47. git prune - Prune unreachable objects from the object database.
  48. git gc - Cleanup unnecessary files and optimize the local repository.
  49. git submodule foreach - Run a command in each submodule.
  50. git clean - Remove untracked files from the working directory.

This is an exhaustive list of Git commands, but there are many more options and parameters available for each command.
You can find more information about each command by running the following command:

1
git help <command>

Example:

1
git help pull

Working with your repository

I just want to clone this repository

If you want to simply clone this empty repository then run this command in your terminal.

1
git clone GIT URL

My code is ready to be pushed

If you already have code ready to be pushed to this repository then run this in your terminal.

1
2
3
4
5
6
cd existing-project
git init
git add --all
git commit -m "Initial Commit"
git remote add origin GIT URL
git push -u origin master

My code is already tracked by Git

If your code is already tracked by Git then set this repository as your “origin” to push to.

1
2
3
cd existing-project
git remote set-url origin GIT URL
git push -u origin master

Find all git repos locally

1
2
3
4
5
6
7
8
9
```

## Branching
Master is called default branch
Features should be branches

Create a new branch
``` bash
git checkout -b FEATURE_NAME

Switching branches

1
git checkout master

Delete a branch

1
git branch -d FEATURE_NAME

Push a branch

1
git push origin FEATURE_NAME

Update and Merge

Update local to latest commit

1
git pull

This will fetch and merge the remote changes

Merge a branch

1
git merge FEATURE_NAME

After merging

1
git add FILE_NAME

See the difference

1
git diff SOURCE_BRANCH TARGET_BRANCH

Tagging

Its recommended to tag releases

1
git tag 1.0.0 branchId 

BranchId is the first 10 characters of a commit

Git Log

Gets the status/history of a repo

1
git log

Replace (local changes)
git checkout – FILENAME

Drop all changes
git fetch origin
git reset –hard origin/master

Git Flow

http://nvie.com/posts/a-successful-git-branching-model/
https://nurelm.com/your-git-flow-branching-strategy/

Git Submodules

https://git-scm.com/book/en/v2/Git-Tools-Submodules
https://chrisjean.com/git-submodules-adding-using-removing-and-updating/

Git Hooks

https://githooks.com/
https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks
https://www.atlassian.com/git/tutorials/git-hooks
https://www.digitalocean.com/community/tutorials/how-to-use-git-hooks-to-automate-development-and-deployment-tasks
https://git-scm.com/docs/githooks

Git GUI Clients

https://git-scm.com/downloads/guis
https://git-scm.com/download/gui/mac
https://gitextensions.github.io/
http://gitup.co/
https://github.com/FredrikNoren/ungit
https://git-cola.github.io/
https://www.gitkraken.com/
https://www.hostinger.com/tutorials/best-git-gui-clients/
https://www.syntevo.com/smartgit/download/
https://git-cola.github.io/downloads
https://www.gitkraken.com/download
https://tortoisegit.org/download/

Git Common Actions

Committed to HEAD and now everything is lost?
git reflog
514abda (HEAD -> develop, origin/develop) HEAD@{0}: checkout: moving from 1f9b87325ebc1861d655f62988a5e8112ca36ced to develop
1f9b873 HEAD@{1}: commit: Fixed compile issues
f7ab7b9 HEAD@{2}: checkout: moving from develop to f7ab7b96e37cabf83c9d7f81441b03a07db210bc
514abda (HEAD -> develop, origin/develop) HEAD@{3}: reset: moving to HEAD

git reset –hard 1f9b873

Git LFS

https://support.atlassian.com/bitbucket-cloud/docs/use-git-lfs-with-existing-bitbucket-repositories/

https://support.atlassian.com/bitbucket-cloud/docs/use-bfg-to-migrate-a-repo-to-git-lfs/

https://rtyley.github.io/bfg-repo-cleaner/

https://support.atlassian.com/bitbucket-cloud/docs/use-git-lfs-with-bitbucket/#UseGitLFSwithBitbucket-client

https://support.atlassian.com/bitbucket-cloud/docs/use-git-lfs-with-bitbucket/#UseGitLFSwithBitbucket-tracking

https://support.atlassian.com/bitbucket-cloud/docs/reduce-repository-size/

https://support.atlassian.com/bitbucket-cloud/docs/use-bfg-to-migrate-a-repo-to-git-lfs/

Unit Git Plugins

Good GIT
Git for artists
Git UniTEAM
Using Theirs - Which GIT Branch?
GitHub for Unity
UniGit
UniMerge

Git Courses

LinkedIn Learning

Git Essential Training: The Basics

References

http://nvie.com/posts/a-successful-git-branching-model/
https://www.atlassian.com/git/tutorials/comparing-workflows#feature-branch-workflow

Video Tutorials
GitFlow with SourceTree:
https://www.youtube.com/watch?v=6LhTe8Mz6jM&t=11s

Pragmatic Version Control Using Git
https://pragprog.com/titles/tsgit/pragmatic-version-control-using-git

Pro Git
https://github.com/progit/progit

Git book
https://book.git-scm.com/

https://www.gamasutra.com/blogs/TimPettersen/20161206/286981/The_complete_guide_to_Unity__Git.php

http://madewithunity.blogspot.com/2015/11/unity-5-and-git.html

https://towardsdatascience.com/10-git-commands-you-should-know-df54bea1595c

http://nvie.com/posts/a-successful-git-branching-model/
https://www.atlassian.com/git/tutorials/comparing-workflows#feature-branch-workflow

Video Tutorials
GitFlow with SourceTree:
https://www.youtube.com/watch?v=6LhTe8Mz6jM&t=11s

Pragmatic Version Control Using Git
https://pragprog.com/titles/tsgit/pragmatic-version-control-using-git

Pro Git
https://github.com/progit/progit

Git book
https://book.git-scm.com/

https://riptutorial.com/git/example/1017/merge-one-branch-into-another

https://guides.github.com/introduction/git-handbook/

https://www.w3schools.com/git/git_branch_merge.asp