Useful Git Stuff

This is one of my old posts from a blog that’s not active anymore. The content is still pretty good so I’ll reshare it here on this blog 🙂

Forking – If you’d like to contribute to a Github project, the very first thing to do is fork the project.

Cloning – Then create a folder on your computer, let’s call it “sandbox”. Use the terminal to cd into it. And then clone the forked repo using git clone repo_address. This will get the project on your computer.

Branching – Next create branches where you can make changes to the code. For example, I created a branch called fix/error_msg to fix or edit the existing error message in Jetpack.

You can create a branch with git branch branch_name and then git checkout branch_name to switch to that branch OR you can create and switch in one single command git checkout -b branch_name. You can see all the branches on the computer using git branch and status using git status.

Changes – You can make changes to the files now but if you need to discard any of those changes just type in git checkout . and you’ll be okay.

Commit â€“ Once you’re ready to commit. Add all the changed files using git add . and then make a commit using git commit -m 'commit_description' where commit description can be “wrapped text in esc_url function” or something like that. Just describe what is this commit about.

Push â€“ Once committed, next step is to push these changes to the forked repo. You do that by typing git push origin branch_name. In my example branch, this would mean git push origin fix/error_msg.

Pull Request – Go to your forked repo and you’ll see an option to create a Pull Request. You should explain what this PR will do and submit it.

Review â€“ If the reviewers ask you to make changes, make changes on your branch, add/stage the files, make a commit, push the changes and it’ll automatically add another commit under your existing PR.

Merge â€“ If your code looks good, the reviewers or code maintainers will merge it to master and it’ll be added to the next release. (btw you can party at this point, you did something incredibly awesome!).

Other useful commands

Deleting Branches. The first one deletes remote branch, the other deletes a local branch.

$ git push --delete <remote_name> <branch_name>
$ git branch -d <branch_name>

Adding a new remote. Here’s me adding a remote called upstream that links to Jetpack’s original repo.

$ git remote add upstream git@github.com:Automattic/jetpack.git

If it’s been a while since you created that fork, you’ll want to make sure you’re using your project’s latest version. Use the fetch and merge commands to apply the latest project changes to your local repository, without losing any of your local changes.

$ git fetch upstream
$ git checkout master
$ git merge upstream/master

Bonus tip: If your current branch becomes stale and master branch moves on with new code, you may need to rebase your pull request.

Git Diff To Sublime Text

I use git diff command to check the difference (additions/deletions) on my current branch. That works just fine but I’d love to have this result in a text editor like Sublime.

This can be done by saving the file in .diff format. Example: git diff > foo.diff and it’ll create a foo.diff file for you. This can then be opened in Sublime Text 🙂

Git Rebase

Today I learned about Git Rebase while making a code contribution (commit) to the Jetpack project on Github.

I had some issues setting up the Jetpack dev environment. The yarn build command wasn’t working as expected.

I tried fixing this by installing command line tools and XCode on Mac but that didn’t help. Digging things further, I realised yarn build worked fine on the master branch but was returning errors on the custom branch (the branch that I was currently working on).

To fix this, I thought it’d be nice if there was a way to reset my custom branch to the master branch and re-apply all the changes I had done so far. This would give me a branch that’s updated with the master branch along with the changes I had done so far on the custom branch.

Turns out, this can be achieved by running git rebase master command. I learned this from this StackOverflow post, which says:

git rebase master does what you’re asking for — takes the changes on the current branch (since its divergence from master) and replays them on top of master, then sets the head of the current branch to be the head of that new history. It doesn’t replay the changes from master on top of the current branch.

That fixed the issue. The command yarn rebuild worked just fine after I rebased my current branch with master 🙂