Git cheat sheet
Some definitions
Git is a version-control tool for tracking changes in source code during software development.
A commit records a snapshot of your changes and associate it with a message and a commit id.
A branch is a separate version of the main repository that allows the developper to test a new feature for example without affecting the main repository.
Staging means adding a file to the Git index before commiting it.
Remote repositories are versions of your source code that are hosted on the Internet, mainly to share the code or collaborate with others. Examples of Git-based source code repository hosting service include Github, Bitbucket or Gitlab.
You can download a printable Git command line cheat sheet here (Source).
Create a repository
- create a new local repository
git init my_project_name
- Download from an existing repository
git clone my_url
Observe the repository
- list files not yet commited
git status
- Show full change history
git log
- Show change history for file/directory including diffs
git log -p [file/directory]
- Show the changes to files not yet staged
git diff
- Show the changes between two commit ids
git diff commit1 commit2
Working with branches
- List all local branches
git branch
- Create a new branch
git branch new_branch
- Switch to a branch and update working directory
git checkout my_branch
- Delete a branch
git branch -d my_branch
- Merge branch_a into branch_b
git checkout branch_b
git merge branch_a
Make a change
- Stage all changed files in current directory
git add .
- Commit all your tracked files to versioned history
git commit -am "commit message"
- Unstages file, keeping the file changes
git reset [file]
- Revert everything to the last commit
git reset --hard
Synchronize with remote repository
- Get the latest changes from origin (no merge)
git fetch
- Fetch the latest changes from origin and merge
git pull
- Push local changes to the origin
git push
Leave a comment