in mobile

Git command snippets.

List of mostly used git commands

  • Setting up a local git repo
    • Transform current directory to a git repo
      • git init
    • Empty Git repository in the specified directory
      • git init <directory_name>
  • Git Add remote repo
      • git  remote add <remote_url>
      • Verify: git remote -v
  • Clone Remote Git repo
    • git clone <remote_repo_url>
  • Git Detail:
    • git  remote show origin
    • git remote -v
  • Git Branch List
    • Local: git branch
    • Remote: git branch -r
    • Local and Remote : git branch -a
  • Git Branch Create
    • Local: git branch <branch_name>
    • Local checkout: git checkout -b <branch_name>
    • Remote: git push -u <remote_name> <branch_name>
      git push -u origin branch_name
  • Git Checkout 
    • git checkout <branch_name>
    • create and checkout local branch: git checkout -b <branch_name>
    • Checkout remote branch
      • git fetch origin / git fetch –all
      • git checkout <branch_name>
      • git branch -a   //They’ll be called something like remotes/origin/branchname
      • git checkout remotes/origin/branchname
  • Switch Branch
    • Local: git checkout <branch_name>
  • Git Branch Delete
    • Local: git branch -d <branch_name>
      git branch -D <branch_name>   //Force delete git  branch
    • Remote: git push origin –delete <branch_name>
  • Git Rename Branch
    • For Current Branch: git branch -m <new_branch_name>
    • For Specific Branch: git branch -m <old_name> <new_name>
  • Git Merge
    • Merge Specified branch to current Branch
      • git merge <branch_name>
  • Git Stash
    • git stash
    • List stashed changes
      • git stash list
    • Clear all stashed changes list
      • git stash clear

 

 

Sometimes git fetch –all won’t fetch all the remote branch to local. In that follow the following steps.

  • git branch -a      <List both remote-tracking branches and local branches.>
    • debugging
    • master
    • * token_refactor
    • remotes/origin/HEAD -> origin/master
    • remotes/origin/discussion
    • remotes/origin/master
    • remotes/origin/token_refactor
  • git checkout  remotes/origin/token_refactor
    • but when we list local branch with git branch
      • * (HEAD detached from f26f4dc)
      • debugging
      • master
      • * token_refactor
      • remotes/origin/HEAD -> origin/master
      • remotes/origin/discussion
      • remotes/origin/master
      • remotes/origin/token_refactor
    • Here we see the current branch is detached from head this is because any checkout of a commit that is not the name of one of your branches will get you a detached HEAD. A SHA1 which represents the tip of a branch would still give a detached HEAD. Only a checkout of a local branch name avoids that mode. To resolve this problem follow the following steps
  • git checkout -b token_refactor <commit_hash>
  • git pull origin token_refactor

This will resolve the issue and track the branch.

 

Write a Comment

Comment