Git add -A and git commit one liner

When working with Git, I often find myself typing:


git add -A && git commit -m "My commit message"

This stages any changes including new files (which "git commit -am" doesn’t), marks any files that have been deleted from the working copy as removed and commits to the repository.

Following a quick Google search, and thanks to this handy git alias article, I created a “ca” (commit all) alias using:


git config --global alias.ca '!git add -A && git commit'

Now committing all changes is as simple as:


git ca -m "My commit message"

This is a good solution, however I prefer adding the following function to my ~/.profile file:


function gca {
  git add -A && git commit -m "$1"
}

Which gets us to:


gca "My commit message"

Pulling changes into a Github fork from the original repository

If you maintain your own ongoing fork of a project on Github, you will inevitably want to pull in changes from the originator’s repository. Here’s how I usually go about it.

First add the other guy’s repository to your list of remotes:


cd my-fork
git remote add other-guy https://github.com/other-guy/other-guys-repo.git 

If you were to then list your remotes, you would have something like:


origin
other-guy 

Now it’s just a case of pulling from the relevant branch on their repo, in this case ‘master':


git pull other-guy master

Hope that saves someone some time.

Completely Flattening a Git Repository

Whilst working on a recent project, I ended up with a massive git repository due to frequent commits of large-ish files. As I was the only one working on the project and I was certain I didn’t need anything from the history, I wanted to be able to flatten all the commits in the repository.

I didn’t find exactly what I was after but found a handy tip on Stack Overflow. In essence you re-initialize your local repository and force a push to your remote. A word of warning, if you have anyone else pulling or pushing to the remote repository, they will hate you for this as it will completely screw up any revision history or branches they may be working on.

Here’s my modified version of the tip on Stack Overflow to take into account .gitignore and removing the existing .git repository:


$ cd {{project-directory}}
$ rm -rf .git
$ git init
$ git add .
$ git commit -m 'Initial commit'
$ git remote add origin {{url}}
$ git push --force

p.s. Don’t say I didn’t warn you!

Git – setting up a remote repository and doing an initial push

There is a great deal of documentation and many posts on Git out there, so this is more of a note to self as I keep forgetting the steps needed to set up a remote repository and doing an initial “push”.

So, firstly setup the remote repository:


ssh git@example.com
mkdir my_project.git
cd my_project.git
git init --bare
git update-server-info # If planning to serve via HTTP
exit

On local machine:


cd my_project
git init
git add *
git commit -m "My initial commit message"
git remote add origin git@example.com:my_project.git
git push -u origin master

Done!

Team members can now clone and track the remote repository using the following:


git clone git@example.com:my_project.git
cd my_project

Bonus

To have your terminal prompt display what branch you are currently on in green, add the following to your ~/.bash_profile (I have my current directory displayed in cyan):


function git-branch-name {
  git symbolic-ref HEAD 2>/dev/null | cut -d"/" -f 3
}
function git-branch-prompt {
  local branch=`git-branch-name`
  if [ $branch ]; then printf " [%s]" $branch; fi
}
PS1="\u@\h \[\033[0;36m\]\W\[\033[0m\]\[\033[0;32m\]\$(git-branch-prompt)\[\033[0m\] \$ "