Git Cheat Sheet
BASICS
git clone <URL>download a remote repositorygit add -Aadd to the stash all changesgit commitcommit changes in the stashgit checkout <BRANCH>move to a branchgit push <ORIGIN>upload all commits to the repository with the name ORIGINgit fetchdownload a list of the changes in the repository, but do not apply themgit 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 ## STASHgit 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:
gmove one line forwardkmove one line backwardFgo one page forwardBgo one page backwardCTRL Fgo forward the same amount of lines that the screen can showCTRL Bgo backward the same amount of lines that the screen can show
- Search commands:
/<TEXT>search a textnmove to the next search resultNmove 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 --onelineshows thecommit IDand the commit messagegit log —-decoratereferences to the branches are also showngit log —-graphan ASCII graph is drawed representing branch activitygit log -pshows changes made in each commitgit log -—statthe amount of the code added and removed is showngit log -3shows 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 authorgit log —grep="message"filters by commit messagegit log -S"VALUE"searches some value in the files contained in the commitsgit log -G"regex"searches commits whose patch text matchesa a REGEXgit log -i -S"value"modifier for ignoring commits matching the filtergit log -—no-mergesshows commits that are not mergesgit log branch1..branch2lists commits that diverge between two branchesgit log FILEshows commits including a file
DIFF
git diff -—cachedshows changes between the stash and stage areagit diff HEADshows changes between the stage area and HEADgit diff <branch>shows changes between branchesgit 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 repositorygit 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.:
APImethods 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:
- Move to the latest commit that had not the bug
- 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.
- When no more commits are left, a binary search is made to find the exact commit that has introduced the bug
git bisect startstart the bisect processgit checkout <COMMIT ID>moves to a specific commitgit bisect badmarks 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 branchgit bisect resetcome 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 configurationgit config —-listlist of modified parametersgit config —global alias.graph "log —graph --oneline"creates an alias of a Git command called graphgit 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




