I am never good at using Git.

But it’s not my fault. Git has too many commands, and I am not a command line guy. That’s why I usually use SmartGit, a Git software with user interface that supports Mac.

However, there are times when you have to run on the command line (eg. scripting or you just want to be real fast).

These are my common use cases during development work flow. I don’t cover everything, or advanced stuff.

Push an existing repos to Github

Assuming you have a repos on github already, issue the following commands:

cd existing_git_repo
git remote add origin [email protected]:[user]/[reponame].git
git push -u origin master

Setup Github to remember password

The instructions. Basically,

git config --global "Your Name Here"
git config --global "[email protected]"

Then download and setup git-credential-osxkeychain.

curl -s -O
chmod u+x git-credential-osxkeychain

Issue a which git and note the path of git. Assuming it is /usr/bin/git, you have have to move to /usr/bin/ (IMPORTANT: less the git part!).

sudo mv git-credential-osxkeychain /usr/bin/

On Color Options

One of the most important configuration

git config --global color.ui true

With that, commands like git diff and git log -p looks better

What’s changed

Sometimes, you want to see what are the changes in your working directory (compared to HEAD/the last commit).

git diff

Or you want to see what are the overall changes between the last 2 commits

git whatchanged -n 1

Then the actual code changes

 git log -p

Added new files, Updated changes, or Deleted files

If you have added new files or updated tracked files

git add .

If you have deleted files or updated tracked files

git add -u

You could do both in a single step

git add -A

What are my remotes/branches

To know,

cat .git/config

Or you can list the remotes

git remote -v

Change remote url

With the list of remotes, you can change to a new URL (http or ssh). Eg. Change origin remote to a ssh url:

git remote set-url origin [email protected]:USERNAME/REPOS.git

Update your repos

It’s a 2 step process. First you fetch the changes from a remote named origin

git fetch origin

Then you merge a branch master to local

git merge origin/master

Or Simply

git pull origin master

If origin is a default remote and ‘master’ is default branch, you can drop it eg. git pull.

Fix merge conflicts

This always happen when you work in teams. A very commmon question.

It usually goes like this.. You tried to update

git fetch
git pull
... not uptodate. Cannot merge.

So you commit your local changes

git add .
git commit -m "some changes"
Automatic merge failed; fix conflicts and then commit the result.

So you need to resolve the conflict

git mergetool -y

At this point you use the GUI (eg FileMerge) to resolve the conflicts and save. Then you commit

git add .
git commit -m "fixed conflicts"
git push


Revert a file

You can checkout a file to a particular commmit.

git checkout <sha1-commit-id> filename.c

In the case when you have a merge conflict, and you know it should just take your file,

git checkout --ours filename.c

Or if it should be their file,

git checkout --theirs filename.c

Revert a commit

Using revert is for the case where you want to roll back a commmit. It affects a commit, not files.

In many cases, we want to revert 1 file. To do that, read the previous section that uses checkout to a certain commit.

For example, to revert to the fourth last commit:

git revert -n HEAD~3

The -n is --no-commit, which is not to create a new commit with the reverted changes.


Useful when you want to tag a version.

git tag -a v1.2

If you want to push to the tag, it works similarly like a branch.

git push origin v1.2

Delete a commit that has been pushed

This is usually when you accidentally commit wrongly.

git log
git reset --hard <sha1-commit-id>
git push origin HEAD --force

Remove untracked files and directories

When you use git reset, you will still ended up with untracked files.

To remove them,

git clean -f -d

Play Save, Test First

For example, to test what a command like git clean will really do, add a -n:

git clean -f -d -n

Git will list the changes it could perform. secret tricks

Read this cheat sheet. You will learn shortcut keys and referencing issues easily.




Back to Home