Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

:octocat: 💭 A collection of git tipshttps://gitpitch.com/ericat/git-tips/master?grs=github&t=white#

NotificationsYou must be signed in to change notification settings

ericat/git-tips

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A collection of git-related tips, tricks and best practices

Copycat @Octocats

Table of Contents

Standards and Best Practices

  • Commit early and often
  • Keep branches short-lived
  • Keep branches up-to-date
  • Rebase when possible
  • Enforce naming standards

More ongit best practices.

Commits

  • Use consistent casing in the subject line
  • Do not end the subject line with a period
  • Use imperative mood in commit messages
  • Limit the subject line to 50 characters
  • Use a body

The commit should look like this:

<title><BLANK LINE><body>

Git uses the imperative mood when merging or reverting:

GoodAdd validation error msg

Badadding validation error msg

You can start your commit with either a lower case or an uppercase letter, but be consistent.

Lowercase works well withlabels, for example:

docs(changelog): update change log to beta.5

This is a handy table for labelsAngular's commit standards and guidelines:

LabelDescription
featA new feature
fixA bug fix
styleChanges that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)
refactorA code change that neither fixes a bug nor adds a feature
perfA code change that improves performance
testAdding missing or correcting existing tests
choreChanges to the build process or auxiliary tools and libraries such as documentation generation
docDocumentation only changes

You should also use abody to explain what the commit does. A good idea is to reference the JIRA / Github issue, for example:

fix: validation on text inputThis fixes the validation on the text input, broken in commit a05bcd3.Closes SEWA-104.

You can set up your editor to open a standard template on commit. Find instructionshere.

Naming Branches

  • Prefix withfeature orfix labels;
  • Use lowercase;
  • Include JIRA issue;
  • Separate words with either slash, underscore or hyphen (avoid mix and match)

Goodfeat/SEWA-104/react-barcodes

Badfeat_SEWA_104_React-barcodes_Step-2


Setup

Open your default editor on every commit:

git config --global core.editor "code --wait"git config --global core.editor "atom --wait"git config --global core.editor "subl -n -w"git config --global core.editor "vim"

Use a template for your commit messages

Create a file called~/.gitmessage.txt in your home directory with the following content:

Subject line<blank line>Body.[JIRA-XXX]

Tell git to use it:

git config --global commit.template ~/.gitmessage.txt

Ignore Files

You can ignore files locally or globally by adding them to:

  • your repo's.git/info/excludesfile
  • a local.gitignore
  • your.gitignore_global in your home directory

Then point thecore.excludesfile setting in your.gitconfig to it:

[user]name = Kittyemail = kitty@ticketmaster.co.uk[core]excludesfile = /Users/kitty/.gitignore_global

The.gitignore_global:

npm-debug.lognode_modulescoverage.DS_Store

Autocorrect Typos

git config --global help.autocorrect 1

This will correct your typos:

git checkotuWARNING: You called a Git command named 'heckout', which does not exist.Continuing under the assumption that you meant 'checkout'in 0.1 seconds automatically...README.md

Git by Task

Commit Code

The first commit of a repository cannot be rebased like regular commits, soit's good practice to create an empty commit as your repository root:

git commit -m "root" --allow-empty

Amend the latest message, if you haven't pushed:

git commit --amend -m "New commit message"

If you forgot to add a file to the latest commit, you can still reuse the samecommit message:

git add filenamegit commit --amend --no-edit

Stash Code

Useful when something needs a quick fix, but you are not finishedwith what you were doing:

git add -agit stashgit stash apply

Some cleanup:

git stash listgit stash clear

This will apply and delete from the stack:

git stash pop

Inspect your changes

Diffing code:

git diff origin/develop  // see changes that develop does not havegit diff origin/develop <filename> // can also pass a file namegit diff HEAD // compare with staged changesgit diff -w   // see changes without indent changesgit diff --cached // see diffs for files already in the staging area

Search Code

git log -3 // show the last 3 commitsgit log --after="2014-7-1"git log --after="yesterday" // and also "1 week ago" or --before

A list of examples:

