Commands
Aniket Kulkarni  

Git Commands you should know

In this post, I am going to discuss useful Git Commands. I like using Git commands instead of the Git GUI. Here are the some commands, which I came across my IT journey. Firstly, I will start with simple yet powerful branching in Git. Followed by committing, pushing the code to the repository. Frequently used, pulling, merging code. At last, when you wish to contribute in any open source repository. Most of the time you work on fork. Let’s jump into the Git Commands:

Branching

Git Branching is really powerful, when the development is happening in parallel. Two or more developers can create their own branch and develop the feature seamlessly using branches. Let’s look at the commands:

  • Create a new branch
git checkout -b my_feature_branch
  • List all branches
git branch --all
  • Delete a branch from local repository
git branch -D my_branch_name

You can specify multiple branches in the above command by using separator as space. Here is an example:

git branch -D my_branch_1 my_branch_2
  • Get all the remote branches at local
git remote update origin

This will also, update any branch exist on the local with remote branch

  • To switch to last branch from current branch
git checkout -

e.g. You are on feature branch “my_feature” for some reason you checkout to master. Now, you are done with master branch and want to switch to my_feature branch again then just issue above command. You don’t need to mention the branch name explicitly. I found this command very handy when you want to quickly take a master pull and switch back to the feature branch.


Commit and Push

After you write your code, you can choose to commit the code. You can either commit specific files or all the modified and newly added files. When you commit the changes, changes are committed on local machine. These are not yet on the remote repository. Git push command is used to push all the changes from local machine to the remote repository.

  • Add all files to commit
git add .

Here, dot (.) means add all the files to commit

  • Add specific files
git add my_file1 my_file2

This way you can add only relevant files that you want to commit.

  • Commit the file to local Repository
git commit -m "My dev feature - Initial commit"

In the commit command you need to add the message by specifying the -m. This will commit all the changes to the local repository, not the Origin/Remote.’

  • Push to remote branch named feature_branch
git push origin feature_branch

git push would work too. But I would like to add the branch name explicitly. This helps when you are pushing code to master branch, mind replies “be careful”


Pull, Rebase and Merge

When many developers are working on the same project, by creating feature branches. They often need to pull the code from the master/main branch and rebase or merge with the feature branch. This helps to maintain the code integrity.

  • Take update from origin master branch
git pull origin master
  • Merge the master branch into feature_branch
git merge master

Make sure you are on the feature_branch before running the command

  • Pull the latest code from master and rebase in one go
git pull --rebase origin master

This command pulls the remote master and sync with local master. Additionally, –rebase with origin master. If you are working some feature branch and want to rebase with the master branch, then this command is really helpful.

  • When you issue “git status” command and you see the output something like this
Your branch is ahead of 'origin/master' by 4 commits.

To solve this you can rebase with the master.

git pull --rebase

Fork

I would prefer using branches while working on forked repository. Follow the steps to commit a feature into the fork repository. This assumes that you created a fork and cloned on your local. Make sure you are on master branch of fork.

  1. Add fork to upstream master
git remote add upstream git@github.com:aniketskulkarni/coding-problems.git

2. Pull the latest code from upstream master. Make sure you are on master

git pull upstream master

3. Push the updated code to origin master

git push origin master

4. Create a branch

git checkout -b my_feature

5. Commit the code and push to my_feature branch. Raise a PR for upstream master.

Follow from the step 2 to step 5 to push a new feature to fork branch.

You may be wondering what is upstream master and origin master. To get the clear picture of this. Issue the following command.

git remote -v

Well, this is not all. I will keep updating this post as and when I find some interesting to share with you. For all other command, please refer the official doc.