Post

Git Study Notes

Git Local

Git is a powerful tool for version control, not just for code files but also for other text files.

1. Visualize Commit Tree

  • Keep a mental graph of the commit tree in mind.
  • Practise visualizing commit trees using interactive tools like Learn Git Branching

2. Branching and Merging

branch early, and branch often.

  • Branch is lightweight and useful. Create and switch to a new branch: git checkout -b branch.
  • Create a new commit that merge the modification in BRANCH to current branch: git merge BRANCH.

3. Refer to a commit

  • Each Git commit has a unique hash identifier. You can use a prefix of the hash to indicate a specific commit. The prefix can be arbitrarily short as long as it remains unique.
  • “HEAD” points to the currently checked out commit. It automatically updates to the latest commit on the current branch.
  • The name of a branch would also point to it’s current commit.
  • HEAD^ refers to the commit before “HEAD.” HEAD^2 refers to the second parent of “HEAD.”
  • main~10 refers to the 10th commit before the “main” branch. main^^3~5 refers to the 5th commit before the third parent of the commit 1 step before main.

4. Resetting and Reverting

  • Move the current branch to a specific commit: git reset COMMIT.
  • Create a new commit that undoes changes: git revert COMMIT. Reverting a commit ancestor will reverse the changes between them.

5. Rebasing

  • Move “BRANCH1” and its modifications to become a descendant of “BRANCH2”: git rebase BRANCH2 BRANCH1
  • Open an interactive rebasing UI: git rebase -i.

6. Cherry-picking

  • Apply specific commits to the current branch: git cherry-pick COMMIT1 COMMIT2.

7. Tag

  • Add a tag for a commit: git tag NAME COMMIT.
  • Get the nearest ancestor tag and distance: git describe COMMIT.

Git Remote

Git remote allow users to store and share files on internet.

1. Clone

  • Clone a remote repository: git clone LINK. This Note introduces how to use SSH protocol LINK.

2. The “remote” branch on local

  • There would be “REMOTE/BRANCH” branch(es) after clone(e.g. origin/master).

  • The “REMOTE/BRANCH” would be the same as the branch in remote repository at last “fetch”.

  • Synchronize the remote branch on local with remote repository: git fetch.

3. Pull and Push

  • Fetch and merge: git pull <REMOTE/BRANCH> <BRANCH>. Fetch and rebase: git pull --rebase.

  • Push local to remote: git push <REMOTE/BRANCH> <BRANCH>.

4. Tie local and remote branch manually

  • Create and tie: git checkout -b BRANCH REMOTE/BRANCH

  • Tie: git branch -u REMOTE/BRANCH <BRANCH>

5. Extra Parameters

  • Push the last commit of branch foo to the main branch in remote origin: git push origin foo^:main.

  • Remove the foo branch: git push origin :foo.

This post is licensed under CC BY 4.0 by the author.