Handy Github CLI Commands and Actions

Posted On Dec 03, 2023 |

CLI Commands and Actions for Git and Githbub.

This is mostly for ME. Others might find it useful, but most of this is for me to have one place to go for these commands & scenarios instead of searching the web for them every time. You could also get this code from my github repo here

Be sure to bookmark this page for future use. Cheers to coding 🎉

-----------------------------------------------
-----------------------------------------------

delete branch locally

git branch -d localBranchName

delete branch remotely

git push origin --delete remoteBranchName

Create new git branch

git checkout -b branchName

This is shorthand for:

git branch branchName
git checkout branchName

Make an empty commit for pushing to production.

git commit --allow-empty -m "Commit message"
git push -u origin branch_name 

Scenario

  • Has a pre-existing remote repo with code
  • Has a local folder that is not a git repo but I want to turn it into a git repo and sync it with my pre-existing remote repo

How to get it done.

  • Go to local repo
  • git init
  • git remote add origin <ssh link to remote repo>
  • git fetch - this will bring upstream code down to local repo
  • git checkout -b "new branch name" - this will move the new code changes to the new branch
  • git add on the new branch
  • git commit -m "message" on the new branch
  • git push -u origin new_branch_name
  • On github, merge the new branch back into master branch
  • On terminal (local computer). Do a git pull to merge online master branch to local master branch
  • Proceed to make the necessary changes like deleting some files, moving things around, e.t.c from master branch. Could also do this step before merging to master branch.

ANOTHER VERSION of the above instructions

  • git init on local computer
  • git remote add origin <ssh link to remote repo>
  • git pull <ssh link to remote repo> <remote branch name>
  • git add <local file>
  • git commit <message>
  • git push -u origin master

How to resolve a merge conflict where master branch has changed resulting in your own code being out of sync with master

From the commandline

  1. git checkout master
  2. git fetch
  3. Git checkout the branch you were using to do your work
  4. git merge master
  5. If the code merges automatically, GREAT. If not, it will show you things that couldn't be merged automatically. To resolve this, Go to VSCode or your IDE. Click "Accept both changes"
  6. Push your changes back to github and merge conflict should be resolved.

After setting up SSH, how to add email and username

git config --global user.email "email@example.com"
git config --global user.email (To confirm setting) 
git config --global user.name "Mona Lisa"
git config --global user.name (To confirm setting)

Checkout remote branch

git fetch or git fetch origin

git branch -v -a To see list of branches fetch from remote github repo

git checkout remote_branch_name or git switch branch_name or git switch -c branch_name origin/branch_name

Merging changes from master into my branch

git checkout custom_branch && git rebase master

---------------------------------
---------------------------------

I hope you enjoyed this blog. Feel free to get access to my FREE Technical Resources where I give away stuff worth 10X more than this blog for FREE.

And you will get emails whenever I publish new content.

Or maybe you don’t want access to my juicy free technical resources but you still wanna be in the know, feel free to join my free technical newsletter.

Follow me on Youtube

Follow me on Linkedin

Follow me on Github

Follow me on Twitter

If you want to contact me, speak to me here

Want me to create a similar technical blog for your brand? Contact me here.


Categories: : Git & GitHub