When you’re new to git, it can get difficult to wrap your head around it at first. This is especially true if you’re migrating from subversion or when you’d rather spend more time writing your code than trying to learn git.
Git requires some learning, and you might as well spend the time doing it rather than wondering what just happened after running a command. Learn.github is a good place to start to learn the basics.
But even after spending some time learning git, there are going to be times that you’ve to painfully search the web/manual for basic stuff like working with remote branches or more complex stuff like handling a failed merge.
For the same reason, I’m creating this post as a reference point for myself to avoid repetitive searches. I’ll keep adding new commands here if and when I use them.
This is no way near to a complete reference for git and there might be better ways to do the things listed here. These are just the commands I use the most, in the manner I prefer to use them.
Commit and Stage all changes
git commit -am "log message"
Push Changes to remote repo
Pull Changes to local repo
Check current status of working tree
Discard local uncommitted changes to a file (in this case, abc.txt)
git checkout -- abc.txt
Branches (Branch name is assumed to be abc)
Working with local branches
Create a branch
git branch abc
Switch to a branch
git checkout abc
View all existing branches
Remove a branch
git branch -d abc
Merge a branch
# In this case, merging abc into mastergit checkout master
git merge abc
Working with remote branches
Push a local branch to remote repo
git push origin abc
Delete a remote branch
git push origin :abc
Setup a local branch to track a remote branch
git branch --track abc origin/abc
Create a local branch, push it to remote repo. and then pull changes from it
# Creating the new branch and switching to itgit checkout -b abc;
# Pushing the changesgit push
# Meanwhile someone else made changes to the remote version# Pulling those changesgit pull origin abc
# if you want to simply use 'git pull' to get the changes, do the followinggit config branch.abc.remote origin
git config branch.abc.merge refs/heads/abc