Initialization

  • git init
  • Creates an empty Git repository in the given directory. If it doesn't exist, the directory will be created.
git init <project-name>
  • git clone
  • Clones a repository located at the given URL onto the local machine.
git clone <project-url>
  • git config
  • Gets or sets the options for the current repository, or globally depending on the command.
# set up the credentials on a global level, meaning that every commit from the current machine will use the given user
git config --global user.name "John"
git config --global user.email "john@john.com"

# omit the --global option to set the configuration for the current repository only
git config user.name "John"
git config user.email "john@john.com"

# get local user name
git config user.name

# get global user name
git config --global user.name

Files

  • git add
  • Updates the index using the current content found in the working tree, to prepare the content staged for the next commit. Depending on the options provided different actions will take place.
# adds the specified file to the index, where <file> is the file you want to add
git add <file>

# stages the specified files under the given directory, including files from the subdirectories
git add <directory>/\*.<extension>
git add pages/\*.html

# adds all new files to the index, ignoring modifications and deletions
git add .

# stages all changes in the working tree
git add -A
# or
git add --all

# stages modifications and deletions without adding new files found in the working tree
git add -u
# or
git add --update
  • git commit
  • Commits the staged changes.
# commits the changes with the given message
git commit -m "Super cool commit"

# replaces the last commit with the combination of the staged changes the changes from the last commit.
# if nothing is staged, this command can be used to update the last commit message.
git commit --amend -m "New super cool commit message"

# same as above except it doesn't change the commit message
git commit --amend --no-edit
  • git clean
  • Shows which files would be removed from the working directory or removes them, depending on the given option.
# show what would be removed
git clean -n

# do the cleaning forcefully
git clean -f

Branches

  • git checkout
  • Changes your current branch to the one provided.
# switches to the given branch
git checkout <branch-name>

# creates a new branch and then switches to it
git checkout -b <branch-name>

# creates a new branch if it doesn't exist, otherwise resets it and then switches to it
git checkout -B <branch-name>

# copies the old branch and creates a new one with the same state
git checkout -b <branch-name> <old-branch-name>
  • git push
  • It applies the local changes to the remote branch.
git push origin <branch-name>
  • git pull
  • It applies the remote changes to the local branch.
git pull
  • git fetch
  • Retrieves the changes and branches committed by other team members.
git fetch origin
  • git merge
  • Merges two or more branches together.
# merges the <branch-name> into the branch you are currently working on
git merge <branch-name>

# to merge into a specific branch use checkout first
git checkout <branch-to-merge-to>
git merge <branch-name>
  • git branch
  • Used to list, create, or delete branches
# list all branches
# current branch will be highlighted with an asterisk
git branch --list

# list all merged branches
git branch -a --merged

# delete a branch
git branch -d <branch-name>

# delete a branch forcefully
git branch -d --force <branch-name>
# a shorthand
git branch -D <branch-name>

Revisions

  • git show
  • Shows the information about the git repository. It can be used to show the information about commits, tags, trees, and blobs.
git show HEAD
  • git status
  • Shows the working tree current status. For example, if you want to see the currently staged files use this command.
# shows the current status
git status

# shows the current status in the short format
git status -s
  • git log
  • Shows the history of changes. The result includes data like commit id, date, and author.
# show the history
git log

# show the history by author
git log --author="Name"

# show the history by the commit message
git log --grep="Some Message"
  • git diff
  • Shows the difference between the working tree and the given branch.
git diff <branch-name>

Reset and Revert

  • git reset
  • Reset the current branch to the specified state.
# this will undo/rollback the last commit.
# the commit is lost after this
git reset --hard HEAD~1

# to keep the changes use the --soft option
git reset --soft HEAD~1
  • git revert
  • Reverts the existing commits.
# this will open the system editor and prompts you to edit the commit message prior to committing the revert
# this is the default option
git revert

# in this case, the revert command will not open the editor
git revert --no-edit

I hope you will find this list useful and help you clear out any doubts you might have. Personally, I use TortoiseGIT which I recommend.