Post

Getting Started With Git

What is Git?

An open-source version control system for tracking modifications in source code and files.

As a software developer, git has been part and parcel of my daily development workflow, allowing me to coordinate my code changes with other developers with ease, without worrying that one may overwrite or break the codebase.

Below are some basic terminology in git.

Git Terms 
gitgit is typed in the command line to run git commands
repoShort-form for repository
localA copy of the repository in one's development machine
remote/originThe central repository hosted in the cloud/server
cloneCreate a copy of the repository, in your local machine
forkCreate a copy of the original repository in
your Github account (Used in Github)
.gitignoreSpecify which filenames to be excluded from tracking
commitA snapshot in the timeline of a git repository,
identified by a descriptive message and an SHA id
stageList of files and code changes that are ready to be committed
pushApply commits from local to remote
pullApply latest commits from remote to local
stashTemporary changes made to a working repository
HEADReference to the most recent commit in the current branch
resetUndo changes made by rolling back to previous commits
branchAn independent line of development
A lightweight movable pointer to a specific commit
masterUsually denotes the main branch
mergeCombine the changes of two or more branches into a single branch
rebaseRe-apply the changes made on a single branch on
top of changes made from another branch
Pull requestAn event created by a contributor to discuss
and merge new code changes to the main branch.

Github != Git

Git is a version control software tool that is used in the command line.

Github is a web hosting service for managing git repositories in the cloud.

For most cases, users will use git to manage changes between repositories on their local machine and the remote ones in Github. Github is a web-hosting service that provides cloud-based storage and GUI for storing and managing the centralized repositories.

Glossary of Git Commands

Configuration 
git config --global user.name nameSet the name you want attached to your commits
git config --global user.email email_addressSet the email you want attached to your commits
git config --global color.ui autoSet automatic command line coloring for easy reviewing of Git output
GIT_SSL_NO_VERIFY=booleanEnvironment variable for running git commands on server without SSL certificates
Initialisation/Setup 
git initInitialize an existing directory as a Git repository
git clone urlClone a repository using a remote url
Change Management 
git statusShow modified files in working directory, staged for your next commit
git add fileAdd a file to the stage before your next commit
git rm fileDelete the file from project and stage the removal for commit
git diffDifference between what is changed but not staged
git diff --stagedDifference between what is staged but not yet commited
git commit -m descriptive_messageCommit your staged content as a new commit snapshot
git pushApply your local branch commits to the remote repository branch
git pullFetch and merge any commits from the tracking remote branch
git reset fileUnstage a file while retaining the changes in working directory
git reset --soft commitRewrite working tree from specified commit
Re-apply changes made to the staging area for that commit
git reset --hard commitRewrite working tree from specified commit
Clears the staging area
git push origin -f branchForce push branch to set the remote HEAD at the current commit
git show commitShow one or more objects (blobs, trees, tags and commits)
git blame filenameShow history of commits made on the specific file
Branch Management 
git branchList all your local branches.
A * will appear next to the currently active branch.
Adding a -a flag lists all possible branches (local + remote)
git branch branchCreate a new branch at the current commit
git fetch origin branchFetch remote repository branch and create a local one
git checkout branchSwitch to target branch
git merge branchMerge the specified branch's history into the current one
git rebase branchApply any commits of current branch ahead of specified one
git branch -m oldname newnameRename branch from oldname to newname
git push origin –delete oldname
or
git push origin :oldname
Delete old branch in remote
git push origin -u newnamePush new local branch and
set the remote branch (newname) to track the current local branch
git diff branchBbranchAShow the difference of what is in branchA that is not in branchB
git logShow all commits in the current branch's history
Stash Management 
git stashSave modified and staged changes
git stash listList stack-order of stashed file changes
git stash popWrite working from top of stash stack
git stash dropDiscard the changes from top of stash stack

Conclusion

The above list collates the many commands that I usually use in my day-to-day. This list is especially useful if you need remedies to common problematic scenarios (Eg. Accidentally pushing commits to master/dev branches: git reset; Trying to trace past commits for a specific file: git blame). Despite being around for almost 20 years, git still serves as a primary version control tool for many developers today. With most projects requiring multiple hands on deck and remote work being the new normal, it is hard for one to work on a project properly without it.

Sources

  1. Git vs. GitHub: What’s the Difference?
  2. Git Cheat Sheet Education PDF
  3. Git Commands List for Beginners - Learn the Essential Git Commands in 30 Minutes
  4. Stackoverflow - Renaming a branch in Github
  5. Git vs GitHub: Difference Between Git and GitHub
This post is licensed under CC BY 4.0 by the author.