Skip to content

Instantly share code, notes, and snippets.

@maheshj01
Last active March 17, 2025 04:00
Show Gist options
  • Save maheshj01/cb81e9f6245b6c97b5a5e65b9419d261 to your computer and use it in GitHub Desktop.
Save maheshj01/cb81e9f6245b6c97b5a5e65b9419d261 to your computer and use it in GitHub Desktop.
git cheat Sheet
  1. Clone a project from remote to your system git clone https://github.example.com/project/example.git

  2. To know the status of your project

    • to know the uncommited changes
    • your current branch name etc run git status
  3. create a branch git branch <newBranchName>

  4. Change a branch git checkout <branchname> switch to previous branch git checkout -

  5. To create and switch to the new branch (one liner for point 3 & 4) git checkout -b <newBranchName>

  6. change existing git remote url git remote set-url origin [email protected]:user/repository2.git

  7. see available remotes git remote -v

  8. Abort git merge git merge --abort Use Case: suppose you are on branch B and ran the command git merge A after getting changes of A, if you want to undo the changes(Assuming you have not commited after merge) run this command git merge --abort

  9. rename a branch

    git branch -m <new-name>: if you are on the same branch that you want to rename

    git branch -m <old-name> <new-name>: if you are on some other branch

Stash (Save changes locally without commiting)

  1. Stash all the changes git stash

  2. Stash a particular file git stash push -m "Your stash message here" lib/../filepath.dart source: https://stackoverflow.com/a/5506483/8253662

  3. to see the list of stash git stash list

  4. I popped a stash and now it shows conflicts so you could go to previous state by running git reset --merge, stash will not be popped unless it is popped successfully. see: https://stackoverflow.com/a/60444590/8253662

Fire in the hole

  1. Accidentally made a commit but not pushed Undo a local git commit(Which is not Pushed) and get back the changes git reset --soft HEAD~1

