-
Clone a project from remote to your system
git clone https://github.example.com/project/example.git
-
To know the status of your project
- to know the uncommited changes
- your current branch name etc
run
git status
-
create a branch
git branch <newBranchName>
-
Change a branch
git checkout <branchname>
switch to previous branchgit checkout -
-
To create and switch to the new branch (one liner for point 3 & 4)
git checkout -b <newBranchName>
-
change existing git remote url
git remote set-url origin [email protected]:user/repository2.git
-
see available remotes
git remote -v
-
Abort git merge
git merge --abort
Use Case: suppose you are on branch B and ran the commandgit merge A
after getting changes of A, if you want to undo the changes(Assuming you have not commited after merge) run this commandgit merge --abort
-
rename a branch
git branch -m <new-name>
: if you are on the same branch that you want to renamegit branch -m <old-name> <new-name>
: if you are on some other branch
-
Stash all the changes
git stash
-
Stash a particular file
git stash push -m "Your stash message here" lib/../filepath.dart
source: https://stackoverflow.com/a/5506483/8253662 -
to see the list of stash
git stash list
-
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
- 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)
-
reset your branch to an old commit
git reset --hard <hashId>
e.ggit 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) -
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 -
pull a particular commit from any branch
git cherry-pick <commit hash>
-
add a pushed file to gitignore
git rm -r --cached <file or directory name>
-
Pushing an empty commit example
git commit --allow-empty -m "empty commit
example2 -
Rebase the branch to master (flutter/flutter#97046 (comment))
git fetch remote git rebase remote/master # resolve conflicts git push -f
-
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.gfaaca13f22647c647e5c7d20d55c0fed9e06ca19
- then verify if the commit is good/bad by checking out
git chekcout faaca13f22647c647e5c7d20d55c0fed9e06ca19
- after verifying run
git commit good
if its good andgit commit bad
if its bad. - repeat this until you get a final commit which introduced the bug
- firstly you need to run
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 %
- 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
- Delete a branch
Remote branch
git push origin --delete <branch name>
Local branch
git branch -D <branch name>
- Get a copy of a remote branch into a new branch(locally)
git fetch remote <remote branch>:<local branch>
git checkout <local branch>
- 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
-
git tag <tagname>
// e.g git tag v1.0.0 push tags to github usinggit push --tags
-
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.
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
Heres is what happened in my case
-
I was working on a branch
redesign
-
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.
-
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) -
Now you will see your committed files as
unstaged
-
Save this work locally by moving it temporarily to a stash by running
git stash
. -
Now make your local sync with the remote by running
git pull
-
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 pushgit push -f
. -
You will see your commits from your remote repository are removed.
-
Now get back your work from stash by running
git stash pop
-
Now do the changes you need to with your unpushed work.
-
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>
- 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