mirror of
https://github.com/namibia/awesome-cheatsheets.git
synced 2024-12-04 10:58:27 +00:00
commit
e668f3c19c
83
tools/git.sh
83
tools/git.sh
@ -4,15 +4,32 @@ git clone <address> # creates a git repo from given address (get the address fro
|
|||||||
git clone <address> -b <branch_name> <path/to/directory> # clones a git repo from the address into the given directory and checkout's the given branch
|
git clone <address> -b <branch_name> <path/to/directory> # clones a git repo from the address into the given directory and checkout's the given branch
|
||||||
git clone <address> -b <branch_name> --single-branch # Clones a single branch
|
git clone <address> -b <branch_name> --single-branch # Clones a single branch
|
||||||
|
|
||||||
git add file.txt # adds(stages) file.txt to the git
|
git add <file_name> # adds(stages) file.txt to the git
|
||||||
git add * # adds(stages) all new modifications, deletions, creations to the git
|
git add * # adds(stages) all new modifications, deletions, creations to the git
|
||||||
git reset file.txt # Removes file.txt from the stage
|
git reset file.txt # Removes file.txt from the stage
|
||||||
git reset --hard # Throws away all your uncommitted changes, hard reset files to HEAD
|
git reset --hard # Throws away all your uncommitted changes, hard reset files to HEAD
|
||||||
|
git reset --soft <commit_id> # moves the head pointer
|
||||||
|
git reset --mixed <commit_id> # moves the head pointer and then copies the files from the commit it is now pointing to the staging area,
|
||||||
|
# the default when no argument is provided
|
||||||
|
git reset -hard <commit_id> # moves the head pointer and then copies the files from the commit it is now pointing to the staging area
|
||||||
|
# and working directory thus, throw away all uncommitted changes
|
||||||
|
|
||||||
|
# git reset
|
||||||
|
# 1. Move HEAD and current branch
|
||||||
|
# 2. Reset the staging area
|
||||||
|
# 3. Reset the working area
|
||||||
|
|
||||||
|
# --soft = (1)
|
||||||
|
# --mixed = (1) & (2) (default)
|
||||||
|
# --hard = (1) & (2) & (3)
|
||||||
|
|
||||||
git rm file.txt # removes file.txt both from git and file system
|
git rm file.txt # removes file.txt both from git and file system
|
||||||
git rm --cached file.txt # only removes file.txt both from git index
|
git rm --cached file.txt # only removes file.txt both from git index
|
||||||
git status # shows the modifications and stuff that are not staged yet
|
git status # shows the modifications and stuff that are not staged yet
|
||||||
|
|
||||||
git branch # shows all the branches (current branch is shown with a star)
|
git branch # shows all the branches (current branch is shown with a star)
|
||||||
|
git branch -a # shows all the branches local and remote
|
||||||
|
|
||||||
git branch my-branch # creates my-branch
|
git branch my-branch # creates my-branch
|
||||||
git branch -d my-branch # deletes my-branch
|
git branch -d my-branch # deletes my-branch
|
||||||
git checkout my-branch # switches to my-branch
|
git checkout my-branch # switches to my-branch
|
||||||
@ -32,7 +49,13 @@ git remote add my-remote <address> # creates a remote (get the address from your
|
|||||||
git remote rm my-remote # Remove a remote
|
git remote rm my-remote # Remove a remote
|
||||||
|
|
||||||
git log # shows the log of commits
|
git log # shows the log of commits
|
||||||
|
# git log by default uses less command so you can use these: f=next page, b=prev page, search=/<query>, n=next match, p=prev match, q=quit
|
||||||
|
git log --no-pager # shows the log of commits without less command
|
||||||
git log --oneline # shows the log of commits, each commit in a single line
|
git log --oneline # shows the log of commits, each commit in a single line
|
||||||
|
|
||||||
|
git log --oneline --graph --decorate # shows the log of commits, each commit in a single line with graph
|
||||||
|
git log --since=<time> # shows the log of commits since given time
|
||||||
|
git log -- <file_name>
|
||||||
git log -p <file_name> # change over time for a specific file
|
git log -p <file_name> # change over time for a specific file
|
||||||
git log <Branch1> ^<Branch2> # lists commit(s) in branch1 that are not in branch2
|
git log <Branch1> ^<Branch2> # lists commit(s) in branch1 that are not in branch2
|
||||||
git log -n <x> # lists the last x commits
|
git log -n <x> # lists the last x commits
|
||||||
@ -40,6 +63,9 @@ git log -n <x> --oneline # lists the last x commits, each commit in single l
|
|||||||
git grep --heading --line-number '<string/regex>' # Find lines matching the pattern in tracked files
|
git grep --heading --line-number '<string/regex>' # Find lines matching the pattern in tracked files
|
||||||
git log --grep='<string/regex>' # Search Commit log
|
git log --grep='<string/regex>' # Search Commit log
|
||||||
|
|
||||||
|
git reflog # record when the tips of branches and other references were updated in the local repository.
|
||||||
|
git ls-files # show information about files in the index and the working tree
|
||||||
|
|
||||||
git commit -m "msg" # commit changes with a msg
|
git commit -m "msg" # commit changes with a msg
|
||||||
git commit -m "title" -m "description" # commit changes with a title and description
|
git commit -m "title" -m "description" # commit changes with a title and description
|
||||||
git commit --amend # combine staged changes with the previous commit, or edit the previous commit message without changing its snapshot
|
git commit --amend # combine staged changes with the previous commit, or edit the previous commit message without changing its snapshot
|
||||||
@ -50,6 +76,9 @@ git revert <commit-id> # Undo a commit by creating a new commit
|
|||||||
|
|
||||||
git show # shows one or more objects (blobs, trees, tags and commits).
|
git show # shows one or more objects (blobs, trees, tags and commits).
|
||||||
git diff # show changes between commits, commit and working tree
|
git diff # show changes between commits, commit and working tree
|
||||||
|
git diff HEAD #show changes between working directory vs last commit
|
||||||
|
git diff --staged HEAD #show changes between stage area vs last commit
|
||||||
|
|
||||||
git diff --color # show colored diff
|
git diff --color # show colored diff
|
||||||
git diff --staged # Shows changes staged for commit
|
git diff --staged # Shows changes staged for commit
|
||||||
|
|
||||||
@ -61,7 +90,8 @@ git push --delete my-remote v1.0 # deletes the tag in my-remote (be carefore to
|
|||||||
git push my-remote my-branch v1.0 # push v1.0 tag to my-remote in my-branch
|
git push my-remote my-branch v1.0 # push v1.0 tag to my-remote in my-branch
|
||||||
git fetch --tags # pulls the tags from remote
|
git fetch --tags # pulls the tags from remote
|
||||||
|
|
||||||
git pull my-remote my-branch # pulls and tries to merge my-branch from my-remote to the current branch
|
git pull my-remote my-branch # pulls and tries to merge my-branch from my-remote to the current branch git pull = git fetch && get merge
|
||||||
|
|
||||||
|
|
||||||
git stash # stashes the staged and unstaged changes (git status will be clean after it)
|
git stash # stashes the staged and unstaged changes (git status will be clean after it)
|
||||||
git stash -u # stash everything including new untracked files (but not .gitignore)
|
git stash -u # stash everything including new untracked files (but not .gitignore)
|
||||||
@ -83,18 +113,63 @@ git clean -f # clean untracked files permanently
|
|||||||
git clean -f -d/git clean -fd # To remove directories permanently
|
git clean -f -d/git clean -fd # To remove directories permanently
|
||||||
git clean -f -X/git clean -fX # To remove ignored files permanently
|
git clean -f -X/git clean -fX # To remove ignored files permanently
|
||||||
git clean -f -x/git clean -fx # To remove ignored and non-ignored files permanently
|
git clean -f -x/git clean -fx # To remove ignored and non-ignored files permanently
|
||||||
|
git clean -d --dry-run # shows what would be deleted
|
||||||
|
|
||||||
|
|
||||||
git config --global --list # lists the git configuration for all repos
|
git config --global --list # lists the git configuration for all repos
|
||||||
git config --global --edit # opens an editor to edit the git config file
|
git config --global --edit # opens an editor to edit the git config file
|
||||||
git config --global alias.<handle> <command> # add git aliases to speed up workflow , eg. if handle is st and command is status then running git st would execute git status
|
git config --global alias.<handle> <command> # add git aliases to speed up workflow , eg.
|
||||||
|
# if handle is st and command is status then running git st would execute git status
|
||||||
|
git config --global core.editor <editor_name> # config default editor
|
||||||
|
|
||||||
|
|
||||||
|
git archive <branch_name> --format=zip --outpute=./<archive_name>.zip # create an archive of files from a named tree
|
||||||
|
|
||||||
|
|
||||||
.gitignore
|
.gitignore
|
||||||
# is a file including names of stuff that you don"t want to be staged or tracked.
|
# is a file including names of stuff that you don"t want to be staged or tracked.
|
||||||
# You usually keep your local files like database, media, and etc here.
|
# You usually keep your local files like database, media, etc here.
|
||||||
# You can find good resources online about ignoring specific files in your project files.
|
# You can find good resources online about ignoring specific files in your project files.
|
||||||
# .gitignore is also get ignored
|
# .gitignore is also get ignored
|
||||||
.git
|
.git
|
||||||
# is a hidden directory in repo directory including git files. It is created after "git init".
|
# is a hidden directory in repo directory including git files. It is created after "git init".
|
||||||
|
|
||||||
|
|
||||||
|
# Some useful notes:
|
||||||
|
|
||||||
|
# Better Commit messages:
|
||||||
|
# Key to Effective Debugging
|
||||||
|
# For the commit message to help in debugging effectively, ensure that it is short and use an imperative
|
||||||
|
# mood (spoken or written as if giving a command or instruction) when constructing them.
|
||||||
|
# Also use feature tense for commit messages.
|
||||||
|
# The first word in your commit message should be one of these:
|
||||||
|
# Add
|
||||||
|
# Create
|
||||||
|
# Refactor
|
||||||
|
# Fix
|
||||||
|
# Release
|
||||||
|
# Document
|
||||||
|
# Modify
|
||||||
|
# Update
|
||||||
|
# Remove
|
||||||
|
# Delete etc...
|
||||||
|
|
||||||
|
# About resetting:
|
||||||
|
# Use git revert instead of git reset in shared repositories
|
||||||
|
# git revert creates a new commit that introduces the opposite changes from the specified commit.
|
||||||
|
# Revert does not change history the original commit stays in the repository
|
||||||
|
|
||||||
|
|
||||||
|
# Difference between ~ and ^ in git:
|
||||||
|
# > ^ or ^n
|
||||||
|
# >no args: == ^1: the first parent commit
|
||||||
|
# >n: the nth parent commit
|
||||||
|
|
||||||
|
# > ~ or ~n
|
||||||
|
# >no args: == ~1: the first commit back, following 1st parent
|
||||||
|
# >n: number of commits back, following only 1st parent
|
||||||
|
# note: ^ and ~ can be combined
|
||||||
|
|
||||||
|
# Some tools to improve git skill by visualizing it:
|
||||||
|
# https://git-school.github.io/visualizing-git/
|
||||||
|
# https://learngitbranching.js.org/
|
||||||
|
Loading…
Reference in New Issue
Block a user