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 firstname.lastname@example.org:[user]/[reponame].git git push -u origin master
Setup Github to remember password
The instructions. Basically,
git config --global user.name "Your Name Here" git config --global user.email "email@example.com"
Then download and setup
curl -s -O http://github-media-downloads.s3.amazonaws.com/osx/git-credential-osxkeychain chmod u+x git-credential-osxkeychain
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
Sometimes, you want to see what are the changes in your working directory (compared to HEAD/the last commit).
git diff somefile.py
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 somefile.py
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
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 firstname.lastname@example.org:USERNAME/REPOS.git
Update your repos
It’s a 2 step process. First you fetch the changes from a remote named
git fetch origin
Then you merge a branch
master to local
git merge origin/master
git pull origin master
origin is a default remote and ‘master’ is default branch, you can drop it eg.
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
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
--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
git clean -f -d -n
Git will list the changes it could perform.
Github.com secret tricks
Read this cheat sheet. You will learn shortcut keys and referencing issues easily.