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 | ``` |
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 | ``` |
Commit changes:
1 | ``` |
1 | Add server to local git |
Configure Git for the first time
1 | git config --global user.name "NAME" |
Git Commands
Here is an exhaustive list of Git commands with brief descriptions of what they do:
- git init - Initialize a new Git repository.
- git clone - Create a local copy of a remote repository.
- git add - Add files to the staging area.
- git mv - Move or rename a file or directory.
- git commit - Record changes to the repository.
- git rm - Remove files from the working directory and Git repository.
- git status - Show the status of the working directory.
- git log - Show commit logs.
- git diff - Show changes between commits, branches, etc.
- git tag - Create, list, delete or verify a tag object signed with GPG.
- git branch - List, create, or delete branches.
- git checkout - Switch branches or restore working tree files.
- git merge - Combine changes from different branches.
- git stash - Temporarily save changes that are not ready to be committed.
- git reset - Unstage changes or revert commits.
- git revert - Create new commits that undo changes made in previous commits.
- git remote - Manage set of tracked repositories.
- git fetch - Download objects and refs from another repository.
- git pull - Download changes from a remote repository to the local repository.
- git push - Upload local repository content to a remote repository.
- git config - Configure Git settings.
- git grep - Print lines matching a pattern.
- git bisect - Find the commit that introduced a bug.
- git blame - Show who changed what and when in a file.
- git rebase - Reapply commits on top of another base tip.
- git cherry-pick - Apply the changes introduced by some existing commits.
- git format-patch - Prepare patches for e-mail submission.
- git am - Apply patches created by git format-patch.
- git submodule - Initialize, update or inspect submodules.
- git worktree - Manage multiple working trees.
- git reflog - Show a log of all changes to Git references.
- git archive - Create an archive of files from a named tree.
- git revert - Revert some existing commits.
- git rebase -i - Interactive rebase.
- git merge-base - Find as good common ancestors as possible for a merge.
- git rev-parse - Parse revision (or other objects) identifiers.
- git rev-list - List commit objects in reverse chronological order.
- git rev-set - Manipulate revision identifiers.
- git pull –rebase - Fetch and merge changes on the remote branch.
- git remote add - Add a new remote repository.
- git remote rm - Remove a remote repository.
- git remote show - Show information about a remote repository.
- git remote update - Fetch updates from a remote repository.
- git remote rename - Rename a remote repository.
- git remote set-url - Change the URL of a remote repository.
- git fsck - Verify the connectivity and validity of the Git objects.
- git prune - Prune unreachable objects from the object database.
- git gc - Cleanup unnecessary files and optimize the local repository.
- git submodule foreach - Run a command in each submodule.
- 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 | cd existing-project |
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 | cd existing-project |
Find all git repos locally
1 | ``` |
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/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