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 git@github.com:<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 git@github.com:remote/Project.git - 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

Comments

Popular posts from this blog

Upgrade your kitchen with latest Tech Gadgets and Make cooking faster

Welcome to our latest blog post, where we embark on an exciting culinary journey to elevate your kitchen experience with cutting-edge technology gadgets. In this fast-paced world, cooking has become more than just a necessity; it's an art form that deserves the finest tools. Join us as we explore the innovative tech solutions that can revolutionize the heart of your home, making cooking not only faster but also more enjoyable and efficient. From smart appliances to intelligent cooking assistants, we'll introduce you to a range of futuristic kitchen companions that are sure to inspire your inner chef and transform the way you create delightful meals for yourself and your loved ones. Get ready to upgrade your kitchen and embrace the future of cooking!  In this blog post, we will narrow down the vast array of available kitchen tech gadgets and present only 10 items, carefully selected based on product ratings, price, and their suitability for Indian kitchens.  Are you a Telegram u

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  Acer 109 cm (43 inches) I Series 4K Ultra HD Android Smart LED TV AR43AR2851UDFL (Black)  - You can get this TV as low as for 20,000 Rs - which has Bluetooth, WiFi, Android 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 ste

Duplicate file finder/Remover using perl and SHA1

When you are using a computing devices (either a laptop or PC or a Tab) for your personal use after some time (let take some years) you will realise that your disk is full and most of the space are occupied by duplicate files (Same copy of file located in different locations). For ex: You might have a favourite music file in "My Favourite" folder as well as in the "Album" folder. But finding this duplicate manually is a biggest process. That too if the file names are different OMG!. There are lot of free utilities available to do this in automated way, but if you are a programmer, you will always prefer to do it on your own. Here are the steps we are going to do. This is purely on a linux - Ubuntu system.  (for windows you might need to change the path as per conventions ) Getting SHA1 for all the files recursively in a given directory Compare SHA1 with other files Remove the duplicate file Getting SHA1 of a file Using cpan module   Digest::SHA1 we c