Due to the most time on my job, we need to type many git command lines. So that this article is used to sort several git commands frequently used for easily searching.

Intorduction

Git is one of a distributed version control system (DVCS) commonly used for open source and commercial software development with significant benefit for individuals, teams and businesses. DVCS allows full access to every file, branches, and interation of project. Unlike once popular centralized version control system, such as perforce, DVCS don't need a constant connection to a central repository. Developers can work anywhere and collaborate asynchronously from any time zone.

Basic Git Commands

Set your Name and Email in Git

Before working with Git on the command line, there are some basic configurations need to be set in advance.

By replacing {YOUR NAME} with your first and last name:

$ git config  --global user.name {YOUR NAME} 

Then, replaceing {EMAIL} with the email associated with your GitHub account:

$ git config --global user.email {EMAIL} 

Now, you can see your current configurations, type:

$ git config --list

Note: The method saves the credentials in plaintext on your terminal. Please use credentials before pull changes.

$ git config --global credential.helper store

Connect an existing progect with github

If you have an existing progect and want to connect with github, you can do the following and start to tract with git.

# Initialize the local directory as a Git repository.
$ git init
# Add the URL for the remote repository where your local repository will be pushed.
$ git remote add origin < repository URL >
# Verifies the new remote URL
$ git remote -v
# Push an existing repository
$ git push -u origin master

High Frequency Git Commands

  1. Show the status of the files on your branch
  2. Create a new branch
  3. Check out to your new branch
  4. Join two or more development histories together
  5. Compare the changes
  6. Add your file to the staging area
  7. See the histroy of commits
  8. Commit your file and type the commit message
  9. Push your commit to the remote and set a trackng branch
  10. Update your local copy of repository
  11. Stash your work
  12. Binary search the commit
  13. Start with submodules

Git Commands

  1. Show the status of the files on your branch:
  2. $ git status 
  3. Create a new branch:
  4. $ git branch {BRANCH-NAME}
  5. Check out to your new branch:
  6. $ git checkout {BRANCH-NAME}
  7. Join two or more development histories together:
  8. $ git merge {BRANCH-NAME}
    Example:
    # First, let's check out to the "master" branch
    $ git checkout master
    # Then, merge Dev to current branch
    $ git merge Dev
    

    Description: these two syntaxes will replay the changes made on the "Dev" into the current branch "master" since "Dev" is diverged from "master".

    Now, you can push a new commit describing the merging change, and hence the current commit is already on the top of "master".

  9. Compare the changes:
    • Show you any uncommitted changes since the last commit, type:
    • $ git diff 
    • Compare a specific file across branches, type:
    • $ git diff  {BRANCH-NAME} {OTHER-BRANCH-NAME} {FILE-NAME} 
  10. Add your file to the staging area:
  11. # Preparing to become part of the next commit
    $ git add {FILE-NAME}
    
  12. See the histroy of commits:
  13. $ git log 
  14. Commit your file and type the commit message:
  15. $ git commit -m "your message"
     
  16. Push your commit to the remote and set a trackng branch:
  17. $ git push  -u origin {BRANCH-NAME} 
  18. Update your local copy of repository:
  19. $ git pull 
  20. Stash your work:
    • Stash uncommitted changes in local repo
    • $ git stash 
    • Re-apply your stashed changes
    • # Reappling the changes and removing
      $ git stash pop
      # Reappling the changes and keeping
      $ git stash apply 
    • Manage multiple stashes
    • $ git stash list 
    • View stash diff
    • $ git stash show -p 
  21. Binary search to find the commit:
    • Use binary search to find the commit
    • $ git bitsect <subcommand> <options> 
    • Start a bisect session
    • $ git bitsect start 
    • Bisect reset
    • $ git bitsect reset
    • Bisect skip
    • $ git bisect skip
    • Show bisect log
    • $ git bisect log
      # good: [72a35907200b42246fd039d495cbef8d80fdefe3] Miscellaneous
      git bisect good 72a35907200b42246fd039d495cbef8d80fdefe3
      # good: [72a35907200b42246fd039d495cbef8d80fdefe3] Miscellaneous
      git bisect good 72a35907200b42246fd039d495cbef8d80fdefe3
      # skip: [72a35907200b42246fd039d495cbef8d80fdefe3] Miscellaneous
      git bisect skip 72a35907200b42246fd039d495cbef8d80fdefe3
      # bad: [72a35907200b42246fd039d495cbef8d80fdefe3] Miscellaneous
      git bisect bad 72a35907200b42246fd039d495cbef8d80fdefe3
        
  22. Starting with submodules:
    • Add an existing Git repo as a submodule
    • $ git submodule add   
    • Fetch and merge in the subdirectory
    • $ git submodule update --remote  
    • Show the status of the submodule
    • $ git submodule status 
    • Synchronizes submodules's remote URL configuration setting
    • $ git submodules sync --remote 

Reference

Thanks for reading! Feel free to leave the comments below or email to me. Any pieces of advice or discussions are always welcome. :)

Hope this post will help! :)