Git notes (2): Checking out a branch

Continuing from other notes here.

To checkout a branch:

Clone a repo:

git clone user@server:reponame.git

Fetch all branches:

git fetch remote_name

List all available branches:

git branch -v -a

Checkout required branch:

git checkout -b branch_name remote_name/branch_name

Fetch all changes on branch:

git fetch

 

Committing file deletes to Git

My normal git usage pattern is:

git add .
git commit -m "commit message"

and then when needed:

git push remotename master

The trouble with ‘git add .’ is that is doesn’t stage any deleted files for committing. You can see this if you do a ‘git status’ and you’ll see it list deleted files, but listed as not staged:

$git status
# On branch master
# Changes not staged for commit:
#  (use "git add/rm " to update what will be committed)
# deleted: ...

You’ll notice the comment mentions to use ‘git rm’ to delete files from the repo, but if you’ve deleted files locally, you can either do a ‘git rm filename’ for each of the locally deleted files, or more simply, do this to pick up all changed files for staging, including deletes:

git add -A

Removing commit history in Git

To avoid committing files to version control you can add their names to a .gitignore file, and to committing local changes to an already version controlled file you can use the git command described in my previous post here.

How can you remove history if you’ve already committed a file? For example, let’s say you have a properties file with some environment specific values that you’ve committed, but you want to remove the history of those committed values?

This FAQ walks through the steps needed, which in summary are:

    • Remove all history from the repo:
git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch path/to/your_file_name' 
  --prune-empty --tag-name-filter cat -- --all
  • on Windows, replace the ‘ with “
  • add the filename to .gitignore if you want to avoid future commits (and commit .gitignore: git commit -m “updated gitignore” )
  • push changes back to your remotes: git push remotename master –force

If you need to make local changes to a tracked file and then not commit them in future commit, use the ‘git update-index -skip-worktree’ as described here.

Skipping locally updated files with Git

If you have a local file that you’ve updated locally (like a properties file) but you want to avoid committing the changes when you next commit, use this command:

git update-index --skip-worktree path/to/filename

If you need to add it back to the list of tracked files, then use this:

git update-index –no-skip-worktree path/to/filename