Skip to content

Instantly share code, notes, and snippets.

@carmelo0x63
Last active June 9, 2023 08:05
Show Gist options
  • Save carmelo0x63/a8ed2220e84dea6bed3743a58b5b2533 to your computer and use it in GitHub Desktop.
Save carmelo0x63/a8ed2220e84dea6bed3743a58b5b2533 to your computer and use it in GitHub Desktop.
Collection of Git commands and their effects

Git cheatsheet

Git global setup

Initial config

  • git config --global user.name "<Name> <Surname>"
  • git config --global user.email "<userid>@example.com"
  • git config --global credential.helper store
  • git config --global pull.rebase false
  • git config --global color.ui true
  • git config --global push.default simple
  • git config --global core.pager "less -FRX"

Optionally, within a local repository

  • git config [--local] user.name "<Name1> <Surname1>"
  • git config [--local] user.email "<userid>@example.org"

Useful aliases

  • git config --global alias.log1 "log --oneline --graph --decorate"
  • git config --global alias.log1a "log --oneline --graph --decorate --all"

Show configuration

  • git config -l

Create a new repository

git clone git@github.com:<userid>/<repo_name>.git
cd <repo_name>
touch README.md
git add README.md
git commit -m "add README"
git push -u origin main

Existing folder

cd existing_folder
git init
git remote add origin git@github.com:<userid>/<repo_name>.git
git add .
git commit -m "Initial commit"
git push -u origin main

Existing Git repository

cd existing_repo
git remote rename origin old-origin
git remote add origin git@github.com:<userid>/<repo_name>.git
git push -u origin --all
git push -u origin --tags

Create Local Repository

mkdir <repo_name>
cd <repo_name>
git init
git add <file> or git add .
git commit -m '<put message here>'
  SSH: git remote add origin git@github.com:<userid>/<repo_name>.git
  HTTPS: git remote add origin https://github.com/<userid>/<repo_name>.git
git push -u origin <branch_name>

Existing Git Repo

cd <existing_git_repo>
git push [-u origin <branch_name>]

Cloning

git clone git@github.com:<userid>/<repo_name>.git [Local_directory_name]

NOTE: a new subdirectory will be automatically created having the same name of the remote repository; if a different name has to be assigned Local_directory_name shall be specified
NOTE: must add public key to GitHub > Settings > SSH and GPG keys > New SSH key

Update local repository (e.g. after a merge)

git pull [origin <branch_name>]
git fetch --all

Branching

git branch [<new_branch_name>]
git checkout -b <new_branch_name>
git checkout <existing_branch_name>
git checkout --track origin/<new_branch>
git push --set-upstream origin <new_branch_name>

Merging

git checkout -b new_branch
# ... add some code...
git add -A && git commit -m "New code added to new_branch"
git checkout main
git merge new_branch

Delete Branch

git branch -d <local_branch_name>
git push origin --delete <remote_branch_name>

Rename local branch

git branch -m master main
git fetch origin
git branch -u origin/main main
git remote set-head origin -a

Compare

git diff [--stat] origin/main main
git show-branch --all
git log HEAD..origin/main
git log -p HEAD..origin/main
git diff HEAD...origin/main

Working with commits

  • Point the branch to the new commit:
git commit
  • Point the branch to the same commit as the remote branch:
git pull
  • Point the branch at commit identified by <commit_SHA>:
git reset <commit_SHA>
  • Show the diff for the current commit (add "^" for the previous one, e.g.: HEAD^):
git show HEAD
  • Unstage all files you might have staged with git add:
git reset
  • undo/revert all local uncommitted changes (should be executed in repo root):
git checkout .
  • undo/revert uncommitted changes only to particular file or directory:
git checkout [some_dir|file.txt]
  • Reset branch to commits back:
git reset --hard HEAD~<X>
  • Show what's changed since commits before:
git diff HEAD~<X>
  • Squash a bunch of commits together:
git rebase -i HEAD~<X>
  • Change a commit message after pushing
git commit --amend -m "New message"
git push --force repository-name branch-name

Updating upstream repo after .gitignore

git rm -r --cached .
git add .

Merge upstream repo into local fork

git checkout <branch e.g "main">
git pull git@github.com:<userid>/<repo_name>.git <branch e.g "main">

... in case there are conflicts follow https://help.github.com/articles/resolving-a-merge-conflict-from-the-command-line/ ...

git commit -m "<message>"
git push origin main

Sync forked repo

https://docs.github.com/en/github/collaborating-with-issues-and-pull-requests/configuring-a-remote-for-a-fork https://docs.github.com/en/github/collaborating-with-issues-and-pull-requests/syncing-a-fork https://docs.github.com/en/github/using-git/pushing-commits-to-a-remote-repository

$ git remote -v
origin	git@github.com:<user-id>/k3s-ansible.git (fetch)
origin	git@github.com:<user-id>/k3s-ansible.git (push)

$ git remote add upstream https://github.com/k3s-io/k3s-ansible.git

$ git remote -v                                                    
origin	git@github.com:<user-id>/k3s-ansible.git (fetch)
origin	git@github.com:<user-id>/k3s-ansible.git (push)
upstream	https://github.com/k3s-io/k3s-ansible.git (fetch)
upstream	https://github.com/k3s-io/k3s-ansible.git (push)

$ git fetch upstream
remote: Enumerating objects: 79, done.
remote: Counting objects: 100% (79/79), done.
remote: Compressing objects: 100% (35/35), done.
remote: Total 118 (delta 38), reused 69 (delta 32), pack-reused 39
Receiving objects: 100% (118/118), 16.46 KiB | 4.12 MiB/s, done.
Resolving deltas: 100% (42/42), completed with 12 local objects.
From https://github.com/k3s-io/k3s-ansible
 * [new branch]      ansible-galaxy     -> upstream/ansible-galaxy
 * [new branch]      k3s-ha             -> upstream/k3s-ha
 * [new branch]      master             -> upstream/master
 * [new branch]      reset-improvements -> upstream/reset-improvements
 * [new branch]      revert-107-alpine  -> upstream/revert-107-alpine
 * [new branch]      update-ci          -> upstream/update-ci

$ git branch
  ansible-galaxy
* k3s-ha
  master

$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.

$ git merge upstream/master
Updating 999803a..ef8d3a1
Fast-forward
 .github/workflows/lint.yml                 | 2 +-
 roles/k3s/master/tasks/main.yml            | 2 +-
 roles/reset/tasks/main.yml                 | 4 ++--
 roles/reset/tasks/umount_with_children.yml | 3 ++-
 4 files changed, 6 insertions(+), 5 deletions(-)

$ git push origin master
Total 0 (delta 0), reused 0 (delta 0)
To github.com:<user-id>/k3s-ansible.git
   999803a..ef8d3a1  master -> master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment