Git It Together
--
What is GitHub, Whats a Git?
GitHub is a cloud based code hosting service, it allows programmers to collaborate and manage projects at anytime. GitHub does this through the use of Git repositories, a collection of source code states archived at the request of the user(s). With options to clone or fork these repositories it makes it extremely easy to examine the source code, make adjustments and then upload the source code again. It has revolutionized the industry and has given developers of all levels a place to share projects. Theres a reason Microsoft acquired the company for $7.5 billion in stock.
How GitHub Works?
The general flow of GitHub starts with creating a branch, many people continue to work off the “Master” or “Main”, pushing and pulling which is fine in small groups with readily accessible communication streams. However, this becomes a problem quickly in bigger groups, that operate in timezones or when work is delegated amongst team members, so branches quickly become your friend.
Branches allow for experimentation and development of ideas in an environment that doesn’t affect the “Main”.
Commits are important and the messages are equally important. Make commits frequently, not just when you are done for the day. This is crucial when rollbacks are necessary.
Get in the habit of making descriptive commits, it is a good way to track progress, quickly evaluate versions and navigate Git repos. Reading through hundreds of lines of code is time wasted when a commit message could state what changes have been made.
Push your code to the remote branches. This could also involve merging branches together before pushing them. Pushing your commits is how GitHub tracks different versions of projects.
It is important to do this steps often as a means of staying up to date with members of your team and also creating a long history of your code so that you can go back make changes if need be.
Using Git Commands
Due to the lack of GUI interaction with Git it is crucial to learn how to navigate your repos within the terminal and command line. You should have some general understanding of how the terminal works; basic directory/file knowledge, changing directories, relative path knowledge, a general sense of where you are within the terminal and where you will be entering your commands.
Useful Git Commands
I will share some useful Git commands I have come across, frequently use or that I find may be of value to you.
git init — Initializes an existing directory as a new Git repo.
git clone [url] — Clones the repo with the argument of URL into the current directory.
git status — Shows the files you have worked on that will be staged for your next commit.
git add [file] — Add the current file to the staging phase and prepare it for your next commit.
git reset [file] — Unstages a file and return to a previous version of a project Git commits all have 7 character, alphanumeric references to the versions that were created. This can be used as the argument to return to that specific version.
git commit -m “[message] — Commits your staged contents and creates a new version within your repo appended with the message, being descriptive in your messages is a good habit to get into.
git branch — List a current branches for this repo and shows the active branch
git checkout [branch] — Switch to the branch with the argument name within the list of active branches. If no branch with matching argument, a branch will be created.
git fetch [branch] — Fetch down all the branches from that Git repo.
git merge [branch] — Merge a branch into your current branch.
git push [branch] — Pushes local branch commits that were staged up to the remote branch
git pull — Fetch, merge and update any commits from the remote branch
git stash — Temporarily saves modified files allowing you to merge or switch branches