DEV Community

Cover image for Git/Github Quick Start Reference Document
Caio Cesar
Caio Cesar

Posted on • Edited on

Git/Github Quick Start Reference Document

Git and GitHub

GitHub- A cloud platform used for collaborating that makes use of git.

Features of GitHub: Issues, Discussions, Pull requests, Notifications, Labels, Actions, Forks, Projects.

Git- Allows people to contribute to projects with the use of a distributed version control system (DVCS).

In its most basic form git is a content tracker

Basic Git Commands

  • git init - Initialized an existing git repository or creates an empty git repository.

Git Init Screenshot

  • git Status - Displays the status of the working tree. It could inform changes to be committed, changes not staged for commit, untracked files or if the tree is clean.

Git Status Screenshot

  • git add - Add file contents to the index/working tree.

Git Add

Git Status Screenshot

  • git commit - Record changes to the repository

Git Commit

  • git log - Displays commit logs history for the current repository.

Git log Screenshot

Branches

Branch- Git branches are a reference to a snapshot of your changes. By default git initializes with the branch name of main or master.

  • git branch command can also show the list of existing branches for a repository, create new branches or delete existing ones.

List

  • The current selected branch in the screenshot bellow is master that's why it has an asterisk by its name and its color is green. Git Branch List

Create

  • Creates a new branch based of the selected branch named b1. Git Branch Create

List

  • Lists two branches from the repo. Git Branch List

HEAD

  • The HEAD of the current project is a reference to the selected branch that is refs/heads/master.

  • git switch - Allows one to switch the selected branch to an existing branch.

Git Swtich

  • Now the HEAD of the branch is refs/heads/b1.

  • git checkout - Also allows one to switch the selected branch to an existing branch o create new branches.

Git Checkout

  • Now the HEAD of the branch is back to refs/heads/master.

Basic Git Knowledge

Repository- A project that may contain files and folders with versioning.

Commit- represents a change with an assigned a unique ID to 1 or many files.

Pull Request- Responsible for alerting that a commit is ready for merging between branches.

Pull- The git pull command retrieves all base branch commits that hasn't been applied to the current branch.

Cloning and forking

Cloning a Repository- Represents a copy of the repository and its history to a machine.

Forking a Repository- Represents a copy of a repository in a GitHub account.

Merging

Merge- Allows one to adds the changes in the pull request and branch into the target branch.

  • Lets say you have two branches master and b1 with the same ReadMe.txt file and different content on them. Lets assume you want the content in the b1 branch to merge into the master branch. To start the merge you can switch to the master then run the following command.

  • git merge b1

git Merge

  • As you can see from the screenshot above since the content differs there is a merge conflict that needs to be resolved.

  • Now you can resolve the merge conflict in the ReadMe.txt file then add and commit the merged file into the master branch.

Git Merge

Merge Conflict- Occurs when a merge is attempted and changes to the same line of code from parallels changes.

Resolving Merge Conflicts- Git can generate a temporary file that contains differences from each branch. One can manually select what should or shouldn't be merged.

Preventing Merge Conflicts- pull updates frequently and keep branches close to base.

Rebasing

  • Lets say you have two branches master and b1 with the same ReadMe.txt file and different content on them as seen in the figure bellow.

2 branches

  • The rebase command such as rebase master changes the base of the command to move b1 to the end of master as displayed in the image bellow:

Rebase

Top comments (0)