Skip to content

Instantly share code, notes, and snippets.

@MukhtaarAziz
Last active September 3, 2022 06:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MukhtaarAziz/dc443d55c10d95915a69310f111383fa to your computer and use it in GitHub Desktop.
Save MukhtaarAziz/dc443d55c10d95915a69310f111383fa to your computer and use it in GitHub Desktop.
Git Commands Cheatsheat

Git Cheatsheet

Table Of Contents (TOC)


Clone only one branch from repo

git clone -b <mybranch> --single-branch <git://sub.domain.com/repo.git>

Does "git push" push all commits from other branches?

No, git push only pushes commits from current local branch to remote branch that you specified in command.

You can tell git to push all branches by setting the --all argument

See the command description


Git Branching

Git Branch: is a developement workflow in the repository.

Every Git repo has minimum one branch called Main ( Previously called master).

New Branch

  • create new branch. (the starting point of branch is HEAD)
git branch <branch-name>
  • Create a new branch from history or old commit
git branch <branch-name> <commit-hash>
  • create new branch and switch/checkout to it
git checkout -b <branch-name>

Delete branches

Note: Cann't delete current active branch.

  • delete single branch (merged or empty)
git branch -d <branch-name>
  • force deleting branch (not empty)
git branch -D <branch-name>
  • delete locally and remotely branch
git push -d <remote-name> <branch-name>
git branch -d <branch-name>

Renaming Branches

  • rename single branch
git branch -m <old-name> <new-name>



Git Remote



In this section we will list all command related to working with git remotes.

Git Fetch Command

Fetching all branches from all Remotes

git fetch --all

This command will fetch all remote origins.


Git Push Command

General Syntax

git push <remote-name> <branch-name>

Other Usages:

  • pushing only current active branch changes
git push
  • Pushing all Branches of single remote, We can use This Command.
git push <remote-name> *

The * is Glob Operator

pushing main branch of remote origin

git push origin main

Git Pull Command

General Syntax

git pull <remote-name> <branch-name>

The git only Pull Current Active Branch, So switch/checkout to desire branch and pull it.

These Examples Don't Works:

  • Pull all Branches of Single Remote using asterisk (*)
git pull <remote-name> *
  • Pull all Branches of All Remotes
git pull --all
  • Pulling another branch that does not current Active branch. Like that current active branch is main and you try to pull dev branch.
git pull origin dev



Git Resetting Branch

reset current branch with specific commit

git reset [--hard | --mixed] {HEAD | HASH }

reset branch with its remote

git reset [--hard] <remote-name>/<brnach-name>



Git Branch Resetting.

Reset: changes the files in the staging index OR working directroy to the state they had when a specified commit was made.

  • Make my project look like it did back then.

  • Move HEAD Pointer to a specific commit

Types:

  • Soft (only back HEAD Pointer to specific commit)
  • Mixed (Changes the Staging Index Only)
  • Hard (Change the Staging Index and Working Directroy)

Soft Reset

  • Move HEAD Pointer.
  • Does not change staging index.
  • Does not change Working Directroy.
git reset --soft <tree-ish>

عند الانتقال الى commit سابقة، يجعل التغيرات التي حصلت من قبل الcommits الاحقة في الـStaging Index (staging area)

الفائدة منه هو تجميع الـ Commits اللاحقة في Commit واحدة.

Mixed Reset

  • Move HEAD Pointer.
  • Changes Staging Index to match repository.
git reset <tree-ish>
git reset --mixed <tree-ish>

عند الانتقال الى commit سابقة، يجعل التغيرات التي حصلت من قبل الcommits الاحقة في الـ Working Directroy.

الفائدة منه هو اعادة تنظم الـ Commits

Hard Reset

  • Move HEAD Pointer.
  • Changes Staging index to match repository.
  • Changes Wroking directroy to match repository.
git reset --hard <tree-ish>

العودة الى Commit سابقة وجعل كل شيئ مطابق لها. التغيرات اللاحقة لها سوف تهمل بالكامل.

الفائدة منه هو عمل تراجع Undo للـ Commits.


Git Tagging

  • Tags allow making points in history as Important.

  • named reference to a commit.

  • most often used to mark releases (v1.0, v2.0, ...)

  • Can mark key feature or changes (eccommerce, design, ...)

  • can mark points for team discussion (bug, Issue123,...)

Tags Types


  1. Lightweight Tags
  2. Annotated Tags (lightweight with a message)

Lightweight Tag

git tag <tag-name> <commit-hash>

Annotated Tags

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

اذا كنت غير متاكد ماذا تستخدم، فاستخدم الـ Annotated. يسمح لك باضافة رسالة وصف.

Listing Tags


list all tags

git tag { --list | -ls }

list annotation message

git list -l -n

Filtering tags use this command

git tag -l "<filter>"

Show the tags content OR show the differents

git show <tag-name>
git diff <tag-name1>..<tag-name2>

Delete Tags


git tag { --delete | -d } <tag-name>

Can not reterive deleted tags.

Tags and Remotes


افتراضيا الـ Tags محلية ولكن يمكن مشاركتها

  • Like Branches, Tags are local unless shared to a remote.
  • git push does not transfer tags.
  • Tags Must be explicitly transferred.
  • git fetch automatically retrieves shard tags.

Share Tags with a remote

share single tag

git push <origin-name> <tag-name>

share all tags

git push <remote-name> --tags

الامر git fetch سوف يعمل fetch لجميع الـ commits و الـ shared tags. لكن اذا كان الغرض من الامر هو فقط tags استخدم الامر git fetch --tags الذي بدوره يعمل fetch لل tags مع الـcommits الضرورية لعملهم. (وهذه الحالة نادرة الاستخدام)

Delete Tags from a remote

طريقة حذف الـTags مشابهة لطريقة حذف الـBranches

git push <origin-name> :<tag-name>
git push { --delete | -d } <remote-name> <tag-name>

examples:

git push origin :V1.0
git push --delete origin V1.1

Checking Out Tags

الـ Tags هو معرف الى commit معينة في تاريخ الـ Commits.

  • Tags are not branches.
  • Tags Can be checked out just like any commit.

افضل طريقة لعمل checkout للـTags هي: انشاء فرع جديد خاص به من خلال الامر

git checkout -b <branch-name>  <tag-name>

الطريقة الاخرى:

git checkout <tag-name>

التي سوف تضع المستودع بحالة Detached HEAD

وهي حالة تحصل عند عمل checkout الى commit في الـ Local repository فتشبه عمل فرع مؤقت unnamed branch فعند اضافة commits جديدة لا تربط او تعود الى اي فرع. وغالبا ما تحذف بعدة اسبوعان تقريبا.

عند حصول هذه الحالة يمكن الحل بعدة طرق :

  • عمل Tags الـcommits في الـ Detached HEAD حتى نستطيع العودة لها.
git tag temp
  • انشاء فرع خاص لها
git branch temp_branch
  • الطريقة الافضل هي:
git checkout -b temp_branch

Detached HEAD State


  • checking out a commit puts the local respository in "detached HEAD" state
  • Like being on an unnamed branch.
  • New Commits will not belong to any branch.
  • detached commits will be garbage collected (~ 2 weeks)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment