avatar khanh than

SOFTWARE ENGINEER

To keep tracking my skills & accomplisments

01 Nov 2024

● Dev Tools

Git And Github

Git (based on C) is a powerful, open-source version control tool that allows developers to track changes, collaborate, and manage project history efficiently. While GitHub (based on ruby-on-rails) is the popular platform for hosting Git repositories.

Author: Git was created by Linus Torvalds in 2005, who also developed Linux in 1991. Github was built by Tom Preston-Werner, Chris Wanstrath, PJ Hyett, and Scott Chacon. Microsoft acquired GitHub in 2018.

These are some common helpful commands of git:
(Warning: we should use a new branch named “dev” to develop or code, which allows us to do the code review before merging.)

  1. git pull (git pull origin Branchname): updates your current local branch by fetching the latest changes from the corresponding remote branch and merging those changes into your local branch.

  2. git clone GithubURL : clone a github repository down to local machine.

  3. git remote –v : show the github link.
    • git remote add origin Github_Url: set a remote origin for local directory.

  4. git add . : stage or track all changes at current time before committing and pushing.
    • git add Filename : stage only one file.
    • git add ––all : will include deleted files also.

  5. git commit –m “messages” : add a short title to all the changes (added by the latest command “git add”) before pushing.
    • git commit –amend : modify the most recent commit.
  6. git push (git push origin Branchname): push all the changes (committed by all commands “git commit”) to the remote repository at Github.
    Note: We should use a new branch named “dev” to develop new features, which allows us to do the code review later.

  7. git init : Initialize “git” for current directory. (a .git file & log files is added to it).

  8. git status : show the staged files or other status of working directory.
    • git show commitID : show the information or changes of a commit.

  9. git checkout BrName : switch to another branch named “BrName”.
    • git checkout -b BrName: create, copy, then switch to a new branch named “BrName” from current branch.
    • git checkout -b BrName main: create, copy, then switch to a new branch named “BrName” from “main” branch.

  10. git branch -d BrName : delete a branch.
    • git branch -m newBrName : rename the current branch.

  11. git merge : merge a specific branch to current Branch.
    • git checkout targetBranch : enter the target_branch
    • git merge featureBranch : merge feature_branch to the target_branch.

  12. git log : display a list of commits in an order.
    • git log –oneline –reverse: display only the first line in reversed order.

  13. git branch -a : show all local and remote branches.
    • git branch -r : show only remote branches. (remote branch starts with “origin/”).

  14. git fetch : fetch all remote branches to local machine without auto merging like “git pull”. This requires some following commands:
    • git fetch origin remoteBranch : fetch only one specific remote branch. This requires us to know the name of the remote branch (use: git branch -r).
      -> git checkout remoteBranch : switch to the remoteBranch.
    • git merge origin/main : After fetching, we have to do merging ourselves.
      Shorter alternative :
    • git checkout -b newBranch origin/remoteBr : create, then copy a remote branch into a new branch. This is useful to do the review code at local.

  15. git diff : display the difference between local unstaged changes and the lasted cloned or fetched version.
    • git diff main origin/main : difference between local main and remote main after fetching.
  16. git blame Filename : report which users changed which parts of a file.

  17. git bisect : use binary search to find the closest commit that introduced a bug. The process is below:
    1. git bisect start : start the searching.
    2. git bisect bad : set current version is bad.
    3. git bisect good v2.6.13-rc2 : set the commit v2.6.13-rc2 to be good (use “git log” to display list of commit IDs).
    4. git bisect bad : this command will “check out” back one step to the previous version, then we could inspect whether the bug issue is still existing or not.
    5. If the bug is still there, “git bisect bad” again to “check out” back another step to the more previous version, inspect the bug issue again. Continue until we find the closest version that the bug issue does not happen. Now we know the commit after this version is a place where the bug issue happened for the first time. Use “git show commitID” to check the changes inside it.
    6. git bisect reset: cancel the searching session, then fix the bug.

  18. git rebase : keep commit history clean and clear by allowing to combine several commits into one by “squashing” technique, then force pushing again. (Note: should not use for a group branch).
    • git rebase -i HEAD~5 : tell git that we want to rebase the last five commits. A window editor will appear, replace the word “pick” with “squash” on the commit we want to squash into one commit.
    • Edit a new commit message for the commit that will replaces all the combined commits.
    • git push origin featureBranch -f : to force push the change the commit history at Github.

  19. git reset : undo “git add” and “git commit” if they have not been pushed yet:
    • git reset –soft HEAD~1 : undo the last commit (git commit) but keep change staged (no touch to “git add”).
    • git reset HEAD~1 : undo the last commit and also unstage changes. (both git commit and git add).

  20. git revert : undo a commit by creating a new commit, even when the commit was pushed already:
    • git revert a8a1f7b: will undo all changes made in the commit with ID a8a1f7b and create a new commit at the same time. You will need to push this new commit to the remote repository.
Share on: