git
Some advices and useful commands for git. This is a compilation of these three web-pages. For intermediate git users.
Useful commands
git diff --name-only --diff-filter=U# refgit config --global alias.conflicts "diff --name-only --diff-filter=U"git rebase --onto NEW_BASE_COMMIT CHANGE_FROM_COMMIT CHANGE_END_COMMITgit clone --depth 1 --branch BRANCH_NAME REMOTE_REPO_URL# shallow clone specific branchgit remote set-branches origin '*'; git fetch -v; git checkout the-branch-i-ve-been-looking-for# https://stackoverflow.com/questions/23708231/git-shallow-clone-clone-depth-misses-remote-branchesgit fetch --unshallow# to full clone
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-gitCoding/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
git diff $start_commit..$end_commit -- path/to/file # compare changes between the commits, you can also specify HEAD@{yesterday}Stashing uncommitted changes
Pull frequently
Autocomplete commands (Tab), so your loop can be faster
Set a global .gitignore
touch ~/.gitignore git config --global core.excludesFile ~/.gitignoreEnable Git’s autosquash feature by default
git rebase -i --autosquash git config --global rebase.autosquash truegit rebase --interactive --autosquashonly picks up on commits with amessage 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.
git blame
git blame -w # ignores white space git blame -M # ignores moving text git blame -C # ignores moving text into other filesAdd an alias to check out merge requests locally In your .gitconfig file
[alias] mr = !sh -c 'git fetch $1 merge-requests/$2/head:mr-$1-$2 && git checkout mr-$1-$2' -git mr upstream 5An alias of HEAD Breaking news: @ is the same as HEAD. Using it during a rebase is a lifesaver:
git rebase -i @~2 # rebase from the second pevious to HEADgit reset/git checkoutto 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, sogit stwill do the same asgit statusprevious 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.
upstream-merge = !"git fetch origin -v && git fetch upstream -v && git merge upstream/master && git push"
visializing the commit graph using this git alias
[alias] logp = log --pretty=oneline --graph --decorate=full logt = log --pretty=oneline --graph --topo-order logd = log --pretty=oneline --graph --date-order loga = log --oneline --decorate=full lg1 = log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(bold yellow)%d%C(reset)' --all lg2 = log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(reset) %C(bold green)(%ar)%C(reset)%C(bold yellow)%d%C(reset)%n'' %C(white)%s%C(reset) %C(dim white)- %an%C(reset)' --all lg = !"git lg1"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 workmore fine-grained add/change
git add -N # then you can use git diff show differences before you use git add -a, you can check it git add -p # more fine grained git checkout -p git rebase -x/ git rebase --exec YOUR_COMMAND_TO_TEST # you can run test suit after each rebase committime-based revision references
git diff HEAD@{yesterday} # compare current HEAD with the HEAD of yesterday git diff HEAD@{'2 months ago'} git diff HEAD@{'2010-01-01 12:00:00'}git rebaseandgit reflogif you ever found you rebased away a committed changes, you can usegit reflogto find it.keep it clean
git branch --mergedto get the list of merged branches. Then you can clean it up
Version history
v1 20200720 draft
Last updated
Was this helpful?