Skip to content

Instantly share code, notes, and snippets.

@carloswm85
Last active March 15, 2024 01:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carloswm85/cf1e27778cd5ccdc7cbafff1f94fa4c1 to your computer and use it in GitHub Desktop.
Save carloswm85/cf1e27778cd5ccdc7cbafff1f94fa4c1 to your computer and use it in GitHub Desktop.

GIT

  • main branch is always used in production.

Tutorials:


Git Commands

Useful Commands

Command Description
git checkout -b new_branch New branch.
git checkout -- . Discard changes in the working directory and revert it to the state of the last commit.
git pull --rebase -
rm -rfv .git -
git switch daves_branch Pull my remote branch.
git rm anyFile --cached, then commit Forcing files into .gitignore

| `` | |

Repository management

Commands used for creating, initializing, cloning, and configuring Git repositories. Examples include git init, git clone, git config.

Command Description
git init Create an empty Git repository or reinitialize an existing one
git clone Clone a repository into a new directory
git config Get and set repository or global options
git remote Manage set of tracked repositories
git fetch Download objects and refs from another repository
git pull Fetch from and integrate with another repository or a local branch
git push Update remote refs along with associated objects
git archive Create an archive of files from a named tree
git submodule Initialize, update or inspect submodules
git worktree Manage multiple working trees
git fetch -v --dry-run check if pull needed in Git
git config --get remote.origin.url display information
git remote show origin full information

Branching and merging

Commands used for creating, listing, switching, merging, and deleting branches. Examples include git branch, git checkout, git merge.

Command Description
git branch List, create, or delete branches
git checkout Switch branches or restore working tree files
git merge Join two or more development histories together
git rebase Reapply commits on top of another base tip
git cherry-pick Apply the changes introduced by some existing commits
git revert Revert some existing commits
git reset Reset current HEAD to the specified state
git tag Create, list, delete or verify a tag object signed with GPG
git describe Give an object a human readable name based on an available ref
git reflog Manage reflog information

Committing and tracking changes

Commands used for adding, committing, and tracking changes to the repository. Examples include git add, git commit, git status.

Command Description
git add Add file contents to the index
git commit Record changes to the repository
git diff Show changes between commits, commit and working tree, etc
git status Show the working tree status
git log Show commit logs
git blame Show what revision and author last modified each line of a file
git bisect Use binary search to find the commit that introduced a bug
git grep Print lines matching a pattern
git stash Stash the changes in a dirty working directory away
git cherry Find commits yet to be applied to upstream

Reverting and resetting changes

Commands used for undoing changes to the repository. Examples include git revert, git reset.

Command Description
git reset Reset current HEAD to the specified state
git checkout Switch branches or restore working tree files
git revert Revert some existing commits
git clean Remove untracked files from the working tree
git reflog Manage reflog information
git revert --abort Cancel the revert operation and reconstruct the original HEAD
git reset --soft Uncommit changes, changes are left staged (index). Scope: Head only, index and working directory unchanged
git reset --mixed Default value. Uncommit + unstage changes. Changes are left in working tree. Scope: Index + Head, changed
git reset --hard Reset the current branch to the specified commit and discard all changes. Uncommit + unstage + delete changes. Nothing is left. Scope: Working directory + Index + HEAD, all changed
git checkout -b <new-branch> <start-point> Create a new branch and switch to it
git reset HEAD <file> Remove the specified file from the staging area
git revert -m <parent-number> <commit> Revert a merge commit

Stashing and cleaning changes

Commands used for temporarily storing changes and cleaning up the repository. Examples include git stash, git clean.

Command Description
git stash Stash the changes in a dirty working directory away
git stash list List all stashed changesets
git stash apply Apply the changes recorded in the specified stash
git stash pop Apply and remove the changes recorded in the most recent stas
git stash drop Remove the changes recorded in the specified stash
git stash clear Remove all stashed entries
git clean Remove untracked files from the working tree
git clean -f Remove untracked files from the working tree forcefully
git clean -d Remove untracked directories in addition to untracked files
git clean -x Remove only files ignored by Git

Collaboration

Commands used for collaborating with other developers on a Git project. Examples include git push, git pull, git fetch.

Command Description
git clone Clone a repository into a new directory
git push Update remote refs along with associated objects
git pull Fetch from and integrate with another repository or a local branch
git fetch Download objects and refs from another repository
git remote Manage set of tracked repositories
git branch -r List the remote branches
git merge Join two or more development histories together
git rebase Forward-port local commits to the updated upstream head
git tag Create, list, delete or verify a tag object signed with GPG
git cherry-pick Apply the changes introduced by some existing commits

