Skip to content

Instantly share code, notes, and snippets.

@rahulkhatri19
Last active September 23, 2022 04:06
Show Gist options
  • Save rahulkhatri19/ebfe8abe0a85ed706546bb9ae17aa05e to your computer and use it in GitHub Desktop.
Save rahulkhatri19/ebfe8abe0a85ed706546bb9ae17aa05e to your computer and use it in GitHub Desktop.
Basic git commands

Basic git command

  • General commands: create branch, add changes to git, commit/save changes, push code

    $ git checkout -b [branch name] // create branch - git checkout -b master

    $ git add . // add code to git

    $ git commit -m "message" // commit chages

    $ git push origin branch_name // push code to origin - git psh origin master

  • check status of git: $ git status

  • Tag:

    $ git tag -a v1.4 -m "new version 1.4" // creating new tag 1.4 with mwssage new version 1.4

    $ git tag v1.4-lw // light weight tag, it only store checksum

    $ git show v1.4 // check what new in tag

    $ git push origin v1.4 // push tag to origin

    $ git push origin --tags // all tags

    $ git tag -d v1.4 // delete a tag in local rep

    $ git tag -d v1.4-lw // delete a light weight tag in local rep

    $ git tag --list // get list of tags

  • Stashing:

    $ git stash show // show current stash difference

    $ git stash show stash@{1} // show stash difference

    $ git stash save "stash_name" // save stash

    $ git stash list // stash list

    $ git stash apply // it will give back current changes

    $ git stash apply stash@{2} // it will give back particular stash changes

  • Change commit:

    $ git commit --amend -m "message" // to change curent commit message

    $ git commit --amend --no-edit // last commit modify

    $ git reset --hard HEAD~1 // delete current commit.

  • check difference in a file before commit:

    $ git diff file_name // git diff .gitignore

  • pull remote branch:

    $ git checkout --track origin/branch_name

  • merge:

    $ git merge branch_name_to_be_merge // for merging be on branch which will be after merging example be on master

  • rename a branch:

    $ git branch -m <new-branch-name>

    $ git branch -m <old-branch-name> <new-branch-name> // if on another branch

  • delete or remove local branch:

    $ git branch -d <branch name> // be on any another branch

  • delete or remove remote branch:

    $ git push --delete <remote_name> <branch_name> // be on any another branch

  • Adding git in a project and config to github:

  $ git init
  $ touch README.md
  $ git add README.md
  $ git commit -m "first commit"
  $ git remote add origin remote_repository_link  // https://github.com/rahulkhatri19/repos_name.git
  $ git push -u origin main
  • Push an existing Git repository
$ git remote rename origin old-origin  // renameing origin to remote origin, if you want to maintain more than one remote branch.
$ git remote add origin https://gitlab.com/rahulkhatri19/firebaseauth.git
$ git push -u origin --all
$ git push -u origin --tags
  • Delete old remote url from local

$ git remote remove old-origin

  • Adding existing project to github:

    $ git remote add origin remote_repository_link // https://github.com/rahulkhatri19/repos_name.git

    $ git push -u origin main

  • removing/deleting folder/Directory from git remote:

    $ git rm -r folder_name // git rm -r app/.cxx

  • removing/deleting file from git:

    $ git rm folder_name/file_name.extenshion // eg $ git rm .idea/file.txt

  • removing / deleting local changes:

    $ git checkout -- file_name // eg $ git checkout -- app/src/main/java/com/project_name/MainActivity.java

  • pull a tag code / get old code in new branch:

    $ git checkout -b newBranchName tagversion /// git checkout -b oldcode v1.2.1

  • git check global url:

    $ ~/.git/config // to edit use $ nano

  • check remote url or repository linked with which remote url push and pull both:

  $ git remote -v
  > origin  git@github.com:USERNAME/REPOSITORY.git (fetch)   
  > origin  git@github.com:USERNAME/REPOSITORY.git (push)
  • Change your remote's URL from SSH to HTTPS:

    $ git remote set-url origin https://github.com/USERNAME/REPOSITORY.git

  • Generating SSH key with RSA encryption:

    $ ssh-keygen -o -t rsa -b 4096 -C "email@example.com"

    Save path, if you want a specific path then enter it or leave blank for default.

    asked to override so Y.

    Enter passphrase : // Just to add extra security level

  • Change user detail in particular project:

    Go to : .git -> config (text file) -> // .git folder and config file then type at bottom:

[user]
	email = your@email.com
	name = Rahul
  • unable to update code from global to local machine:

    $ git pull . origin/branch_name

  • change upstream branch:

    $ git branch --set-upstream-to origin/branch_name

  • git update local branch from remote:

    $ git pull origin branch_name

  • stop rebase at any stage:

    $ git rebase --abort

  • start interactive rebase:

    $ git rebase -i HEAD~2 // 2 -> last two commit, HEAD ~ sign then number where number is last commit number.

    Now a editor is there, to edit text editor press i -> to be in insert mode save changes -> esc : wq

    // For changing name of author replace 'pick' with 'e' to edit desired commit.

    To change author name ->

  $ git commit --amend --author="Rahul Khatri <khatri.rahul019@gmail.com>"
  $ git rebase --continue
  $ git push --force origin master

Note: Whithout force push chnages will not be effect in server.

  • Retrive deleted file/folder if commit not done:

    $ git checkout -- file_path/folder_path

    If error came in retrive deleted file/folder than need to index first :

$ git reset -- file_path/folder_path
$ git checkout -- file_path/folder_path
  • Rebase update code

$ git pull --rebase origin branch_name

  • Find out SHA Key

$ .\gradlew signingReport

For Android: Just go to Your project in CMD and try above comand.
// ...(Project Path) AndroidStudioProjects\Project_name> .\gradlew signingReport

For Flutter: Just Go to project and inside android folder open CMD.
// ...(Project Path) FlutterProjects\Project_name\android> .\gradlew signingReport

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