To Undo a pushed commit use git revert (see point 14)

  1. reset your branch to an old commit git reset --hard <hashId> e.g git reset --hard 6e559cb
    Warning: running this command your branch will ignore all the recent commits made and move your branch to the older commit specified(6e559cb)

  2. undo a Pushed commit with a revert commit using git revert <commitId to revert> git revert 53d46518 Note that revert command makes another commit on top of the existing commit to bring the files to the previous state

  3. pull a particular commit from any branch git cherry-pick <commit hash>

  4. add a pushed file to gitignore git rm -r --cached <file or directory name>

  5. Pushing an empty commit example git commit --allow-empty -m "empty commit example2

  6. Rebase the branch to master (flutter/flutter#97046 (comment))

       git fetch remote
       git rebase remote/master
       # resolve conflicts
       git push -f
    
  7. To find a bad commit in code which introduced regression use git bisect

    • firstly you need to run git bisect start to tell git to checkout to current commit, this will also clear any past bisect history
    • specify whether the current commit is good git bisect good
    • Then specify the other commit which is bad git bisect bad you can also do vice versa after running the second command git will give you a commit to verify e.g faaca13f22647c647e5c7d20d55c0fed9e06ca19
    • then verify if the commit is good/bad by checking out git chekcout faaca13f22647c647e5c7d20d55c0fed9e06ca19
    • after verifying run git commit good if its good and git commit bad if its bad.
    • repeat this until you get a final commit which introduced the bug

Here is a example bisection

source

Note: Bisection was done on master channel

mahesh@Maheshs-Air-M1 flutter_beta % git bisect bad
mahesh@Maheshs-Air-M1 flutter_beta % git bisect good 2.5.3
Bisecting: a merge base must be tested
[0f465e5b2a3ed2431321b490a614c3d15089854c] Revert "update ScrollMetricsNotification (#87421)" (#87693)
mahesh@Maheshs-Air-M1 flutter_beta % pwd                                    
/Users/mahesh/Documents/flutter_beta
mahesh@Maheshs-Air-M1 flutter_beta % git bisect good
Bisecting: 397 revisions left to test after this (roughly 9 steps)
[33755f203dfeae491f586a73abdf4e400749f32a] [autofill] opt-out instead of opt-in (#86312)
mahesh@Maheshs-Air-M1 flutter_beta % git bisect good
Bisecting: 198 revisions left to test after this (roughly 8 steps)
[169020719bc5882e746b836629721644633b6c8a] cc3c53cde [webview_flutter] Update version number app_facing package (flutter/plugins#4375) (#90690)
mahesh@Maheshs-Air-M1 flutter_beta % git bisect good
Bisecting: 99 revisions left to test after this (roughly 7 steps)
[0e72f992371765d41147bae5a21095b2ae817e7a] Restart input connection after `EditableText.onSubmitted` (#84307)
mahesh@Maheshs-Air-M1 flutter_beta % git bisect bad
Bisecting: 49 revisions left to test after this (roughly 6 steps)
[2e663b2472a549ffbae197a14aa1251715c7cfe9] [bots] Print more on --verbose analyze_sample_code (#90880)
mahesh@Maheshs-Air-M1 flutter_beta % git bisect good
Bisecting: 24 revisions left to test after this (roughly 5 steps)
[131b6b18916995916d5387df3130be50bf0e19fd] 90b2844ce [google_maps_flutter_web] Add Marker drag events (flutter/plugins#4385) (#91002)
mahesh@Maheshs-Air-M1 flutter_beta % git bisect good
Bisecting: 12 revisions left to test after this (roughly 4 steps)
[a20743ddfe61208eb1c778c105d96b316b6005d1] Roll Engine from 1f8fa8041d49 to 8ec71b64f6ca (12 revisions) (#91071)
mahesh@Maheshs-Air-M1 flutter_beta % git bisect bad 
Bisecting: 5 revisions left to test after this (roughly 3 steps)
[c48c428e46d57b289f7becce0cec4e75c80c5489] Xcode 13 as minimum recommended version (#90906)
mahesh@Maheshs-Air-M1 flutter_beta % git bisect bad
Bisecting: 2 revisions left to test after this (roughly 2 steps)
[fc02dcbb4ca632fcb08c31c36b398b69d8c56683] Roll Plugins from 90b2844ce7e5 to 63eb67532a7a (2 revisions) (#91039)
mahesh@Maheshs-Air-M1 flutter_beta % 
mahesh@Maheshs-Air-M1 flutter_beta % git bisect bad
Bisecting: 0 revisions left to test after this (roughly 1 step)
[c54e9d2fda3045bff8e53bcad6ad27bcef2185e4] Roll Engine from d0d8e348b6b4 to e83795a0a7c7 (11 revisions) (#91036)
mahesh@Maheshs-Air-M1 flutter_beta % git bisect bad
Bisecting: 0 revisions left to test after this (roughly 0 steps)
[faaca13f22647c647e5c7d20d55c0fed9e06ca19] Catch FormatException from bad simulator log output (#90966)
mahesh@Maheshs-Air-M1 flutter_beta % git bisect good
c54e9d2fda3045bff8e53bcad6ad27bcef2185e4 is the first bad commit
commit c54e9d2fda3045bff8e53bcad6ad27bcef2185e4
Author: engine-flutter-autoroll <[email protected]>
Date:   Thu Sep 30 13:18:05 2021 -0400

    Roll Engine from d0d8e348b6b4 to e83795a0a7c7 (11 revisions) (#91036)

 bin/internal/engine.version        | 2 +-
 bin/internal/fuchsia-linux.version | 2 +-
 bin/internal/fuchsia-mac.version   | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)
mahesh@Maheshs-Air-M1 flutter_beta % 

  1. Cherry pick PR changes for review
git fetch origin pull/<PR-id>/head:<BRANCH_NAME> // pulls and creates a new branch

git checkout BRANCH_NAME

Running above commands would checkout and pull the PR changes in a new branch (optionally) if you want to see the changes of the PR in staging area you can do a soft reset until a commit in the past. This will stage the changes past the commit for you to compare

git reset --soft <past commit id> 

see: https://stackoverflow.com/a/72465195/8253662 Also, See: https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/reviewing-changes-in-pull-requests/checking-out-pull-requests-locally

To co-author a PR you can commit to author's repo

git push origin error-ui
git remote add author https://github.com/author/github-freak.git
git push author error-ui
  1. Delete a branch

Remote branch

git push origin --delete <branch name>

Local branch

git branch -D <branch name>
  1. Get a copy of a remote branch into a new branch(locally)
git fetch remote <remote branch>:<local branch>
git checkout <local branch>
  1. Foce pull changes from remote (overwrite local)

Note don't forget to take a back up of current branch using git branch backup

git reset --hard remote/main
  1. git tag <tagname> // e.g git tag v1.0.0 push tags to github using git push --tags

  2. Delete a pushed commit from remote repository without losing your work

Here's a clean way of removing your commits from the remote repository without losing your work.

Quick Answer

git reset --soft HEAD~1 # 1 represents only last 1 commit 
git stash # hold your work temporary storage temporarily.
git pull # bring your local in sync with remote
git reset --hard HEAD~1 # hard reset 1 commit behind (deletes local commit)
git push -f # force push to sync local with remote
git stash pop # get back your unpushed work from stash

A detailed explanation of why this works.

Heres is what happened in my case

  1. I was working on a branch redesign

  2. I accidentally committed and pushed a secrets file along with my other work. So my secrets are now exposed (Gitguardian's yelling about this in my email xD) Now I want to remove my secrets file from the repository but want to keep my work too.

  3. So run git reset --soft HEAD~1 this will move your local repository on your machine 1 commit behind (modify the number to move n commits behind)

  4. Now you will see your committed files as unstaged

  5. Save this work locally by moving it temporarily to a stash by running git stash.

  6. Now make your local sync with the remote by running git pull

  7. Now run git reset --hard HEAD~1 (again modify the number to remove n commits) to remove the remote commits from your repository and do a force push git push -f.

  8. You will see your commits from your remote repository are removed.

  9. Now get back your work from stash by running git stash pop

  10. Now do the changes you need to with your unpushed work.

  11. git diff: See the difference in the current working tree from the previous index (Our initial commit) by running the command 

git diff <commit id>
  1. How to avoid accidentally pushing to remote e.g when contributing to open source see Stackoverflow

git remote set-url --push origin no_push


Don't use Git pull: git pull --rebase

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment