Git cheat sheet

From Coolscript
Jump to navigation Jump to search

Initialize

  • Create Local Repo
git init
  • Configure Account
git config --global user.name  "Name"
git config --global user.email "mail@server.com"
  • Store passwords
git config credential.helper 'cache --timeout=99999'

Status

git status

Add data

notepad readme.md
git add readme.md
OR
git add .
  • Unstage:
git reset HEAD readme.txt
  • Show Changes
git log
git log --patch
  • Add Empty Folder
mkdir ./temp
touch ./temp/.gitkeep
            ^^^^^^^^
  • Ignore Files and Folder, add
.gitignore
cat .gitignore
*.tmp
/debug


  • Remove (before Commit)
git rm --cached README.md
  • Add remote repo
git remote add origin https://server.com/repo.git
git add .

Commit

  • Commit and write changes to editor
git commit 
  • Be verbose
git commit -v
  • Commit and write changes to oneline
git commit -m "message"
  • Commit (without add) and write changes to oneline
git commit README.md -m "message"
  • Amend to last comit
git commit --amend -m "amend"

Branches

  • Add branch
git branch feature/new-table
  • Add new branch and change into it
git checkout -b feature/new-table
  • Change branch
git checkout feature/new-table
  • Change branch
git switch main
  • Delete Branch
git branch -d feature/new-table 
  • Delete Remote Branch (with column)
git push origin :feature/docs-merge-request
  • List branch
git branch
  • List remote branch
git branch -r
  • List local branch
git branch -v
  • List connected branch
git branch -vv

Diff/Log/Show

  • Diff
git diff
git diff --staged
  • Log
git log
git log --oneline
  • Show
git show <Commit ID>
  • Blame (who)
git blame <file>
  • Remote
git diff main remotes/origin/main

Merge

  • Merge with rebase
git checkout -b feature1/new-table
  • manage changes if conflicts
git rebase master
git checkout master
  • merge
git merge feature1/new-table
  • Merge with confilct
abort merge
git merge --abort
  • Merge to master /fast forward) - no changes
git checkout master
git merge feature/new-table
  • No fast forward
git merge feature/docs-hotfix --no-ff
  • Gitlab - create project
git remote add origin git@gitlab.com:joexx/gittraining.git
git push origin master
  • Get remote repo to local
git pull origin master
  • Reset due to conflict (remote mor up to date than local)
git reset --hard origin/master
or (with a cleaner history)
git pull origin master --rebase
  • Update local
git pull
  • Rebase the last 3 commits (?)
git rebase -i HEAD~3

Practical Sample1

mkdir project
cd project
git clone ....
git checkout -b "New Branch"
#Make changes
git add *
git checkout -m "Your comment here"
git push --set-upstream origin "New Branch"

Tag

  • Add Tag, do this after the commit
git tag v0.1
git tag
git push --tags

Working with remote repos

  • get remote repo to local
git fetch
  • get remote repo to local and commit
git pull
  • Remote diff
git diff origin/main
  • Rebase remote
git rebase origin/main
git rebase --continue
  • Local to remote (default)
git push
  • Push to specific remote branch
git push --set-upstream origin feature/docs-hotfix
  • Pull repo and add a existing remote branch to the local branch
git clone git@ssh.server.com/repo.git
git checkout -b branch-name
git branch --set-upstream-to=origin/branch-name branch-name
git pull

Practical Sample2

mkdir project
cd project
git clone ....
git remote remove origin
git remote add origin git@.../.. .git
#Make changes
git add -A
git commit -m "initial project"
git push --set-upstream origin main

Tools

  • tig