My GitHub Setup, Tricks & Daily Commands

Sridhar KattaSridhar Katta
7 min read

Over the years I have picked up a lot of GitHub tricks, ways that I work around GitHub. I mostly use some set of commands and want to make a list of it & have it for reference.

Setting Up SSH:

I think anyone who is working with Git & GitHub should set up SSH on their machine, because it gives us more flexibility than anything. There are other ways to do it like personal access tokens, but those are hard to set up, and keys often get expired after some time. You need to store that token somewhere & always have to copy & paste whenever you need to. So why do that stuff? It’s better to set up SSH whenever you start working with GitHub.

  • Go to your terminal from Linux or Mac, run the below command & press enter. It will create two files: one is id_rsa & the other is id_rsa.pub.

  • Now go to your profile settings on github.com & add your SSH key contents from id_rsa.pub with a description.

ssh-keygen -t rsa -b 4096 -C "your_github_email@example.com"
  • Now to authenticate whether you have added it correctly or not, from your terminal run this command & type yes to authenticate. Now you can start using GitHub with SSH.
ssh -T git@github.com

Setting up multiple GitHub Accounts

Over time I had to set up multiple GitHub accounts, so I have followed this approach to create it. Go to your terminal in your .ssh folder and check for a config file. If it is not there, create it.

  • Open your config file in your favourite editor of choice (I like NVIM for these kinds of things).

  • Add the following content into your config file. You can change your host name as you like.

# Sridhar 2nd GitHub account
Host github.com-sridhar-2nd
     HostName github.com
     User git
     IdentityFile ~/.ssh/your_ssh_file_name
  • Once you add the above configuration, whenever you want to clone or want to use your second GitHub account, you need to change your clone URL or git remote URLs.
# this will be authenticated using my first SSH key
git clone git@github.com:sridhar02/pointing-poker.git

# using second SSH key
git clone git@github.com-sridhar-2nd:sridhar02/pointing-poker.git

# Remove remote origin — this command will remove your current repo origin
git remote remove origin

# Set remote origin — if the repo is already there, you can change it to use the 2nd account
git remote add origin git@github.com-sridhar-2nd:sridhar02/pointing-poker.git

Setting up dual GitHub accounts is as simple as this. It is not a big thing at all.

Branch Creation

When you work with Git, you create your own branches from your main or master branch. Use the below command to create from your current branch:

git checkout -b branch_name

Change Branches:

Sometimes you may need to hop from one branch to another. You can use the below command:

git checkout branch_name

Note: If you are changing from main to your feature branch, easiest way to change is using the switch command. This will take you to your previous branch from your current branch. I got to know this from a friend when working with him & from that time this has been one of my most used commands.

# Switch branches from current to previous
git switch -

Fundamental Commit & Stage Commands:

  • Once you clone a repo from GitHub & add changes, you can see your changes using the status command, which shows a list of files changed & untracked file changes like adding new files.
git status
  • Once you have your changes ready, stage them using the below commands:
# Stages all your current files
git add .

# If you only want to stage separate files, add them one by one
git add file_name
  • Once files are staged, add your commit message:
git commit -m "add your commit message"

Push & Pull

  • Once you have staged & committed your changes locally, now you need to push your changes to your origin. Use the below command:
git push origin branch_name

# Sometimes you will be asked to set upstream origin so then you can use:
git push --set-upstream origin branch_name

# Once upstream is set up you can just do:
git push
  • Sometimes you need to pull the latest changes from your branch from origin to local, then use this command:
# Branch name can be anything — your current branch or main or master
git pull origin branch_name

Note: One thing I personally follow is not to pull main or other branches into your current branch as it disturbs the current commits that you have worked on & adds extra commits instead of just yours. Best way to update your current branch is to rebase or merge. I prefer rebase as it gives a clean history (covering rebase next). Only use pull when someone has updated your branch directly on origin & you don’t have those changes locally.

Rebase & Merge

  • Rebase: When you work with multiple people, often code gets pushed to main or master continuously & you might be working on your separate branch. When you often need to update your current branch in line with main or master, use rebase. The below command rebases your branch on top of the main branch. Most of the time it works easily, but when the same file is changed by multiple people, you may need to resolve merge conflicts & stage them to continue the rebase until it finishes.
git rebase origin/main

Note: If your branch has 10 to 15 commits then one problem you may run into when rebasing is resolving merge conflicts after every commit, which can be tricky & time-consuming. In those cases, you can opt for git merge which basically merges the current main into your branch (adds an extra commit). One more thing to add — whenever you rebase a branch, you need to force push that branch when pushing to origin. Use --force-with-lease only — don’t use just --force as it is the safer option.

# Force push with lease (safer)
git push origin branch_name --force-with-lease

# Force push (not safer)
git push origin branch_name --force
  • Merge: This is also another way to update your current branch with main or master. This merges your main changes into your current branch. (You can explore some advanced things as well in merge when you add new commit messages & other options — explore that on your own.)
# merge main into current branch
git merge origin/main

Stashes

I think this is my most used command in Git, as I often work on something I can’t push or want to just keep locally. Stash command is the most useful.

# Git stash your current changes
git stash 

# Git stash with custom message & untracked files
git stash save -u "stash message"

# Git stash list
git stash list
  • Once you stash some changes, then comes the part to apply or pop those changes into your current branch. Apply means applying stashed changes while keeping them in the stash list. Pop means applying and removing the stash from the list.
# Apply stash, 0 or whatever index you want from stash list
git stash apply stash@{0}

# Pop stash
git stash pop

Note: One better option is to use stash apply instead of pop if you think you'll need those changes again. I mainly use pop for instant updates to the current branch or when rebasing for a quick turnaround.

Cherry Pick

This is the best way to move commits from one branch to another. I often work with release & main branches where I merge commits to main, then cherry-pick and merge them to release branches. Switch to the branch where you want to add the commit, copy the commit ID you want to cherry-pick & run the below command:

git cherry-pick commit_hash_id

Reflog

To view your recent Git commit history (including branch changes, resets, rebases, etc.), you can use the following command in the terminal. Personally, I’m not a fan of logs from terminal — I just use Git Graph extension from VSCode.

# Extra --oneline parameter gives a nice list in one-liners
git reflog --oneline

Conclusion

Git is one of those tools that sticks with you through every project. Learn it once, understand it well, and it’ll make your daily workflow smoother, faster, and less frustrating.

Resources

These are some extensions I use with VSCode & guides I refer to when I need to know something:

5
Subscribe to my newsletter

Read articles from Sridhar Katta directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sridhar Katta
Sridhar Katta

A developer, geek, enthusiast, who loves to solve problems and fix things with technology.I am working on 💻frontend web development with Javascript and I love contributing to 🌟 open source.