Basics | Git

Git States#

Working Directory => Staging Area => Repository (.git folder) => Remote Repositor

Commands#

help#

git help command

init#

git init project-name

initialize an empty git repo in current folder

git init .

status#

git status

add#

move file from working directory to staging area

git add <file>

add all files in working directory to staging

git add .

all tracked files in the entire working tree are updated no file will be added

git add -U
git add -update

This adds, modifies, and removes index entries to match the working tree

git add -A
git add -all

commit#

use core editor which has been configured as git editor - multiline

git commit

inline

git commit -m "message"

add all modified (tracked) files before commit

git commit -a
git commit -am "message"

if you add close #issueNumber to end of message it will close the issue and associate the commit to that issue

log#

all commits

git log

single line

git log --oneline

asterisk based graph denoting our branching hierarchy

git log --graph

which commits are a part of which branches

git log --decorate

history for all the branches

git log --all

history of one file

git log -- <file>

show#

shows the last commit and a diff containing all changes

git show

shows the details of annotated tag

git show <tagname>

ls-files#

show tracked files

git ls-files

reset#

unstage changes that have staged

git reset HEAD <file>

reset to a commit id
basically, change where HEAD is pointing. it preserves staging area.

git reset <commit> --soft

change HEAD pointing position and ustage commited changes

git reset <commit>
git reset <commit> --mixed

change HEAD position and wipe out pending changes

git reset <commit> --hard

reflog#

shows all actions we've taken while in the repository

git reflog

checkout#

revert unstaged changes

git checkout -- <file>

config#

at the user level not repo level

git config --global

list of configs

git conifg --list

add hist alias for showing history

git config --global alias.hist "log --oneline --graph --decorate --all"

diff#

difference between current working directory and HEAD

git diff
git difftool

difference between a specific commit and HEAD

git diff <id> HEAD

open configured diff tool

git difftool <id> HEAD

difference of two branches

git diff <branch> <branch>

branch#

show current branch

git branch

show all branches

git branch -a

delete branch

git branch -d <branch>

checkout#

switch to branch

git checkout <branch>

create and switch to new branch
carry modifications into new branch

git checkout -b <branchname>

merge#

merge branch into current branch

git merge <branch>

resolve conflicts using mergetool

git mergetool

tag#

label some commit points

two types:

  • lightweight: just label
  • annotated: details

Tag the HEAD

git tag <name>

Tag a commit

git tag <name> <commit>

Tag the last commit of a branch

git tag <name> <branch>

annotated

git tag -a <name> -m "commit message"

show annotated tag details

list tags

git tag
git tag --list

delete a tag

git tag -d <name>

push deleted tag

update a tag

git tag -f <name> <commit>

push updated tag

stash#

stash working directory

git stash

list stashed files

git stash list

apply and drop stash

git stash pop

remote#

by convention, the first and primary repo named origin

git remote add <refname> <url>

the list of remotes

git remote -v

update url

git remote set-url <refname> <url>

show remote details

git remote show <refname>

push#

push current branch in Git v2.0#

git push

push and set up a tracking branch relationship between the <branch> branch on the local repo and the <branch> on the remote repo#

git push -u <remote> <branch>

push all branches#

git push --all

push a tag#

git push <remote> <tagname>

push all tags#

git push --tags

push deleted tag#

git push <remote> :<name>

push updated tag#

git push --force <remote> <name>

clone#

git clone <url> <name>

fetch / pull#

git fetch

fetch and merge

git pull

rename and removing file#

edit#

rename will be staged automatically and git will know it's the same file

git mv <file> <newfilename>

delete <file> and create an untracked <newfilename>

mv <file> <newfilename>

add all deleted and added files. So it detects the file has been renamed.

git add -A

remove#

delete <file> and stage changes

git rm <file>
rm <file>
git add -u

.gitignore#

one pattern or expression per line

Branches#

name for timeline

Merge#

Fast-Forward#

  • when no additional work has been detected on the parent branch
  • git will simply apply all commits from the other branch to parent branch, as if we never branched off to begin with
  • we can disable it

Automatic#

  • when non-conflict merge detected
  • preserve both timelines
  • will have a merge commit

Manual#

  • conflict
  • resolve and commit a merged commit

Rebase#

rewinds the current commits that are on your branch to a point to where the branch you're merging in originally diverged then playing back the commits that happend on the branch that you're wanting to bring in then playing on top of that, any commits that have happened on the branch that you're currently on

git pull --rebase

it will rebase instead of merge

Markers (Pointers)#

Head#

  • Point to last commit of current branch
  • Can be moved manually or by switching branch

issue and PR#

for associating a commit to issue we can add a comment and in comment we can use # to find the issue/PR