git

Some advices and useful commands for git. This is a compilation of these three web-pages. For intermediate git users.

Useful commands

NAME

git-rev-list - Lists commit objects in reverse chronological order
SYNOPSIS

git rev-list [<options>] <commit>…​ [[--] <path>…​]

git grep PATTERM $(git rev-list --all -- PATH) -- PATH # search all changes for this pattern

split feature!! clean, so you know it.



You want branch descriptions:

git branch --edit-description

This will open up your editor and let you attach metadata to the branch. You can extract it with:

git config branch.<branch>.description

A couple of important notes:

    This is stored locally. By definition it can't be pushed since it's stored in .git/config. All the same it works great for this use case.

    If you delete the branch, the description will delete as well.

    You can push this description into merge commits if you set git config --global merge.branchdesc true. This means when you issue git merge --log <branch>, it'll force the branch description into the stock merge commit message. This has a lot of uses. For example, this is how I track topic branch release notes at my employer.



https://stackoverflow.com/questions/11886132/can-i-add-a-message-note-comment-when-creating-a-new-branch-in-git

  • Coding/Actions/CI

    • Clone almost anything

    • Pull frequently

    • Commit early and often

    • Comment your commits as you would have others comment theirs

    • Push when your changes are tested

    • Branch freely

    • Merge carefully

    • Stash before switching branches

    • Use editors and IDEs that “git it” (so you can do above things easily)

    • Automate your workflow with GitHub Actions

    • Build and publish packages

    • Check and resolve your security advisories and alerts

    • Scan your code for vulnerabilities

    • Publish your documentation pages

  • Sharing

    • Fork a repo

    • Use gists to share snippets and pastes

    • Explore GitHub

    • Contribute to open source projects

    • Watch projects

    • Follow friends

    • Send pull requests

    • Create and resolve issues

    • Write informative README pages

    • Use Markdown

    • Convert your older repos to Git

    • Use GitHub project boards

    • Collaborate on documentation in wikis

  • My suggestions

    • tag your commits (adding more information to your code)

    • Include batteries (tests and examples)

  • git aliases

  • See the repository status in your terminal’s prompt (edit PS1)

  • Compare commits from the command line

  • Stashing uncommitted changes

  • Pull frequently

  • Autocomplete commands (Tab), so your loop can be faster

  • Set a global .gitignore

  • Enable Git’s autosquash feature by default

    git rebase --interactive --autosquash only picks up on commits with a

    message that begins fixup! or squash!, and Git still gives you the

    chance to to move things around in your editor like a regular interactive

    rebase.

    You can check more details in this thoughtbot blog postarrow-up-right

  • git blame

  • Add an alias to check out merge requests locally In your .gitconfig file

  • An alias of HEAD Breaking news: @ is the same as HEAD. Using it during a rebase is a lifesaver:

  • git reset / git checkout to undo changes

  • .gitconfig file

    • your ~/.gitconfig file (you need set git config your name and email for first use of git. it is saved to this file)

    • your repo's .gitconfig

  • aliases (faster command)

    • git config --global --add alias.st status, so git st will do the same as git status

    • previous command will save to the ~/.gitconfig file

    • you can also aliases to shell commands, e.g., put the following command to the alias section in your .gitconfig.

  • visializing the commit graph using this git alias

  • a nicer force-push Use git push --force-with-lease, it will not allow you to force-push if the remote branch has been updated. So you won't throw away someone else's work

  • more fine-grained add/change

  • time-based revision references

  • git rebase and git reflog if you ever found you rebased away a committed changes, you can use git reflog to find it.

  • keep it clean

    git branch --merged to get the list of merged branches. Then you can clean it up

Version history

  • v1 20200720 draft

Last updated