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

Make use of JavaScript console methods

When I say JavaScript console methods, the one and only thing that hit most of our mind is console.log() and we use it a lot and only that some might be familiar with error() and debug(). Did you ever think of checking if there are any other methods available in console other than log? Most of us don't do that, but knowing those methods might have saved lot of our development time. In this post I will show you some of the console methods which will be very useful in our day to day coding.
Log based on conditionconsole.assert(assertion, log_message); To print something on the console only if the assertion failed - assertion can be any expression which returns a boolean.
Note: In chrome this log will print as an error with message saying "Assertion failed:" with the log_message passed. In FireFox its a normal log message.
Log number of occurrence console.count([label]); If we want to count something, it may be a click, may be a callback, or event triggers. We don't …

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.