Git for me is THE best and most powerful source control system around. I can’t imagine developing without it anymore such is the flexibility and control it gives me over all other source control systems I’ve used. For my own reference purposes and as a quick goto/reminder list, here is a bunch of my most common Git commands that I use on a daily basis. Of course, there are so many git commands that I probably haven’t even used most of them but these are what currently work for me.
git status
Simply shows the current state of affairs of the repository. What’s being tracked, what isn’t, etc.
git remote add origin "<path/to/remote/repo>"
Add a remote location to push to/pull from. This is set automatically when cloning
git log --pretty=oneline
Simple, succinct log
git checkout -b mynewbranch
Creates a new branch “mynewbranch” and checks it out at the same time
git branch -d mynewbranch
Delete “mynewbranch” locally
git push :mynewbranch
If you pushed the branch then this deletes it on the remote
git pull --rebase
Grabs latest changes from origin, rewinds your changes, then plays them back over the top of the pulled changes. I use this over git pull, git merge, and git fetch all the time as I think the history looks better/cleaner in gitk and I seem to get fewer conflicts this way
git add -A
Adds all files to the staging area, both updated and new files, as well as those marked for delete
git rm --cached <file>
Removes file from staging after the result of a git add command
git diff --cached
Shows the differences between original and changed files after adding them to the staging area
git commit -m "commit message"
Commits the staged changes
git commit --amend
Adds currently staged changes to the previous commit. Useful for when you should have added them previously but forgot!
git reset --soft HEAD^
Undoes the last commit putting the files back in the staging area. Keep re-issuing to undo one commit at a time. Useful if you forget to add a file or hunk as long as you haven’t already pushed to other users.
git clean
Removes untracked files from working directory. Use the -n (–dryrun) option to see what will be affected and -f to make it happen
git reflog
Shows every action taken on the repository and even contains any commits you may have lost and want to retrieve (as long as you haven’t done a garbage collection). Useful for getting back into a good state to undo the damage you did with a previous command!
git gui
Visual tool to help see what’s staged, what’s not, and an easier way to stage hunks rather than whole files
gitk --all
Visual tool to see all the changes in the repository. The –all flag means include all branches
There’s obviously a whole lot more but these get me by for the most part. I’ll keep adding more as and when they creep into my daily usage but for now this is what keeps me productive.