GIT: Switching from code branch to dev/master

We recently upgraded to a latest version of Django (1.8). Earlier, we were running Django 1.2 and we created a new GIT branch called django18 that had all the migration changes. After going Live, we planned to stay on the django18 branch for a while so that if anything goes catastrophic, we can revert back to dev/master. But it had been a few months now and we didn’t revert back to dev and master. After I merged the django18 branch with dev, and then merged dev to master – here’s what I did to switch our dev and production server to run dev and master branches respectively. It wasn’t as easy as doing a checkout dev/master and just pulling code. With a lot of code and structural changes, I had to run through a lot of hoops to get this done right:

# Make sure the Django1.8 branch is up to date
git checkout task/django18
git status

# update dev branch
git checkout dev
git pull varun.verma dev

# update master branch
git checkout master
git pull varun.verma master

'''
error: The following untracked working tree files would be overwritten by merge:
	portal/__init__.pyc
	portal/api/__init__.pyc
	portal/api/admin.pyc
	portal/api/management/__init__.pyc
	portal/api/migrations/0001_initial.pyc
	portal/api/migrations/__init__.pyc
	portal/api/models.pyc
	portal/api/urls.pyc
	...
Aborting
'''

# View the the untracked files that will be cleaned. Review that all are .pyc files
git clean -fx --dry-run

# Now remove the untracked files
git clean -fx

# pull master branch again and it will complain about the statuc files as shown below
git pull varun.verma master

'''
From gitserver.domain.com:/var/git/your_repo
 * branch            master     -> FETCH_HEAD
error: The following untracked working tree files would be overwritten by merge:
	portal/static/admin/css/base.css
	portal/static/admin/css/changelists.css
	portal/static/admin/css/dashboard.css
	portal/static/admin/css/forms.css
	portal/static/admin/css/ie.css
	...
Please move or remove them before you can merge.
Aborting
'''

# View the untracked directories
git clean -dfx --dry-run
'''
Would remove portal/bi/management/
Would remove portal/another_app/management/
Would remove portal/static/admin/
'''

# Now get rid of the portal/static directory and build it again later
git clean -dfx

# pull master branch again and it will pull this time and list the additions/deletions/modifications
git pull varun.verma master

# make sure master local and origin are in sync
git status

'''
On branch master
Your branch is up-to-date with 'origin/master'.
'''

# now pull the static files
python manage.py collectstatic --settings= portal.settings_stage

# modify wsgi.py file to use the stage settings file
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "portal.settings_stage")

# restart apache
apache2ctl restart

# You may notice a list of .pyc files that now show as modified. You will have to checkout each of them individually
git checkout portal/__init__.pyc
...

Git – rename a file

Here’s how I just renamed a file under a git repository since I didn’t want to lose tracking changes by just deleting the old file and adding the new one to the repository. Although I use SmartGit but I had to do the renaming part on the terminal.

1. Rename the file using git command

$ git mv old_filename new_filename

2. Use git status to check the old and new file names.

$ git status
# On branch your-branch
# Changes to be committed:
#   (use "git reset HEAD ..." to unstage)
#
#     renamed: old_filename -> new_filename
#

3. Commit the file using SmartGit or from terminal using

$ git commit -m "Rename file"

4. Push changes to the remote repository from SmartGit or from terminal using

$ git push

Allowing multiple users to pull from a git repository on a shared server

A pretty common situation. Multiple developers working together on their local development machines and connecting to a central git repository with their own credentials. And there’s a dev/staging server where all users need to to have access to go login and pull the code. When you clone the git repository on the server, it essentially sets it up as the user who set it up and doesn’t allow other users to pull the code. Here’s how the config looks like:

[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
[remote "origin"]
        url = ssh://varun.verma@server.path.net/var/git/project_repo.git
        fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
        remote = origin
        merge = refs/heads/master
[branch "dev"]
        remote = origin
        merge = refs/heads/dev
[branch "task/upgrading_django"]
        remote = origin
        merge = refs/heads/task/upgrading_django

And the user can pull the repository using git pull

Now, in order to configure the repository so that all users who have access to the server are able to pull the code, here’s how the configuration should look like:

[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
[remote "origin"]
        url = nouser@server.path.net/var/git/project_repo.git
        fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
        remote = origin
        merge = refs/heads/master
[branch "dev"]
        remote = origin
        merge = refs/heads/dev
[branch "task/upgrading_django"]
        remote = origin
        merge = refs/heads/task/upgrading_django
[remote "varun.verma"]
        fetch = +refs/heads/*:refs/remotes/origin/*
        url = varun.verma@server.path.net:/var/git/project_repo.git
[remote "other.user"]
        fetch = +refs/heads/*:refs/remotes/origin/*
        url = other.user@server.path.net:/var/git/project_repo.git

And pull the repository by executing sudo git pull varun.verma task/upgrading_django. The system would ask for your local server password to sudo followed by the password for the remote git repository. This should resolve the problem.

Changing author name for all git logs

I recently realized I had goofed up with setting up my GIT account with an incorrect name. I found this cool way to update the logs and update the username to the correct one:

Note: This would update all commits so far. If you wanted to modify commits for specific user, then you may have to write a bash script for it. You can find more details here: http://stackoverflow.com/questions/750172/change-the-author-of-a-commit-in-git. But the below would work fine if it’s only yourself in the previous logs.
Just execute this command within your GIT home repository:

git filter-branch -f --env-filter "GIT_AUTHOR_NAME='varun.verma'; GIT_AUTHOR_EMAIL='varun.verma@accellion.com'; GIT_COMMITTER_NAME='varun.verma'; GIT_COMMITTER_EMAIL='varun.verma@accellion.com';" HEAD 

GIT – Merge dev to master

I generally like to merge master into the development first so that if there are any conflicts, I can resolve in the development branch itself and my master remains clean.

(on branch development)$ git merge master
(resolve any merge conflicts if there are any)
git checkout master
git merge development (there won't be any conflicts now)
git push

Ignore file types being tracked in GIT

I use SmartGIT for source control. I link the GIT repository directly to my source repository locally. As a result, the compiled source files also get tracked within GIT. e.g. the python compiled files (.pyc) also add up in the GIT tracker. In order to ignore file types being tracked within GIT, navigate to the local source repository linked to GIT. There must be a hidden directory named .git. Navigate to .git/info/ and look for a file called exclude (without any extensions). If the file doesn’t exist, create one. Add the file types that you would like GIT to ignore tracking. e.g. here’s the content of the file where I would like to exclude .pyc files from tracking

# git ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments.
# For a project mostly in C, the following would be a good set of
# exclude patterns (uncomment them if you want to use them):
# *.[oa]
# *~
*.pyc
*.project
*.pydevproject