KeyShoe's Blog

Learning Git

While I’ve always used Git for basic tasks like adding, committing, and pushing changes, I’ve recently started diving deeper into its more powerful features. This is in attempt to try and have a better understanding of the tools which I use on a daily basis.

Git Branches

You can create a branch which basically is a new route to make changes to the code from a point in time. You can freely work on a new branch, make changes, commit those changes and then later merge them to the main branch.

Below are few useful commands related to branches.

  1. Create a new branch.
$ git branch branch-name

# Alternately, use the below command to create and checkout to the new branch
$ git checkout -b branch-name
  1. List down the local branches. The current branch will be highlighted.
$ git branch
  1. List down the local branches alongwith the branches in the remote repositories.
$ git branch -a
  1. To go to any of the existing branch.
$ git checkout branch-name

For now, these are the only commands I have used which are related to git branches. Hopefully I will learn more commands and their usecases as time goes on and new things are added to my workflow.

Git Commits

Committing changes is a fundamental Git operation, but understanding how to manipulate those commits reveals some of Git’s most powerful features. Commits are basically screenshots of code. You can commit your code whenever you have made any type of changes. The main feature of commits is that you can go back in time to a previous commit and start working from there by creating a new branch or by just overwriting the commits.

Here are few of the commands I have started to use.

  1. To commit any change and write the commit message in the editor.
$ git commit
  1. To commit with a one line message.
$ git commit -m "commit message"
  1. To commit the changes to the tip of the current branch.
$ git commit --amend
  1. To commit without any message while amending.
$ git commit --amend --no-edit
  1. To go back to a previous commit, but keep all the changes.
$ git reset --soft HEAD~1

# To go back to a previous commit, without saving all the changes. 
# Use with caution as it is destructive.
$ git reset --hard HEAD~1

These are few of the git commit commands which I have been used recetly.

Daily Commands

These are few of the commands which I have used recently which have helped me in my workflow.

  1. To see all the files which have been changed since the last commit
$ git status
  1. To see the changes made inside any file since the last commit. Really helpful
$ git diff
  1. To add the changes from working directory to the staging area.
$ git add -A
# or
$ git add .
  1. To pick the changes to be added one by one. Really helpful if you made multiple changes and want each commit to be a single change. Have been using this command a lot recently.
$ git add -p
  1. To see all the commits I made in a git repository. This have been really helpful to follow the commit message convention.
$ git log
  1. To push the commits to the git remote repo.
$ git push
  1. To unstage any of the staged files.
$ git restore --staged main.go 
# Alternately, you can use the below command to restore the changes in a file
$ git restore main.go

Conclusion

So, yeah there are a bunch of commands in git and the list above is just scratching the surface of this amazing tool. I am still learning to use this tool to make the best out of it.

I’ll try my best to keep this post updated as I learn new useful commands and add those to my workflow.

Hope this list provides someone the information they need to help them in their learning git journey.

#Programming #Git #Learning