git checkout -b –no-track

Ever want to checkout a new git branch from another branch without setting up tracking?

Here is the longhand way:

git checkout old-branch
git branch new-branch
git checkout new-branch

But there is a quicker way:

git checkout -b new-branch old-branch

… which does the same thing, albiet in one command.

How to checkout and track a remote git branch

One of those really handy things to remember…  When git “tracks” a branch, it basically sets up an entry in .git/config which tells git what to do with push and pull.  For example:

I had a remote branch called Task/Round3.3.

I wanted to work on it locally, but have push and pull work right.

So I ran this:

git checkout -b Task/Round3.3 --track origin/Task/Round3.3

To which git said:

Branch Task/Round3.3 set up to track remote branch refs/remotes/origin/Task/Round3.3.
Switched to a new branch "Task/Round3.3"

And in .git/config, these lines were added:

http://Task/Round3.3
remote = origin
merge = refs/heads/Task/Round3.3

Now, when I checkout Task/Round3.3, I am able to say `git pull` and `git push`, and it will do the “right thing”…

git add -u

Here is a nice little tidbit I ran across some time ago…  Ever delete a bunch of files from a git working copy, and then had to go in and tell git that you meant to delete them?  For example:

[jason@dc40 AppStruct]$ git status
# On branch master
# Changed but not updated:
#   (use "git add/rm ..." to update what will be committed)
#
#       deleted:    Python/AppStruct/Application.py
#       modified:   Python/AppStruct/Database/PostgreSQL.py
#       deleted:    Python/AppStruct/Date.py
#       deleted:    Python/AppStruct/JSON.py
#       deleted:    Python/AppStruct/Util.py
#       deleted:    Python/AppStruct/__init__.py
#
# Untracked files:
#   (use "git add ..." to include in what will be committed)
#
#       Python/AppStruct/NewFile.txt
no changes added to commit (use "git add" and/or "git commit -a")

Previously, the way to handle this would be:

   git rm ...
   git rm ...
   git rm ...
   git rm ...
   git rm ...
   git add ...
   git add ...

Rather, isn’t this easier?

   git add -u
   git add .

From the man page for git-add:

–update | -u
Only match against already tracked files in the index rather than the working tree. That means that it will never stage new files, but that it will stage modified new contents of tracked files and that it will remove files from the index if the corresponding files in the working tree have been removed.