Skip to content

Instantly share code, notes, and snippets.

@shivampip
Last active November 23, 2020 18:26
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 shivampip/51367dd51a94f0348e21ec6d6b1fd227 to your computer and use it in GitHub Desktop.
Save shivampip/51367dd51a94f0348e21ec6d6b1fd227 to your computer and use it in GitHub Desktop.
Git Branch
  • Show list of branchs
git branch -a
  • Create branch
git branch new-branch
  • Switch to new-branch
git checkout new-branch
  • Delete branch (which is not merged)
git branch -D new-branch
  • Delete branch after it is merged
git branch -d new-branch

Merging branch

  • First go to the branch, that we want to merge into (master)
  • merge my=branch into master (current branch)
git merge my-branch

Merging when conflit

  • created branch cc from master
  • made changes in master
  • made changes in cc
  • unfortunately in both branchs, a same file has also been changed.
  • so switch to master
  • merge
git merge cc
  • It will say CONFLIT, and append the code of both branch in that file
  • Go, edit that file
  • then
git add .
git commit  //only commit, no message
:wq         //if it is vim
  • congratualtions, branch cc is merged into master successfully

Push local branchs to remote

  • push cc to remote (only cc)
git push origin cc
  • pull changes
git fetch
  • pull cc to local (only cc)
git pull origin cc
  • create pull-request on github site
  • it can be merged to master on github site

Shortcuts

  • Create new branch and switch to it
git checkout -b my-new-branch

Case-study with 2 computers

  • A created new branch aa
  • A made changes and pushed it to remote
git push origin aa
  • B pulled changes
git fetch
  • B switched to aa branchs (yes, it is auto setup)
git checkout aa
  • B makes changes to aa
  • B pushes it
git push origin aa
  • and so on

Happy coding

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