Tagging

Commands used for creating, listing, and deleting tags in a Git repository.

Command Description
git tag Create, list, delete or verify a tag object signed with GPG
git tag -l List tags
git tag -a Create an annotated tag object
git tag -d Delete a tag object
git describe Describe the commit that is the ancestor of the current revision a specified tag
git push --tags Push tags to a remote repository
git show Show information about a commit, tag, or tree
git log --tags Show commits that are referenced by refs/tags
git rev-list --tags List commits that are referenced by refs/tags
git push <remote> <tagname> Push a single tag to a remote repository

Miscellaneous

Other commands that don't fit into the above categories. Examples include git log, git diff, git grep.

Command Description
git status Show the working tree status
git config Get and set repository or global options
git log Show commit logs
git diff Show changes between commits, commit and working tree, etc
git grep Print lines matching a pattern
git blame Show what revision and author last modified each line of a file
git mv Move or rename a file, a directory, or a symlink
git rm Remove files from the working tree and from the index
git archive Create a tar or zip archive of the contents of a tree
git help Display help information about Git

EXTRA

diff

Command Description
git diff Compares changes: locally working/staging
git diff master origin/master Compares changes: local/remote
git diff --staged Compares changes: staging/repository
git diff --color-words Color changes made in a diff

log

Command Description
git log Logs all commit history
git log -1 Logs last one commit
git log -n 5
git log --oneline
git log --grep="bug" prints out any commit with the string 'bug'
git show Shows last commit and content, add as last parameter what you want to find, let's say the SHA

Settings

Initial Configuration

git config --global <variable> <value>

Variable Value
user.name "UserName"
user.email "my@mail.com"
alias.lg "log --oneline --decorate --all --graph"
alias.s "status -s"
`` ``

Current Configuration

Command Description
git config --list --show-origin
git config --local --list Repository level
git config --global --list Global Git, at OS user level
git config --system --list Entire machine, all users and repositories

CONVENTIONS

How To Write Commits

  1. Lowercase and Punctuation: Do not end in punctuation. All lowercase.
  2. Mood: Imperative in subject line, which gives the tone you are giving an order. Presente tense.
  3. Type of Commit: Specify the type of commit.
  4. Length: First line no more than 50 characters. Body restricted to 72 characters. Separate subject from the body with a blank line.
  5. Content: Be direct, try to eliminate filler words and phrases. Use the body to explain what and why vs. how.

Types of Commits

  • build – changes that affect the build system or external dependencies
  • chore – changes that do not relate to a fix or feature and don't modify src or test files (for example updating dependencies)
  • ci – continuous integration related
  • docs – updates to documentation such as a the README or other markdown files
  • feat – a new feature is introduced with the changes
  • fix – a bug fix has occurred. Add tiker number when possible
  • perf – code change that improves performance
  • refactor – refactored code that neither fixes a bug nor adds a feature
  • style – code formatting such as white-space, missing semi-colons, and so on.
  • test – including new or correcting previous tests
  • revert – reverts a previous commit
  • update – What is this for?

'Conventional Commit' Structure

<type>[optional scope]: <description>
[optional body]
[optional footer(s)]

'Conventional Commit' Example

fix: fix foo to enable bar
This fixes the broken behavior of the component by doing xyz.
BREAKING CHANGE
Before this fix foo wasn't enabled at all, behavior changes from <old> to <new>
Closes D2IQ-12345

Good Examples

  • feat: improve performance with lazy load implementation for images
  • chore: update npm dependency to latest version
  • fix: bug preventing users from submitting the subscribe form
  • update: incorrect client phone number within footer body per client request

Bad Examples

  • fixed bug on landing page
  • Changed style
  • oops
  • I think I fixed it this time?
  • 'empty commit messages'

Links


Workflows

GitHub

Create a new repository on the command line.

  1. git init
  2. git status
  3. git add .
  4. git status
  5. git commit -m 'Commit message'
  6. git branch -M master
  7. git remote add origin https://github.com/username/repository-name.git
  8. git push -u origin master

Push an existing repository from the command line.

  1. git remote add origin https://github.com/username/repository-name.git
  2. git branch -M main
  3. git push -u origin main

GitLab

  1. git init
  2. git remote add origin https://gitlab.com/username/repository-name.git
  3. git push --set-upstream origin master
  4. git status
  5. git add .
  6. git commit -m 'Commit message'
  7. git push

Clients

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment