Lets keep the necessary git commands close at our fingertip for everyday work. This is a list of common git commands. Also tried to add few links which I think essential to know for every git beginner.
This is neither a cheatsheet nor a detailed documentation of git scm. Check useful links at the bottom for details references.
The Basic workflow
- Start a repository
- git init = Start a new repository in current directory
- git clone <repository-url> = Clone a repository
- Do some change
- Add/index the Changes
- git add . = Add all files of current directory
- git add <path/file.name> = Add specific file
- git add Documentation/\*.txt = Add files matching pattern
- git commit -am “<message>” = Commit all your local changes
- Check the status
- git status = Files changed in working directory
- git log = History of changes
- git log —-all —-graph —-oneline = Pretty one line log with encestry graph
- git log branch1..branch2 = Check what are not merged in 2 branches
- Get updated
- git fetch = Fetches latest changes from origin
- git pull = Fetches latest changes from origin and merge
- git push = Push changes to origin
- git push <origin> <branch> = Push changes to origin
Working With Branches
- Creating Branch
- git branch <branch-name>
- git branch = list your available branches
- git branch -a = list your available branches including remote branches
- git branch -v = see the last commit on each branch
- Switching to branch
- git checkout <branc-hname>
- git checkout -b <branch-name> = create and immediately switch to a branch
- Getting remote branch to local
- git fetch; git branch <new-branch-name> origin/<new-branch-name>
- Pushing local branch to remote
- git push -u origin <branch-name> (assuming you are on the new branch now)
- Merging branches
- git merge <branch-name> = <branch-name> will be merged into current branch
- Deleting branch
- git branch -d <branch-name> = delete a local branch
- git push <remote-name> :<branch-name> = delete a remote branch
Working with Remotes
- List of remotes
- git remote -v
- Adding remote
- git remote <name> <url>
- git remote assembla https://assembla/repo/path = #Example adding assembla repo as a new remote
- Update from/publish to a specific remote
- git fetch <remote-name> = Fetching from a specific remote
- git push <remote-name> <branch-name> = Fetching from a specific remote and branch
Fixing mistakes and conflicts
- Check canges/differences
- git diff = Changes to tracked files
- git diff $id1 $id2 = What changed between 2 commits
- Discard all modifications
- git checkout — <filename>
- Revert last (or specific) commit
- git revert HEAD
- git revert <commit-ref>
- Fix last commit
- git commit –amend [-m “updated message”]
- Unstage staged modifications of a file
- git reset HEAD <file>
- Resolving with our/theirs
- git checkout –ours <path/file>
- git checkout –theirs <path/file>
Some Git concepts and tricks
- How to ignore files/folders?
- What does mean updated/indexed/committed?
- Who changed what?
- What are tracking branches?
- Stash in few words. Believe me, you’re gonna need it!
- Be quicker with aliases
- Wanna mark a commit/version? Tag it!
- How to refer a specific commit version?
If you recommend any other command to be listed here, please let me know.
git log –oneline –graph
is not working for me. I am using git 1.7.12 (Apple Git-37) in OS X 10.8
Does the git log graph tool need some extension?
You are right Ron vai. –all was missing in that command. Correcting right now.
Thanks a lot bro 🙂