Git Cheat Sheet

1. Git configuration

  • Git config
    Get and set configuration variables that control all facets of how Git looks and operates.
    Set the name:
    $ git config –global “User name”[/code]
    Set the email:
    $ git config –global “”[/code]
    Set the default editor:
    $ git config –global core.editor Vim[/code]
    Check the setting:
    $ git config -list[/code]
  • Git alias
    Set up an alias for each command:
    $ git config –global checkout[/code]
    $ git config –global branch[/code]
    $ git config –global commit[/code]
    $ git config –global status[/code]

2. Starting a project

  • Git init
    Create a local repository:
    $ git init[/code]
  • Git clone
    Make a local copy of the server repository.
    $ git clone[/code]

3. Local changes

  • Git add
    Add a file to staging (Index) area:
    $ git add Filename[/code]
    Add all files of a repo to staging (Index) area:
    $ git add*[/code]
  • Git commit
    Record or snapshots the file permanently in the version history with a message.
    $ git commit -m ” Commit Message”[/code]

4. Track changes

  • Git diff
    Track the changes that have not been staged:
    $ git diff[/code]
    Track the changes that have staged but not committed:
    $ git diff –staged[/code]
    Track the changes after committing a file:
    $ git diff HEAD[/code]
    Track the changes between two commits:
    $ git diff Git Diff Branches:[/code]
    $ git diff < branch 2>[/code]
  • Git status
    Display the state of the working directory and the staging area.
    $ git status[/code]
  • Git show Shows objects:
    $ git show[/code]

5. Commit History

  • Git log
    Display the most recent commits and the status of the head:
    $ git log[/code]
    Display the output as one commit per line:
    $ git log -oneline[/code]
    Displays the files that have been modified:
    $ git log -stat[/code]
    Display the modified files with location:
    $ git log -p[/code]
  • Git blame
    Display the modification on each line of a file:
    $ git blame <file name>[/code]

6. Ignoring files

  • .gitignore
    Specify intentionally untracked files that Git should ignore. Create .gitignore:
    $ touch .gitignore List the ignored files:[/code]
    $ git ls-files -i –exclude-standard[/code]

7. Branching

  • Git branch Create branch:
    $ git branch [/code]
    List Branch:
    $ git branch –list [/code]
    Delete a Branch:
    $ git branch -d [/code]
    Delete a remote Branch:
    $ git push origin -delete [/code]
    Rename Branch:
    $ git branch -m[/code]
  • Git checkout
    Switch between branches in a repository.
    Switch to a particular branch:
    $ git checkout[/code]
    Create a new branch and switch to it:
    $ git checkout -b [/code]
    Checkout a Remote branch:
    $ git checkout[/code]
  • Git stash
    Switch branches without committing the current branch. Stash current work:
    $ git stash[/code]
    Saving stashes with a message:
    $ git stash save “”[/code]
    Check the stored stashes:
    $ git stash list[/code]
    Re-apply the changes that you just stashed:
    $ git stash apply[/code]
    Track the stashes and their changes:
    $ git stash show[/code]
    Re-apply the previous commits:
    $ git stash pop[/code]
    Delete a most recent stash from the queue:
    $ git stash drop[/code]
    Delete all the available stashes at once:
    $ git stash clear[/code]
    Stash work on a separate branch:
    $ git stash branch[/code]
  • Git cherry pic
    Apply the changes introduced by some existing commit:
    $ git cherry-pick[/code]

8. Merging

  • Git merge
    Merge the branches:
    $ git merge[/code]
    Merge the specified commit to currently active branch:
    $ git merge[/code]
  • Git rebase
    Apply a sequence of commits from distinct branches into a final commit.
    $ git rebase[/code]
    Continue the rebasing process:
    $ git rebase -continue[/code]
    Abort the rebasing process:
    $ git rebase –skip[/code]
  • Git interactive rebase
    Allow various operations like edit, rewrite, reorder, and more on existing commits.
    $ git rebase -i[/code]

9. Remote

  • Git remote
    Check the configuration of the remote server:
    $ git remote -v[/code]
    Add a remote for the repository:
    $ git remote add [/code]
    Fetch the data from the remote server:
    $ git fetch[/code]
    Remove a remote connection from the repository:
    $ git remote rm[/code]
    Rename remote server:
    $ git remote rename[/code]
    Show additional information about a particular remote:
    $ git remote show[/code]
    Change remote:
    $ git remote set-url[/code]
  • Git origin master
    Push data to the remote server:
    $ git push origin master [/code]
    Pull data from remote server:
    $ git pull origin master[/code]

10. Pushing Updates

  • Git push
    Transfer the commits from your local repository to a remote server. Push data to the remote server:
    $ git push origin master [/code]
    Force push data:
    $ git push -f[/code]
    Delete a remote branch by push command:
    $ git push origin -delete edited[/code]

11. Pulling updates

  • Git pull
    Pull the data from the server:
    $ git pull origin master[/code]
    Pull a remote branch:
    $ git pull[/code]
  • Git fetch
    Download branches and tags from one or more repositories. Fetch the remote repository:
    $ git fetch< repository Url> [/code]
    Fetch a specific branch:
    $ git fetch[/code]
    Fetch all the branches simultaneously:
    $ git fetch -all[/code]
    Synchronize the local repository:
    $ git fetch origin[/code]

12. Undo changes

  • Git revert
    Undo the changes:
    $ git revert[/code]
    Revert a particular commit:
    $ git revert[/code]
  • Git reset
    Reset the changes:
    $ git reset -hard[/code]
    $ git reset -soft:[/code]
    $ git reset –mixed[/code]

13. Removing files

  • Git rm
    Remove the files from the working tree and from the index:
    $ git rm <file Name>[/code]
    Remove files from the Git But keep the files in your local repository:
    $ git rm –cached[/code]