Posts in Category: git

Git – Handy git tools and utilities

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

https://rtyley.github.io/bfg-repo-cleaner/

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

https://github.com/arc90/git-sweep


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://github.com/in/projectname.git
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

http://stackoverflow.com/questions/292357/what-are-the-differences-between-git-pull-and-git-fetch


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

http://git-scm.com/blog/2011/07/11/reset.html


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 – File states

Each of the files in the working directory are in one of two high-level states:

  1. tracked files – These are files that were in the last snapshot (aka); they can be in one of the following sub states:
    1. unmodified – These are any files that haven’t been modified since the last commit. They will still be included in the next commit, but remain as is.
    2. modified – These are files that have been modified since the last commit (we probably modified these as part of bug fixes). These files will be included in the next commit, but will be included in thier respective new form.
    3. staged – These are files that are either not present in the last commit (e.g. newly created files) or are “modified” files that we tell git to include

Git – The .gitignore

There are some untracked files in your project file that you want git to completely ignore, e.g. temporary files, log files, etc. You can do this by creating a special new file called .gitignore which sits alongside the .git folder. this file can list everything you want ignored, here’s an example of one:

[vagrant@localhost puppetlabs-ntp]$ ls -la | grep git
drwxrwxr-x 8 vagrant vagrant  4096 Mar  9 15:46 .git
-rw-rw-r-- 1 vagrant vagrant    83 Mar  9 14:02 .gitignore
[vagrant@localhost puppetlabs-ntp]$ cat .gitignore
pkg/
Gemfile.lock
vendor/
spec/fixtures/
.vagrant/
.bundle/
coverage/
.idea/
*.iml
[vagrant@localhost puppetlabs-ntp]$

Tip: if you are using eclipse, then you may want to add “.project” in the above file too.