DEV Community

Abbi Devins-Suresh
Abbi Devins-Suresh

Posted on

Tips/Tricks for a beginner to Git/GitHub

When I switched from medicine to software engineering as a career path, one of the first intimidating things I had to learn was how to handle source control.

The first hurdle was to understand why I needed another copy of my work in a remote location (something I quickly understood the reasoning for when I deleted something that was working and couldn’t get it working again haha because I couldn’t remember what state things were in)

So below is a compilation of the commands I’ve learned in my Git journey to help me stay sane. I’m sure a bunch of these could be found online somewhere (maybe even on official documentation sites but I don’t know about you but I found them and even to this day find them very hard to approach and understand sometimes).

The below situations are some of the most common scenarios I’ve run into (after the initial step of creating a repo on the GitHub website and then connecting the repo with a project I have locally):

Show & pull remote branches

git branch -a OR git branch -r

git checkout -t remotes/repo/branch
Enter fullscreen mode Exit fullscreen mode

Clear tracked files

git clean -d -f
Enter fullscreen mode Exit fullscreen mode

Accidentally commit and push a file

git rebase -i [commit hash from before change]
Enter fullscreen mode Exit fullscreen mode

Change word pick to edit of hash related to commit
Make changes to files
(if the only change you need to do is to remove the file)-

git rm [path to file] -r
Enter fullscreen mode Exit fullscreen mode

If you need to modify that file as well, first modify the file

git add changed file
git commit --amend
git rebase --continue
git push --force
Enter fullscreen mode Exit fullscreen mode

Alternatively for accidentally committing and pushing a file:

git reset — soft [prior commit hash]
git reset file_name
git commit -m “new message for commit”
Enter fullscreen mode Exit fullscreen mode

NOTE: for only single commit in branch:

git commit -a — amend 
Enter fullscreen mode Exit fullscreen mode

after fixing issue locally

git rebase -i master
Enter fullscreen mode Exit fullscreen mode

Show current branch:

git branch — show-current

Unstage a file added:

git restore — staged [file path]
Enter fullscreen mode Exit fullscreen mode

Undo last commit locally:

git reset HEAD~
Enter fullscreen mode Exit fullscreen mode

Cherry-pick:

Merge main in dashboard
Choose sha of commit
Switch to branch you would like

git cherry-pick -x <sha>
git push
Enter fullscreen mode Exit fullscreen mode

confirm push worked and build is successful

Please let me know if there are any mistakes or anything you would add :) I’m definitely still learning all of this even though I’ve been in the field for almost 3 years now.

Top comments (0)