Skip to main content

Git and Github useful commands

Working on a personal forked repository

Most of the time when you are working in an organisation who uses git, you will be requested to fork the project and create pull for your feature or bug fixes (Most of the open source contribution in git will be happening like this).

In this note I will let you know some useful git commands which will be very helpful for you in your day to day life with git. I am not going to explain any git technical terms. I am assuming you already know about it or all those terms are linked to a proper source for more information.

Lets start.
  • You can fork any github project by clicking the fork icon on the top right of a repository. 
  • git clone<your-github-id>/Project.git - to clone your forked Project (personal repo)
  • cd Project/ - get into your repository
  • git fetch origin - will fetch all the details/indexes (branches and commits) from remote origin but will not apply.
  • git branch - is used to check all the available branches and current working branch (working branch will have * prefixed
  • git checkout origin/master - change your current working branch to master.
  • git pull origin master - update your local branch to with your fork repo in the remote(origin)
  • git add file_path - add this file to commit
  • git rm file_path - delete file from git
  • git commit - commit your changes (It is allways good to have your repo uptodate(pull the code) before you do a commit else you may be getting a merge commit).
  • git push origin master - push your commits to remote repositories master branch.

Changing the last commit

  • Don't use this if you pushed the code to remote repo (specifically public repo) - because you have to do -f push which will change the history of others commit also if any.
  • Do all the changes same as you do for a normal commit (add , rm) and do commit with --amend flag
  • git commit --amend this will overwrite your last commit, don't worry your previous changes will also be there.
  • If you just want to change the commit message, simply enter git commit --amend which will prompt you for commit message with last commit message auto filled.
  • For more info refer Changing the last commit

What is git stash?

  • It is simply a backup for your local modified files since last commit of the current branch.
  • git stash save - save/backup modified files
  • git stash list - list all the saved stash
  • git stash pop - apply saved/backed up files to the current branch

Sync/Update your fork(personal repository) with the upstream(remote repository)

  • git remote -v - to list out all the remote repositories(remote url's and it alias names) you have added. origin will be by default created and pointing to the repository from where you clone the project.
  • git remote add upstream - add upstream as a remote which points to main repository of your project
  • git remote -v - Check you new remote listed
  • git fetch upstream - fetch all the details(branches and commits) from remote "upstream"
  • git stash save - Save your local changes if any
  • git pull --rebase upstream master - update your local repo with upstream repo ( --rebase is used to avoid merge commit ) make sure you are not much deviated remote repository else you will end up reolving conflicts for a long time.
  • git satsh pop - if you did stash save before, apply this to the new updated code now.
  • git push origin <branch_name> - update your fork with the local changes
  • For more info refer: Sync a fork with upstream

Merge/Squash branches

  • To merge all the changes from branch1 to current branch
  • git merge --squash <branch1> this commmand will get all the changes from your branch1 to your current branch and will not commit it. You can see the modified files using git status.
  • Now proceed to commit as usual and dont forget to delete the branch1, else your changes will still be there and create problem when you do merge (squash) for next fix.
  • Ref: git merge doc

Update/sync a branch with other branch changes

git checkout <fix1_branch>
git rebase <sync_from_branch>(can be master)
or git rebase <sync_from_branch> <fix1_branch> this will update your branch and apply all the changes of current branch on top of it.
  • rebase will update the git log also, whereas squash will just merge the changes but will not commit the same

Reset your fork to upstream

  • You messed up your fork and wanted to reset your fork to upstream as if you forked freshly.
  • git reset --hard upstream/master - your all local changes will be WIPED OUT (master is a branch name you want to reset to from upstream)
  • Now update your fork forcely - git push origin <branch> -f

Creating and applying patch

  • Creating patch files

    • Patch of an entire branch git format-patch master --stdout > fix_branch.patch - your current branch should be <fix_branch>.
    • Above command will create a new file fix_branch.patch with all changes from the current branch (fix_branch) against master branch.
    • Create patch for a single commit git format-patch -1 commitssh --stdout > commit_name.patch
  • Applying patch

    • Check what are all the changes in the patch using git apply --stat fix_branch.patch
    • Check whether you have any issues appying this patch by git apply --check fix_branch.patch
    • Now apply the patch using git apply < fix_branch.patch - you can see the applied changes in git status
    • When you apply patches created from someone else you can use git am --signoff < fix_branch.patch - so that signed off message will be appended to your commit.
    • Alternatively you can use patch command patch -p1 < path/file.patch

Revert all permission changes

  • git diff -p -R - See all the diff in reverse order
  • grep -E "^(diff|(old|new) mode)" >revert_modes - Take all the mode changes alone
  • git apply <revert_modes - Apply all those reversed permission (mode) changes
  • It can be done in a one liner - git diff -p -R | grep -E "^(diff|(old|new) mode)" | git apply

Creating a git alias

  • git config --global --add alias.alias_name 'your_git_command' - will add a git command alias
  • eg: git config --global --add alias.update "git stash save && git pull --rebase upstream && git stash pop" - now you can simply use git update.

Good to read


Popular posts from this blog

TataSky Refresh after Recharge - Activate after Recharge

If you are using TataSky and doing recharge occasionally and that too after disconnection, this post will be very helpful for you.
Most of the time you will not get the channels listed as soon as you make the payment. It will display message to Subscribe for that channel.
You can easily get channels back by simply giving a missed a call from your registered mobile number.

Note: Make sure your TV and SetupBox is on and showing the error message

Give missed call to +91 80892 80892 (Soft Refresh)wait for 1 minute and
If still not active then try giving missed call to +91 90405 90405 (Heavy Refresh).
Ad:Planning to buy a Best low budget Smart TV? Consider Mi LED Smart TV 4A PRO - You can get this TV as low as for 11,500 Rs - which has Bluetooth, WiFi, Android, 8 GB storage and good customer ratings (4.4/5). Note: Price based on offers, click on the link to see current price on the web page If the above step did not help you, there might be something else going wrong, please contact TataSky S…

Installing Cordova on Ubuntu 16.04 without Android Studio

Apache Cordova is a platform to build mobile applications using JavaScript, HTML5 and CSS.

When I was trying to install cordova for creating an android application on my Ubuntu 16.04 I was facing lot of versioning and dependency problems. So in this post I will be discussing about all those issues and the solution that worked for me. All the steps are via command line and so no android studio. I don't want to install GB's of files to just use sdk.
Install latest version of node and npmInstall CordovaInstall android SDK (Command line)Setting environment variablesCreating android plaformCreating and running first app Installing latest version of nodejs
By default ubuntu 16.04 has a stable version of nodejs added in its own repository. So to install latest version from Ubuntu itself you can simply use apt commad as follows.
$ sudo apt update $ sudo apt install nodejs But when I was using this version (4.2.6 is the latest stable version available when I was trying) I got some…

My First Post

After lot of thinking and research me too started a blog finally to share my knowledge :-).

Not everybody know everything but also everybody else don't know what we know. So lets share and gain knowledge with Win Win approach.