$ git init <directory>
Create empty Git repo in specified directory. Run with no arguments to initialize the current directory as a git repository.
$ git clone <url or path>
Clone repo located at onto local machine. Original repo can be located on the local filesystem or on a remote machine via HTTP or SSH.
# local
$ git config user.name <name>
$ git config user.email "<email>"
or
# global
$ git config --global user.name <name>
$ git config --global user.email "<email>"
Define author name to be used for all commits in current repo. Devs commonly use --global flag to set config options for current user.
# global
$ git config --global alias.<alias-name> <git-command>
Create shortcut for a Git command. E.g. alias.glog “log --graph --oneline” will set ”git glog” equivalent to ”git log --graph --oneline".
# global
$ git config --system core.editor <editor>
Set text editor used by commands for all users on the machine. arg should be the command that launches the desired editor (e.g., vi).
$ git config --global --edit
Open the global configuration file in a text editor for manual editing.
$ git add <directory or file>
Stage all changes in for the next commit. Replace with a to change a specific file.
$ git add -u
Add only modified files
$ git add -p
Add files to stage on interactive and show diff between them
$ git commit -m "<message>"
Commit the staged snapshot, but instead of launching a text editor, use as the commit message.
commit lint message pattern
# new build release
$ git commit -m "build(context): message"
# new deps or lib set
$ git commit -m "chore(context): message"
# new configuration
$ git commit -m "ci(context): message"
# documentation update or create
$ git commit -m "docs(context): message"
# new feature
$ git commit -m "feat(context): message"
# some bug fix
$ git commit -m "fix(context): message"
# some code changed no new implementation
$ git commit -m "refactor(context): message"
# some commit was reverted
$ git commit -m "revert(context): message"
# some css style added or updated
$ git commit -m "style(context): message"
# some test added or changed
$ git commit -m "test(context): message"
$ git status
List which files are staged, unstaged, and untracked.
$ git log
Display the entire commit history using the default format. For customization see additional options.
$ git log -<limit>
Limit number of commits by . E.g. ”git log -5” will limit to 5 commits.
$ git log --oneline
Condense each commit to a single line.
$ git log -p
Display the full diff of each commit.
$ git log --stat
Include which files were altered and the relative number of lines that were added or deleted from each of them.
$ git log --author=”<pattern>”
Search for commits by a particular author
$ git log --grep=”<pattern>”
Search for commits with a commit message that matches .
$ git log <since>..<until>
Show commits that occur between and . Args can be a commit ID, branch name, HEAD, or any other kind of revision reference.
$ git log -- <file>
Only display commits that have the specified file
$ git log --graph --decorate
--graph flag draws a text based graph of commits on left side of commit
msgs. --decorate
adds names of branches or tags of commits shown
# Open config file
$ git config --global --edit
# Create an alias on file
[alias]
s = !git status
c = !git add --all && git commit -m
l = !git log --pretty=format:'%C(yellow)%h%C(red)%d %C(white)%s - %C(cyan)%cn, %C(green)%cr'
# Then save the file
$ git diff
Show unstaged changes between your index and working directory.
$ git diff HEAD
Show difference between working directory and last commit.
$ git diff --cached
Show difference between staged changes and last commit
$ git commit --amend -m "<message>"
Replace the last commit with the staged changes and last commit combined. Use with nothing staged to edit the last commit’s message
$ git rebase <base>
Rebase the current branch onto . can be a commit ID, branch name, a tag, or a relative reference to HEAD
git rebase -i <base>
Interactively rebase current branch onto . Launches editor to enter commands for how each commit will be transferred to the new base.
# 'Add an alias to original url repo'
$ git remote add upstream https://github.com/code.......
# 'Get updates'
$ git fetch upstream
# 'Update local repo (main branch) from upstream'
$ git rebase upstream/main
$ git reflog
Show a log of changes to the local repository’s HEAD.
Add --relative-date
flag to show date info or --all to show all refs.
$ git branch
List all of the branches in your repo. Add a argument to create a new branch with the name .
$ git checkout -b <branch>
Create and check out a new branch named .
Drop the -b
flag to checkout an existing branch.
$ git switch <branch>
Naegate to an existing branch
$ git merge <branch>
Merge into the current branch.
$ git branch -d <branch>
Delete a branch if it exists
$ git branch -D <branch>
Forced Delete a branch
$ git checkout <branch> <file>
Update a specific file from another branch to current branch
$ git remote add <name> <url>
Create a new connection to a remote repo. After adding a remote, you can use as a shortcut for in other commands.
$ git remote -v
Show information about remotes
$ git fetch <remote> <branch>
Fetches a specific , from the repo. Leave off to fetch all remote refs.
$ git pull <remote>
Fetch the specified remote’s copy of current branch and immediately merge it into the local copy.
$ git pull --rebase <remote>
Fetch the remote’s copy of current branch and rebases it into the local copy. Uses git rebase instead of merge to integrate the branches.
$ git push <remote> <branch>
Push the branch to , along with necessary commits and objects. Creates named branch in the remote repo if it doesn’t exist.
$ git push <remote> --all
Push all of your local branches to the specified remote.
$ git push <remote> --tags
Tags aren’t automatically pushed when you push a branch or use the
--all
flag. The --tags
flag sends all of your local tags to the remote repo.
$ git revert <commit>
Create new commit that undoes all of the changes made in , then apply it to the current branch.
$ git reset <file>
Remove from the staging area, but leave the working directory unchanged. This unstages a file without overwriting any changes.
$ git clean -n
Shows which files would be removed from working directory.
Use the -f
flag in place of the -n
flag to execute the clean.
$ git tag -a "version" -m "<message>"
Create a new tag example git tag -a "1.0.0" "v1.0.0"
$ git checkout "1.0.0"
Navegate on code to version "1.0.0" identified by tag
$ git tag -d "1.0.0"
Delete tag "1.0.0"
$ git stash
Add all changed files to stash
$ git stash list
Show stash index and files
$ git stash pop
Get all files from stash to HEAD and remove it from stash.
If you don't want remove it use git stash apply
git log --stat --no-merges --format="%Cred%h : %Cgreen%an - %Cblue%s %C(yellow)%cr"
Changed Files.
Hash : UserName - Message Period
git log --pretty=format:'%C(yellow)%h%C(red)%d %C(white)%s - %C(cyan)%cn, %C(green)%cr'
One line commits info.
Hash Message UserName Period
git diff --stat -c <branch>
List changes from current branch and other branch
git branch -r | grep -v HEAD | while read b; do git log --color --format="%ci _%C(bold cyan)$b%Creset^ %s^ %C(bold blue)%an%Creset^ %C(magenta) %cr^ " $b | head -n 1; done | sort -r | cut -d_ -f2- | sed 's;origin/;;g' | awk -F^ -vOFS=^ 'NR{$3=substr($3,1,60)}1' | head -10 | column -t -s '^'
List all branchs and last commit for each one.
Branch CommitMessage UserName Period
git shortlog -sn
View how many commits each user made
git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset'
Generate a graph showing that branch commits have been merged
git checkout HEAD~1
Go back a commit from the current commit
git checkout <HASH>
Go to a specific commit. Use git log --oneline
to identify the hash
git checkout <current_branch>
# or
git switch <current_branch>
Go to last commit on current branch.