Renaming a Local GitHub branch and applying the change to its corresponding Remote branch


So, this is a ‘Tutorialeto’ to help you rename your GitHub branches when suddenly your Program Manager asks you to change the names as per her standards(I know you hate that 😛 ).

git branch -m old_branch new_branch # Rename branch locally
git push origin :old_branch # Delete the old branch
git push --set-upstream origin new_branch # Push the new branch, set local branch to track the new remote

The following commands will help you accomplish the same. Enjoy!

So you wanna GIT ?

Why GIT ?

Every time you’re asked to submit that crucial programming assignment what comes to your mind after completion of the program ? Uh, not a movie. It’s GIT I know ! So, that’s exactly how you proceed towards storing your lovely little program forever so that you can hug your code every time you miss your ex right?

Git is a terrific way to store not just code but almost every kind of file you can think of that can be stored online. It employs altogether a different approach of controlling versions of your files. While other similar tools like SVN are Centralized Revision Control Systems (CRVS), Git employs Distributed Revision Control System(DRVCS). So, every person having access to your repository can clone your code and maintain a local copy of exactly the same data as in GitHub and can make changes locally with full control. So, in some catastrophical situation, god forbid, some client who cloned your code from Peru can help you to recover your data !!!

There can be nothing better than Git’s own website but I am here to help you skip some contrived details and dive right into basic usage of Git.

Basic Commands

        • git init – Initializes a local Git repository in your current local directory
        • git clone <remote_repository_name> – Clones a remote repository to your local git repository
        • git add <file_name1,file_name2...> or git add * or git add . – Stages (consider it analogically as a local buffer where you store files that are to be committed) your marked files or all for commit
        • git commit -m "<message>" – Commits the staged files with ‘message’ as commit message
        • git push <remote_name> <remote_branch_name> – Pushes your code to the remote named ‘origin’ and its branch ‘master’
        • git remote add <remote_name> <remote_repository_URL> – Adds a remote repository of given name and given URL
        • git remote -v – Displays all remotes with URLs
        • git checkout -b feature_x – Creates a new branch ‘feature_x’ and switches to the branch
        • git branch -a or git branch -r or git branch– Shows branches ; ‘r’ for remote only
        • git checkout -- <file_name> – Discards changes to file
        • git log – Gives commit history
        • git status – Gives status of staging area and working directory
        • git checkout <branch_name> – Moves the HEAD to the specified branch
        • git pull – Pulls code from remote repository’s tracker branch (default /master) to current local branch
        • git fetch origin – Fetches code first from Remote repository’s tracker branch to local branch without merging th code. Gives a chance to check the code before merging
        • git merge <branch_name> – Merges fetched code from specified branch

Note : I will be covering a topic on Basic Branching and Merging in GIT including Merge conflicts soon

Don’t play around with the commands below !!!

      • git reset --soft HEAD~ – Move HEAD to previous commit, Staging Area stays the same
      • git reset --mixed HEAD~ – Move HEAD to previous commit, Staging area also gets erased, Working Directory unaffected
      • git reset --hard HEAD~ – Move HEAD to previous commit, Staging area erased, Working directory moved to previous commit DANGEROUS !!!
      • git reset HEAD <file_name>– Unstages the specified file