Wilhel

Wilhel

Version Control (Git)

Git Command Line Interface#

Pro Git English Version: https://git-scm.com/book/en/v2

Basics#

  • git help <command>: Get help information for a git command
  • git init: Create a new git repository, with its data stored in a directory named .git
  • git status: Show the current repository status
  • git add <filename>: Add a file to the staging area
  • git commit: Create a new commit
  • git log: Show the commit history
  • git log --all --graph --decorate: Visualize the commit history (directed acyclic graph)
  • git diff <filename>: Show the difference between a file and the staging area
  • git diff <revision> <filename>: Show the difference between two versions of a file
  • git checkout <revision>: Update HEAD and the current branch

Branches and Merging#

  • git branch: Show branches
  • git branch <name>: Create a branch
  • git checkout -b <name>: Create a branch and switch to it
    • Equivalent to git branch <name>; git checkout <name>
  • git merge <revision>: Merge into the current branch
  • git mergetool: Use a tool to handle merge conflicts
  • git rebase: Rebase a series of patches onto a new base

Remote Operations#

  • git remote: List remotes
  • git remote add <name> <url>: Add a remote
  • git push <remote> <local branch>:<remote branch>: Transfer objects to a remote and update remote references
  • git branch --set-upstream-to=<remote>/<remote branch>: Create an association between a local and remote branch
  • git fetch: Get objects/refs from a remote
  • git pull: Equivalent to git fetch; git merge
  • git clone: Download a repository from a remote

Undoing#

  • git commit --amend: Edit the content or message of a commit
  • git reset HEAD <file>: Unstage a file
  • git checkout -- <file>: Discard changes
  • git restore: Replaces git reset for many undo operations starting from git 2.32

Advanced Git Operations#

  • git config: Git is a highly customizable tool
  • git clone --depth=1: Shallow clone, excluding complete version history
  • git add -p: Interactive staging
  • git rebase -i: Interactive rebase
  • git blame: View the person who last modified a line
  • git stash: Temporarily remove modifications from the working directory
  • git bisect: Search the commit history using binary search
  • .gitignore: Specifies intentionally untracked files

Miscellaneous#

  • Graphical User Interface: There are many graphical user interface clients for Git, but we prefer using the command line interface ourselves
  • Shell Integration: Integrating Git status into your shell can be very convenient (zsh, bash). Frameworks like Oh My Zsh generally include this functionality
  • Editor Integration: Similar to the previous point, integrating Git into your editor can be beneficial. fugitive.vim is a popular plugin for integrating Git in Vim
  • Workflows: We have covered the data model and some basic commands, but we haven't discussed some conventions when working on large projects (there are many different approaches)
  • GitHub: Git is not the same as GitHub. In GitHub, you need to use a method called pull request to contribute code to other projects
  • Other Git Providers: GitHub is not the only one. There are platforms like GitLab and BitBucket.

Resources#

Exercises#

  1. If you have never used Git before, we recommend reading the first few chapters of Pro Git or completing tutorials like Learn Git Branching. Focus on Git commands and the data model.
  2. Fork the repository of this course website
    1. Visualize the version history and explore it
      git log --all --graph --decorate
      
    2. Who was the last person to modify the README.md file? (Hint: Use git log with appropriate parameters)
      git log -1 README.md
      
      • -x option: View the latest x commits or version information of a specific file
    3. What is the commit message when the collections: line in the _config.yml file was last modified? (Hint: Use git blame and git show)
      git blame _config.yml | grep collections
      git show --pretty=format:"%s" a88b4eac | head -1
      git log --pretty=format:"%s" a88b4eac -1
      
  3. One common mistake when using Git is committing large files that should not be managed by Git or committing files with sensitive information. Try adding a file to the repository and making a commit, then remove it from history (this article may help).
    1. First, commit some sensitive information
      echo "password123">my_password
      git add .
      git commit -m "add password123 to file"
      git log HEAD
      
    2. Use git filter-branch to clean up the commit history
        git filter-branch --force --index-filter\
        'git rm --cached --ignore-unmatch ./my_password' \
        --prune-empty --tag-name-filter cat -- --all
      
  4. Clone a repository from GitHub, make some changes. What happens when you use git stash? What does git log --all --oneline show? When would you use git stash pop to undo a git stash operation?
  5. Like other command-line tools, Git provides a configuration file called ~/.gitconfig (or dotfile). Create an alias in ~/.gitconfig so that when you run git graph, you get the output of git log --all --graph --decorate --oneline.
  6. You can create global ignore rules by executing git config --global core.excludesfile ~/.gitignore_global and configuring your global gitignore file to automatically ignore temporary files from the system or editor, such as .DS_Store.
  7. Clone the repository of this course website from GitHub and look for any typos or areas for improvement. Make a pull request on GitHub to contribute your changes.
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.