Back to Posts

Introduction to Git

Posted in Git

Working with Git on the command line can be daunting. To help with that, i’ve put together a list of common Git commands, what each one means, and how to use them.There are some actions that we tend to use a lot, so it’s good to learn them. Here are my favorites, learning from friends and internet,hope you find them useful.

What is Git?

Git is a distributed version control system. It is basically a command line program. You will be working with Git using git commands almost all the time. Using Git, many developers can make changes to the same code base at the same time without running into accidents like overriding someone else’s changes. Git will only update the differences made to a file.

Key Terms

  • Version Control System (VCS) or Source Code Manager (SCM): A VCS allows you to: revert files back to a previous state, revert the entire project back to a previous state, review changes made over time, see who last modified something that might be causing a problem, who introduced an issue and when, and more.

  • Commit (snapshot): Git thinks of its data like a set of snapshots of a mini file system. Every time you commit, or save the state of your project in Git, it basically takes a picture of what all your files look like at that moment and stores a reference to that snapshot.

  • Repository (repo): A directory that contains your project work, as well as a few files (hidden by default in Mac OS X) which are used to communicate with Git. Repositories can exist either locally on your computer or as a remote copy on another computer.

  • Working Directory: The files that you see in your computer's file system. When you open your project files up on a code editor, you're working with files in the Working Directory.

  • Branch: A branch is when a new line of development is created that diverges from the main line of development. This alternative line of development can continue without altering the main line.

  • Checkout: When content in the repository has been copied to the Working Directory. It is possible to checkout many things from a repository; a file, a commit, a branch, etc.

  • Staging Area or Staging Index or Index: A file in the Git directory that stores information about what will go into your next commit. You can think of the staging area as a prep table where Git will take the next commit. Files on the Staging Index are poised to be added to the repository.

How to Install?

You can install Git on your system using a package manager, an installer or from source.

All instructions are listed at here.

A git installation must have a remote repository (remote code base) where the changes made by many developers accumulate. Two of the most popular repository hosting websites are GitHub and GitLab, however you can also choose to host your own remote repository on a server.

If you are not comfortable with Git commands, then you can use their official GUI which will be available to you after installing Git. If your repository is on GitHub, then you can download their official GUI application from this link.

Since we're done with the basic terms and workflow let's write some code.

Useful commands


Status


  • Check the status of working directory and staging area:
       git status
     
  • Show changes between HEAD and working directory:
        git diff
    
  • Show the list of commits in one line format:
        git oneline
     

    Tags


  • List all tags:
        git tag
      

    Remote


  • List all remote:
        git remote
      

    Branch


  • List all branches:
        git branch
    
  • Create the branch on your local machine and switch in this branch:
        git checkout -b branch_name
    
  • Push the branch to remote:
        git push origin branch_name
      
  • Delete a branch:
        git branch -D the_local_branch
        git push origin :the_remote_branch
      

    Commit


  • Undo last commit:
         git reset --hard HEAD~1
    
  • Squash last n commits into one commit:
          git rebase -i HEAD~5
    
          git reset --soft HEAD~5
          git add .
          git commit -m "Update"
          git push -f origin master
    

    Cherry Pick


  • Add some commits to the top of the current branch:
          git cherry-pick hash_commit_A hash_commit_B
    

    Checkout


  • Checkout a branch:
          git checkout destination_branch
    
          git checkout -m master // In case a merge conflict exists
    

    Rebase


  • Rebase the current branch onto master:
           git rebase master // rebase the current branch onto master
    
           git rebase --continue // continue rebase
    
           git rebase --abort // abort rebase
    

    These are the most necessary commands you need while learning Git,obviously there is a lot more to explore which we'll be learning on the way later.Happy Coding!
  • Android Developer and Open-Source enthusiast.