Simple documentation and CheatSheet for Git implementation

For last few days, I've been learning and working with GIT which is the most adopted distributed version control system. So, while learning the same from various resources I've made some notes and created a CheatSheet for using the tool which covers most of its concepts and command syntax.

What is GIT and its importance?

  • Version control system to track changes in code

  • Free & open source

  • Fast scalable

  • Track the history of activities

  • Helps to collaborate

What is GitHub?

  • GitHub is a cloud-based platform or a website in general, where you can store, share, and work together with others to write code using Git utility. To visit the same refer https://github.com/

Setting up Git on your environment:

  • Install VS code as it's free and open source code editor. To download VS code visit https://code.visualstudio.com/download

  • Also, for Windows Machine download Git Bash and for MacOS download Terminal(if not present).

    To download Git Bash on Windows visit https://git-scm.com/download/win
    For Terminal on Mac you can download/install it from Apple store.

  • After setting up Git run below command on your Machine (Git Bash for windows and Terminal for MacOS) to check if Git is working as required.

git --version

Configuring Git

  • Open Git Bash(Windows) or Terminal (MacOS) and run the below commands to configure user information used across all local repositories;
$ git config --global user.name “[firstname lastname]”
### Sets a name that is identifiable for credit when review version history.

$ git config --global user.email “[valid-email]”
### Sets an email address that will be associated with each history marker.

## Also, in the above commands I'm using --global but you can use --local based on your use-case i.e, if you've multiple accounts for Git.

$ git config --global color.ui auto
### Sets automatic command line coloring for Git for easy reviewing.

$ git config --list 
### Gives our credentail details.

Cloning a Repository

$ git clone [url]
### Retrieve an entire repository from a hosted location via URL such as from HitHub.

Stage & Snapshot

  • status - shows modified files in working directory, staged for your next commit.
$ git status
  1. untracked - new files that git doesn't yet track

  2. modified - changed staged - file is ready to be committed

  3. unmodified - unchanged

  • After modifying a file/code you first need to add to update what will be committed, then perform commit.

  • add - Add new/changed files in your working directory to the Git staging area.

$ git add [file]   (add a specific file)
Or
$ git add .        (add all changed files)
  • commit - commit your staged content as a new commit snapshot.
$ git commit -m “[descriptive message]”
  • push - upload local repo content that you committed to remote repository.
$ git push origin main

## origin  - Remote repo which we originally cloned to our local machine
## main  - branch in that repo

Initialize an existing directory as a Git repository

  • init - used to create a new repo using git.
$ git init
$ git remote add origin [Newly create Repo URL]
$ git remote -v    (to verify remote)
$ git branch       (to check branch)
$ git branch -M main (to rename branch)
### Finally we can push our code to Remote Respository using below push.
$ git push origin main

BRANCH & MERGE

  • This is isolating work in branches, changing context, and integrating changes.
  1. Branching implementation:

$ git branch                           (to check my branch)
$ git branch -M main                   (to rename branch)
$ git checkout  [branch name]          (to navigate between branches)
$ git checkout -b [new branch name]    (to create new branch)
$ git branch -d [branch name]          (to delete branch)
  1. Merge code:

    • Way 1:

        $ git diff [branch name]      (to compare commits, branches, files and more)
        $ git merge [branch name]     (to merge 2 branches)
      
    • Way 2:

      Create a PR (pull request)

      • Pull request - It lets you tell others about changes you've pushed to a branch in a repository on github.
  • Pull command - used to fetch and download content from remote repo and immediately update the local repo to match that content.
$ git pull origin main

Resolving Merge Conflicts

  • An event that takes place when Git is unable to to automatically resolve differences in code between two commits. In this case you'll need to manually check both the changes on the respective branches and make a decision that if you want to keep only one change or both.

Undoing changes

  • Case 1 - Staged changes (those changes which are added but not committed yet).
$ git reset [filename]   (for specific file)
$ git reset              (for a set of files)
  • Case 2 - committed changes (for one commit).
$ git reset HEAD~1
  • Case 3 - committed changes (for many commits).
$ git reset [commit hash]
$ git reset --hard [commit hash]   (--hard option will discard any local changes made to the files)

Inspect & Compare

  • Examining logs, diffs and object information
$ git log
### show the commit history for the currently active branch.

$ git log branchB..branchA
### show the commits on branchA that are not on branchB.

$ git log --follow [file]
### show the commits that changed file, even across renames.

$ git diff branchB...branchA
### show the diff of what is in branchA that is not in branchB.

$ git show [SHA]
### show any object in Git in human-readable format.

Forking repo

  • A fork is a new repo that shares code and visibility settings with original upstream repo.

  • Fork is a rough copy.

To learn GIT in detail I would recommend some documentation and youtube video links as follows, which helped me with the same.

https://git-scm.com/book/en/v2/Getting-Started-What-is-Git%3F

https://www.youtube.com/watch?v=Ez8F0nW6S-w

https://www.youtube.com/watch?v=n5x56RxCM0k&list=PL0lvsZ5ieQicEDanBRMNQlKTY5d-zEmKr

0
Subscribe to my newsletter

Read articles from Sajid Ahmad Lone directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sajid Ahmad Lone
Sajid Ahmad Lone