Posts Tagged Under: git

Git – Handy git tools and utilities

Find and get rid of really big files from your git repo:

Handy tool for getting rid of stale bug/feature branches that have already been merged in to the main release/master branches:

Git – rebasing + squashing process, followed by merging via pull request

Let’s say I have a feature branch called “feature-x” which I want to first rebase onto the “develop” branch. Then it is first best practice to squash all my feature-branch’s commits into a single commit and then merge it into the develop branch via a pull request.

first checkout the develop branch and pull in the latest changes.

$ git checkout develop
$ git pull

Now do the rebasing:

$ git checkout feature-x
$ git rebase -i develop 

At this stage just use vim’s “wq” option to save and quit. This does the rebasing without any squashing.

Now we do the squashing, using the “f” option, for each commit:

$ git rebase -i develop

Now we use the following to create a new overall commit message:

$ git commit -v --amend

Tip: Here are some advice on how to

Git – Create a pull request

do the following to add new branch to stash:

git clone profiles repo: git clone ssh://
git checkout new-base-profiles

git branch temporary-branch-name # creates new branch
git checkout temporary-branch-name
# make changes to files.
git add
git commit
git push origin temporary-branch-name
then on stash -> create pull request

source = temporary-branch-name
destination = new-base-profile

Git – useful links

Git – The 3 main parts of git

A git project is made up of 3 parts:

  1. Working directory – This is the directory where all your project files and folders reside (along with the .git folder). Each of your files within this directory is in 1 of possible states, untracked, unmodified, modified, staged. Will cover more about file states later.
  2. staging area – This is a hypothetical layer which sit’s on top of the last commit’s layer. When you run the “git commit” command the 2 layers get’s merged. Any files that are in the commit layer, that has a newer file directly above it (in the “staging layer”), will get over-written by the newer (staged) file. Note, that a file’s content are tracked so that you can roll back to how the file

Git – Removing files

If there is file that is being tracked by git, but is a file you now want to delete. Then the best way to do it is by running the following git command:

$ git remove filename.txt

This command does 2 things, it first changes the file’s state to untrack, and then it actually deletes the file from the working directory altogether, i.e. it does the equivalent of “rm filename.txt”.

You can still remove the file using the conventional way of “rm filename.txt”, but you would then have to stage the deletion of the file as well, hence you have to take a extra step with this approach.

Irrespective of the approach you take, you will then need to commit this to confirm the deletion.

If you just want to remove a file

Git – resetting Git.

If you want to drop all your local changes and commits, fetch the latest history from the server and point your local master branch at it like this

git fetch origin
git reset --hard origin/master

Git – Intro to branches

To view a list of all branches do:

git branch -a

the one that has an asterix next to it is the one that is currently checked out.

Git – Renaming or moving files

The best way to move or rename a file is doing it a via git’s mv command, e.g.:

$ git mv filename.txt filename2.txt

This will not only make git aware that the file has been moved (or in this case renamed), but it will actually rename the file in the working directory.

You can still use the traditional mv command, but this won’t get picked up git, and so you have to manually tell git of this, like this:

mv filename.txt filename2.txt
git rm filename.txt
git add filename2.txt

Hence the git mv command can do all of the above, but in a single line.

Finally you have to commit the change to take a snapshot for the above to be captured in git’s internal db:

git commit -m "filename.txt has been renamed to filename2.txt"

Git – Viewing past commits

Simply run:

git log

the log command has a huge number of options available:

git log -2 # means only show the last 2 entries

git log -p -2 # same as above but also show diff data.

git stat # shows info in a more summarised form.