GitHub CheatSheet

GitHub CheatSheet

What is Git ?

Git is a software that allows you to keep track of changes made to a project over time. The way Git works is it records the changes you make, stores them and makes it easy to keep track of and reference them whenever you like. GitHub is a hosting service for repositories. Simply put, Git is the tool and GitHub is the service for projects that use Git.

Git Commands

  • git init

The word init means initialize. The command sets up all the tools Git needs to begin tracking changes made to the project. To use it, just type git init in the command line.

  • git status

    Used to check the status of changes made. To use it, just type git status in the command line.

  • git add

In order for Git to start tracking a file, it needs to be added to the staging area. To do this, we can use the git add filename. Filename refers to the actual name of the file.

  • git diff

    Used to check the differences between the working directory and the staging area and this can be done with: git diff filename

  • git commit

    A commit is the last step in our Git workflow. A commit permanently stores changes from the staging area inside the repository. To use it, just type git commit in the command line, with -m and a message . We end up with something like this git commit -m "Complete first line of dialogue". To see how to write a better git message, check here

  • Git log

    Often with Git, you’ll need to refer back to an earlier version of a project. Commits are stored chronologically in the repository and can be viewed with git log. In your output, you will get the following;

  1. A 40-character code, called a SHA, that uniquely identifies the commit. This appears in orange text.

  2. The commit author (you!)

  3. The date and time of the commit

  4. The commit message

  • git log --oneline shows the list of commits in one line format.

  • git log -S "keyword" displays a list of commits that contain the keyword in the message e.g we use git log -S "Add" to find any commits with “Add” in the message.

  • git log --oneline --graph - --graph Displays a visual representation of how the branches and commits were created in order to help make sense of the repository history.

Important Git operations

When working on a Git project, we occasionally make changes that we would like to undo. Git has a few eraser-like features that let us undo mistakes while working on a project.

  • git checkout HEAD filename restores the file in your working directory to look exactly as it did when you last made a commit. Filename refers to the actual name of your file.

  • git reset HEAD filename is used to unstage file from a staging area but doesn't discard the files. It just removes them.

  • HEAD is at the most recent commit.

  • git add filename_1 filename_2 is used to add multiple files to a staging area.

  • git stash stores your work temporarily for later use in a hidden directory.

  • git commit --amend to update your previous commit.

  • git commit --amend --no-edit used to keep the same commit message.

Git Branching

Git branching allows users to experiment with different versions of a project by checking out separate branches to work on.

  • git branch Lists all a Git project’s branches.

  • git branch new_branch used to create a branch.

  • git checkout branch_name used to switch between branches.

  • git branch -D branch_name used to delete a specified branch.

  • git configused to configure frequently used commands such as;

$ git config --global alias.co "checkout"
$ git config --global alias.br "branch"
$ git config --global alias.glop "log --pretty=format:"%h %s" --graph"

and can then be used like this next time $ git co example_branch instead of $ git checkout example_branch . This helps with efficient work flow experience .

Git Workflow

A Git project can be thought of as having three parts:

  • A Working Directory: where you’ll be doing all the work: creating, editing, deleting and organizing files

  • A Staging Area: where you’ll list changes you make to the working directory

  • A Repository: where Git permanently stores those changes as different versions of the project.

The Git workflow consists of editing files in the working directory, adding files to the staging area, and saving changes to a Git repository.

In Github, a basic workflow is used to help team members isolate their work and avoid conflicting code being merged. The workflow used are;

  • Create a branch

A branch is a divergence from the main project. When working with a team, you can create a branch off the main from which each team member can work from, as much as possible without affecting the important main state of the code.

  • Adding and committing changes

When you commit change(s) made to one or more files, there's a record made of a unique ID assigned to the commit that identifies who created the changes, what changes were created, and when the changes were created. You can commit along with a message describing your work, and lastly, push the commit to the remote Github repository.

  • Create a pull request

This is the point where your work is ready to be reviewed before being integrated into the official project. When creating a pull request, you need to be sure your code runs properly with the updated repo to prevent things from crashing. Here, collaborators are able to review and give feedback before deciding if the branch is ready to be merged with the main branch. It is very important to add as much information as possible for the review.

  • Review and merge pull request

Here, the team has the chance to review and able to give a feedback. Each line of code will have a clickable “+” button where you can add a comment in regards to the line. Best practices when reviewing code:

  1. Leave a short description of what should be changed and why it should be changed. Add resources if possible for better guidance.
  2. Be clear and concise.
  3. Look out for potential errors/bugs that might cause problems if the project scales.

Submit review when you are done and once everything is fixed, merge into main.

  • Delete and review branch

It is best practice to keep only active files. After merging, delete the file to keep things organized.

Teamwork on gitHub

To collaborate on GitHub, you make use of remotes. A remote is a shared Git repository that allows multiple collaborators to work on the same Git project from different locations. Collaborators work on the project independently, and merge changes together when they are ready to do so.

Important commands to know when collaborating on GitHub

  • git clone remote_location clone_name used to clone.

  • git remote -v to view a list of git project's remote.

  • git fetch to check for changes in the remote repo and bring them into the working branch.

  • git merge origin/master to merge the branch to the master.

  • git push origin <your_branch_name> Pushes a local branch to the origin remote.

Conclusion

In conclusion, Git projects are usually managed on Github, a website that hosts Git projects for millions of users. With Github you can access your projects from anywhere in the world by using the basic workflow.

The workflow for Git collaborations typically follows this order:

  1. Fetch and merge changes from the remote

  2. Create a branch to work on a new project feature

  3. Develop the feature on your branch and commit your work

  4. Fetch and merge from the remote again (in case new commits were made while you were working)

  5. Push your branch up to the remote for review