Top 5 Git commands that make life easy for me

Spread the love

Open Mobile Version.

I remember the transition from SVN to Git some years ago which made me uncomfortable. I had to undo all my CVS learning to finally understand it.

Years later I feel that the switch to Git has actually been a boon in so many ways. 

Here some of scenarios, which don’t occur very frequently, but Git allows me to handle them so easily whenever they do:

Deploying code on servers without SSH access

There have been cases where I had to maintain some code for a project which has code hosted on a shared server without any SSH(read terminal access). So I can’t install Git remotely and just pull my code.

In such a case I usually maintain an local Git repository and whenever I am done with the requested change, I commit locally and  run: 

git diff-tree -r --no-commit-id --name-only --diff-filter=ACMRT 3f3735422773e13edc50300686b41e1721962eb7 | tar -czf file.tgz -T -

Here “3f3735422773e13edc50300686b41e1721962eb7” is the commit id. This creates a tar which has all the files that were changed or added(for deletion you will have to do it manually), with the directory structure. Extract it and upload the code over FTP in one go.

Fix last commit

Sometimes after making the commit I realize a small error in the code or want to fix the commit message. I run:

git commit --amend

This will open your configured text editor and ask for the message.
If you had made some file changes make sure you had used git add changed/file1 changed/file2 before running this command.

I usually run this with VScode which makes it more visual and easy to use.

Merging several local commits into one

I use this when working on a functionality that will take multiple days/commits to finish. It is usually a good idea to push your code to the remote repository daily, so just in case of any hardware failure you still have your code. But it might not be possible to push to your dev branch daily as you might have some “hot-fixes” coming in as well.

I would usually start working on a feature branch say “feature-x”. I would continue to work parallely on my dev branch as well as feature branch, making sure I keep feature branch up to date with dev branch and push both to remote repo.

Once I am done with feature, checkout to my dev branch git checkout dev-branch:

git merge --squash feature-x

Getting just the code without the Git history

In some cases I had wanted to just have the code in its current state without any history, this was to share code with someone or to start a new Git repository.

A simple command to accomplish run this:

git archive --format=zip  master > ../ab.zip   

Get Git repository from remote server

It might happen in some case that you don’t have access to the remote repository, but still have access to the remote server. This case usually happens when moving on from a old development partner to a new one, where you own the hosting server but not the remote repository.

You can run:

git bundle create project.bundle --all

Download this bundle

git clone project.bundle

Now you can push it to a remote repository that you have access to.

I can assure you there are more amazing Git commands but these were my top 5 Git commands that really help me in handling and managing code.

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: