Basic Git commands on Windows / Linux

Git is a distributed version-control system for tracking changes in source code during software development. It is designed for coordinating work among programmers, but it can be used to track changes in any set of files. Its goals include speed, data integrity, and support for distributed, non-linear workflows – “From Wikipedia”.
Being a developer, we would use git to track our codes daily. This post is for beginners who start to use git on Windows/Linux.

Pre-requisites

– Install Git bash on Windows
– Install Git on Linux

How git works ?

  1. Create a new repository : git init
  2. Check out a repository: 
    • create a working copy of a local repository : git clone /path/to/repository
    • using remote server: git clone username@host:/path/to/repository
  3. Add & Commit changes: 
    • Firstly, its workflow: working directory (hold the actual files -> Index (act as staging area) -> HEAD (last commit made)
    • In order to add, should use: git add filename | git add * (add it to Index)
    • Finally, commit changes, use git commit -m “commit message” (the file committed to HEAD but NOT in the REMOTE repository yet)
  4. Push changes to remote repository
    • git push origin <branch>
    • if you have not cloned an existing repository yet, and want to connect the repository to a remote server: git remote add origin <server>
  5. Update & merge changes
    • To update local repository: git fetch -all or git pull
    • To merge branch: git merge <branch>
    • you are responsible to merge those conflicts manually by editing the file shown by git, after changing, you need to mark them as merged with git add <filename>
    • Before merging, you can preview: git diff <source branch> <target branch>
  6. Create a “branch” (version), make a change, and commit the change
    • Firstly, branch are used to develop features isolated from each other. the master branch is a default branch when we create a repository. Use other branches for the development and merge them back to the master branch upon the completion,
    • So, in order to create a new branch: git checkout -b <feature_x>
    • To switch back to master branch : git checkout master
    • If you would like to delete the branch : git branch -d feature_x
    • Finally, push the branch to a remote repository: git push origin <branch>

Other things

  1. Open a “pull request” / merge requests (propose changes to the master branch)
  2. Tagging:
    • create tag for software release: git tag 1.0.0 
  3. Log: repository history
    • Firstly, to show only the commits of an author: git log –author=Thu
    • Secondly, in order to view an ASCII art tree of all branches, decorated with the name of tags and branches: git log –graph –oneline — decorate –all
    • Third, to display which files have changes: git log –name-status
    • Finally, to see more parameters to use: git log –help
  4. Replace local changes with two following cases:
    • Case 1: In case you did wrong something, you can replace local changes: git checkout filename (this replaces changes in your working tree with the last content in HEAD),
    • Case 2: if you drop all your local changes and commits, fetch latest history from the server and point your local master branch : 
      • git fetch origin 
      • git reset –hard origin/master