[ JagoPG Site ]
This publication is more than a year old. The information can be out-dated.

Git Cheat Sheet

BASICS

  • git clone <URL> download a remote repository
  • git add -A add to the stash all changes
  • git commit commit changes in the stash
  • git checkout <BRANCH> move to a branch
  • git push <ORIGIN> upload all commits to the repository with the name ORIGIN

  • git fetch download a list of the changes in the repository, but do not apply them

  • git merge <BRANCH> apply changes from a branch to the current branch

These commands are equivalent: git pull ~ git fetch && git merge

  • git rm —cached <FILE> removes a file previously added to the repository ## STASH git stash apply && git stash drop ~~ git stash pop

NAVIGATE THROUGH GIT HISTORY

By default Git uses Less application for reading data. These are some commands you can use:

  • Navigation:
    • g move one line forward
    • k move one line backward
    • F go one page forward
    • B go one page backward
    • CTRL F go forward the same amount of lines that the screen can show
    • CTRL B go backward the same amount of lines that the screen can show
  • Search commands:
    • /<TEXT> search a text
    • n move to the next search result
    • N move to the previous search result

FORMAT GIT HISTORY

Several of the command arguments can be mixed. These arguments can be used in other commands as diff too:

  • git log --oneline shows the commit ID and the commit message
  • git log —-decorate references to the branches are also shown
  • git log —-graph an ASCII graph is drawed representing branch activity
  • git log -p shows changes made in each commit
  • git log -—stat the amount of the code added and removed is shown
  • git log -3 shows latest 3 commits

Show commits perfomrmed from a specific date:

  • git log —after="three days ago"
  • git log —before="yesterday"

Commit filtering

  • git log —author="jagoba@pm.me" filters by commit author
  • git log —grep="message" filters by commit message
  • git log -S"VALUE" searches some value in the files contained in the commits
  • git log -G"regex" searches commits whose patch text matchesa a REGEX
  • git log -i -S"value" modifier for ignoring commits matching the filter

  • git log -—no-merges shows commits that are not merges

  • git log branch1..branch2 lists commits that diverge between two branches
  • git log FILE shows commits including a file

DIFF

  • git diff -—cached shows changes between the stash and stage area
  • git diff HEAD shows changes between the stage area and HEAD
  • git diff <branch> shows changes between branches
  • git diff <branch> <file> shows changes between branches and its files

BLAME

  • git blame <file> shows who has modified files, in a certain date. The commit ID is shown.

TAG

  • git tag <name>creates a only-read reference to a commit. Normaly it is used to set a version to an app.
  • git push --tags <origin> Upload all tags to the repository
  • git tag -a v2.0.1 -m “Message” annotates version and adds information about the commit

Application version standard

v.<MAJOR>.<MINOR>.<PATCH>

  • MAJOR: when a change to the application causes a break with previous versions (i. g.: API methods that are removed)
  • MINOR: small changes that includes new features.
  • PATCH: bug fixes

CLEAN COMMITS

Through this process several commits are joined in one commit.

The following command will begin a rebase in the branch where the changes are going to be uploaded: git rebase -i <branch> The command will open the default git editor, where you should write "pick" to the commit you want to use as identifier, and write "squash" to the commits that you want to clean. After saving the file, the rebase will start.

A rebase similar to a merge. The difference is that a rebase modifies the Git history, by putting the <BRANCH> commits before your branch commits, and then your branches' commits are put after.

A merge puts all the commits from the <BRANCH> on the top of your branch.

As the rebase modifies the Git history, you will have to force the push to your branch to upload the changes: git push <ORIGIN> <BRANCH> -f.

You can stop the rebase and reset to the initial state by: git rebase --abort

FIND COMMITS THAT BROKE SOMETHING

The bisect tool allows to the developer to find in a group a commits, the commit that has added a bug to the application:

  1. Move to the latest commit that had not the bug
  2. Start the bisect process: the tool will start moving from commit to commit. You should test whether the current commit includes the bug or not.
  3. When no more commits are left, a binary search is made to find the exact commit that has introduced the bug
  • git bisect start start the bisect process
  • git checkout <COMMIT ID> moves to a specific commit
  • git bisect bad marks the current commit as "having a bug"
  • git bisect good <COMMIT_ID> marks the current commit as good commit, or the bug is not in this branch
  • git bisect reset come back to the initial state

GIT HOOKS

At the .git/hooks folder that is included in the Gitrepository, different tasks can be specified when a Git phase is reached. The tasks are only performed in the local machine. For uploading this scripts to the server, you should use another tool like npm.

Here is an example of adding a lint checker before the commit is performed:

$ echo "npm test && npm run lint" > .git/hooks/pre-commit
$ chmod +x .git/hooks/pre-commit
$ git commit

SETUP GLOBAL GIT ENVIRONMENT

By default, the file ~/.gitconfig contains the global configuration of Git.

  • git config --global <parameter> <value> changes a parameter value in the global configuration
  • git config —-list list of modified parameters
  • git config —global alias.graph "log —graph --oneline" creates an alias of a Git command called graph
  • git graph

 Global Git Ignore file list

  • git config —global core.excludesfile ~./.gitignore_global

The files that you want to ignore in all your repositories have to be annotated in this file: ~/.gitignore_global