git log —onelinegit log -3git log --author="ericat"git log -p -S"Math" // code changes that include "Math", the -p also includes the code changesgit log -p -G <regex>git log --no-mergesgit log --grep="PD-6300"git log —all --grep="PD-6300"git log --all --oneline --decorate --author="Erica" --since="1.week"git log --author=“Ericat|Rebecca"git log — webpack.config.babel.js // show the history of changes for a particular file. Can omit — if there is no risk of mixing it up with a branchgit log master..wip-star-items // see all changes in wip that are not in master.git shortlog // see history without hashes
Pagination when logging

Whengit log shows a: at the bottom, it means paginated results. You can navigate with:

B (back)F (forwards)

or simply j k.

You can also search within the pagination with/searchterm


Some Housekeeping

List all branches that have already been merged intomaster:

git branch --merged master

Delete them:

git branch --merged develop | grep -v 'master$' | xargs git branch -d

Delete local branches that have been deleted remotely:

git prune

Fetch and purge old data, making sure everything is up to date:

git fetch -p

Git Troubleshooting

Fix a detached head

Simply chekout the current branch. If you are on a detached HEAD from develop, do:

git checkout develop

Fatal: xx cannot be resolved to branch

This may happen if you create a branch with a similar name to another, but with different casing.

For example:

fix/SEWA-776/ism-tool-tip andfix/sewa-776/seatmap-tooltip

The error you may get:

fatal: fix/sewa-776/seatmap-tooltip cannot be resolved to branch.fatal: The remote end hung up unexpectedly

To fix, rename.git/refs/head/SEWA-776 to.git/refs/head/sewa-776.


Git By Example

Switch to previous branch

git checkout -

Use grep

Find a list of files containing a CSS variable with grep

Bonus: open them in vim

git grep —name-only \$tint-white | xargs vi

Abort a merge

git reset --hard HEAD

Checkout a specific version of a file:

git checkout <hash> -- <file_path>

Show file in other git branch

git show fe-tests:test/acceptance/sell.js

Pick a file from another branch/commit

git checkout <hash> -- <path_to_file>

Pick a file from another branch/commit (that does not exist in the current branch)

git checkout <other_branch> — <path_to_file>

Pick a file from another branch but rename it

git show <branch>:<path_to_file> > <new_path_to_file>

Find branches who are not yet merged to develop

git branch --no-merge develop

Find out which branch contains a specific commit

git branch -a —contains <hash>

See which branch a commit belongs to

git log --all --source --oneline

List all dev working on a project

git shortloggit shortlog -s -n -egit shortlog -sn // list devs with n of commits

See only meaningful changes without whitespace in diffs

git diff -w

This is useful if you suspect Git has been converting line endings.

See changed words when editing prose

git diff --word-diff

View all global settings

git config --global -lgit config --list

Check parent of a merge and file changes

git show --pretty=raw <hash>

Checking history of a file

git log -- package.jsongit blame package.jsongit blame -L150 package.jsongit blame -L150,+10 package.json

Find out which remote branch a local branch is tracking

git branch -vv

Update your remote

git remote set-url origin <url>

Find the commit where the branch was started

Visually, through the command line:

git log --graph --oneline --all --decorate

Through a few other commands:

git reflog --date=local | grep branchnamegit cherry -v develop // finds the diff between your branch and develop

same as:

git log --oneline feat/JIRA-687-react-input ^developgit log develop..master // show all commits that your branch have that are not yet in master

Add everything but whitespace changes

git diff --ignore-all-space | git apply --cached

Find a commit that touches a particular snippet of code

git grep

The above will output a list of files that contain a particular snippet.

Need to remove some files from a previous commit

git reset —soft HEAD^

Checkout a new branch from a hash

If you want to checkout and old version of your code, you can do it in another branch:

git checkout -b test-branch 56a4e5c08

Check if a rebase is in progress

You can check whether a rebase is in process by looking for the directory.git/rebase-merge/.

Checkout only part of a file

git checkout -p (<filename>, optional)

When you've committed yet anotherconsole.log (you could also use a linter :P)...

Some JIRA Assistance

Find out what has changed in the past two weeks (sprint goals?):

git log --since='2 weeks ago' --oneline

What have you done last week? #timesheets

git log --all --oneline  --author='Erica' --since='1.week'

Grep for a ticket name:

git log --grep='PD-6300'git log —all --grep='PD-6300'

[8]ページ先頭

©2009-2025 Movatter.jp