Git Basics and Cheatsheet
What is VCS
VCS (Version Revision Control) System
- A system for managing changes to documents, programs, web pages, etc.
- Maintains a revision history of changes to the document
- Maintains multiple versions of a document
- Enables multiple users to collaborate on a common collection of documents
- There are many revision control systems available
- rcs, cvs, subversion, mercurial
- git
What git is?
Git is a decentralized version control system (VCS) for tracking changes in computer files.
- Distributed version control
- Coordinates work between multiple developers
- Who made what changes and when
- Revert back at any time
- Local and Remote repos
Developed by the Linux development community
Inital Goal:
- Speed
- Simple design
- Strong support for non-linear development (thousands of parallel branches)
- Fully distributed
- Able to handle large projects like the Linux kernel efficiently (speed and data size)
Git vs Github
- Git is a high quality version control system
- Github is a cloud-based hosting service
- Making VCS distributed (backup and collaboration with people)
- Remote repositories: copies of your files on other computers
- Alternative free git servers: gitLab, bitbucket, phabricator, …
Installing git
- Go to Google : Install git on <your OS>
Basic Commands to work with Local Repository
Initialize Local Git Repository
- Git Repository contains all the files/folders associated with a project
1 | git init |
- Begins tracking the directory by creating a .git hidden folder that contains the structures required for version control
- The file history appears as snapshots in time called commit
Config Name and Email
1 | git config --global user.name '<your name>' |
Add File(s) To Index
- Making git keep track of a file/directory (so-called staging the file/directory)
1 | git add <file> |
Add ALL Files To Index
1 | git add . |
Remove File(s) from Index
1 | git rm --cached <file> |
Check Status of Working Tree (Staging)
- Gives the status of your git repository
- Untracked files
- Changes that are not committed
- Your current local branch
- Any unmerged files
- Changes between the local and the remote branch
- The remote and the remote branch
<remote-name>/ <remote-branch>
1 | git status |
Commit Changes In Index
- You will need to add a file before committing its changes
- Git commit is a Lightweight copy-and-paste
- Only stores the changes (snapshot, not differences!) from one version to another (called delta)
- each commit has a globally unique name
- git keeps a history of commits by having them as a linked list
1 | git commit |
This command will bring you to vi interface by default to leave a commit message. To type the commit message, press
i
to go into insert mode. After finish typing, pressEsc
to exit insert mode.Then you can:
- press
:wq
to save and quit- press
:q!
to quit without saving
Commit Changes In Index (Add comment here)
1 | git commit -m '<your commit message>' |
1 | git commit <file> | -a (all files) -m "<message>" |
Check your commits
1 | git log |
1 | git vlog |
Branches
- The commits create a linked list showing one line of history
- You can create multiple lines of history using branches
- Branches are essentially pointers to specific commits
- Can easily be created and deprecated
Listing the existing branches
1 | git branch |
alternatively
1 | git branch --list |
1 | git branch -l |
Create a new branch
1 | git branch <new branch name> |
Note the new branch with have all your files of your main/master branch.
Switch to the branch
1 | git checkout <branch name> |
Try adding some files and switch back to the main/master branch. See the directory and you will found git will only show the files of the current branch in the directory.
Create and switch to a new branch
1 | git checkout -b <new branch name> |
Branch Merge
- Combining the work from two different branches
- Merging creates a special commit with two unique parents
Merge a branch to current branch
1 | git merge <merging branch name> |
This command will bring you to vi interface. To type the merge message, press
i
to go into insert mode. After finish typing, pressEsc
to exit insert mode.Then you can:
- press
:wq
to save and quit- press
:q!
to quit without saving
Merge conflicts
How it happens?
- The changes made are auto-merged first.
- If auto-merge fails, you will need to resolve the conflict manually
Solving Merge conflicts
- You will need to tell git which version you want to keep
- Does not affect the merged branch version
- You can use git mergetool if you want a visual tool for resolving conflicts
Local Git Workflow
- Create a local repository (init or clone)
- Create a branch (the master branch is for a stable version only—branch early, branch often)
- Add the files you want to be tracked by git (typically text- based files—not executables)
- Work on your project, make commits (you may create sub- branches)
- Merge to master once completed
Work with Remote Repository
Remote Repositories
- A repository on a remote machine which you have access to
- The remote repository is shown as
<name-of-remote>/<name-of-remote-branch>
on your local repository (since last time you synced to the remote)
Note that:
- The branches on your local repo and remote repo could be different
Basic Commands to work with Remote Repository
Push to Remote Repository
1 | git remote |
Link a remote repository on github
1 | git remote add <remote name> <url> |
First Push to Remote Repository
1 | git push -u <remote name> <branch name> |
Push to Remote Repository
- Uploads the local branch into the remote branch
- All people having access to the remote branch can see your changes
1 | git push |
Synchronizing a remote branch with your local branch
1 | git fetch |
Pull Latest From Remote Repository
- Combining fetching and merging the remote branch into the local branch together (technically the same)
- there is a remote added to the git once you cloned, so git knows where to fetch from, you can change it
1 | git pull |
Clone Repository Into A New Directory
1 | git clone <url> |
To Clone a local copy of a remote repository without linking a remote manually:
1 | git clone [—recursive] <username>@<remote address> <name-of-local-directory> |
- ^Requires
- ssh: uses a ssh-key for authentication or
- https: asks for username/password on the CLI
what is .gitignore?
.gitignore
file basically record the files we want our git to ignore.
Tips of Commits
-
Commit your changes after each unit of work
-
Use appropriate messages for your commits
-
Separate subject from body with a blank line
-
Limit the subject line to 50 characters
-
Capitalize the subject line
-
Do not end the subject line with a period
-
Use the imperative mood in the subject line
-
Wrap the body at 72 characters
-
Use the body to explain what and why vs. how
Tips of Branching
- Branch early branch often next version to replace (merge into) master
- branch
master
ormain
is only for stable version- only stable, well-tested commit
Some common namings of branch
dev
ordevelop
: development version- next version to replace (merge into) master
topic
: experimental bleeding edge- test stuff out before merging into develop