›INDEX
Last Updated:

Git

A more detailed guide: Git scm docs. This document contains commonly used and useful git commands.

Suggestion: Use the index on the left of the page to navigate to what you're concerned with.
If you have a suggestion to improve this page, leave a message: Contact page

Basics

Initialize a git repo

In the directory you wish to start versioning:

git init

Check the status

git status

Stages in git

These are the stages that a file can have in git.

  • Untracked: A file that hasn't been marked to be either tracker or ignored by git. You can track files by using add.
  • Modified: These files are being tracked by git and have changed with respect to either the staging area or the last commit.
  • Staged: (Changes to be committed) These are files that have been added to the staging area before they get committed.
Untracked -- git add --> Staged -- git commit --> clean
Modified -- git add --> Staged -- git commit --> clean

Commit

Once you have files you can add them to the staging area ( a place where files are stored before they are committed ). This is a fluid area where changes are stored temporarily. Once you commit a set of changes it's permanently stored in git unless explicitly deleted.

To add files to the staging area:

git add <file_name>
git add <directory>         # Adds all files in directory.
git add .         # Add all files in current directory.
git add -A     # Add all changes and files created.

Remove a file that is being tracked:

git rm -r <filename>

Delete changes made:

git restore <filename>

Remove file from staging area:

git restore --staged <filename>

Commit changes into git:

git commit -m "Commit Message"
git commit          # Opens an editor for the commit message

Commit messages are usually descriptions of what was changed in that commit. Each created commit has a UUID (id that is unique to each commit).

Change last commit message

git commit --amend -m "New message"

Logging

  • git log - A simple log of current branch.
  • git log --all - A log of all commits.
  • git log --author <name> - Filter by author.
  • git log --committer <name> - Filter by committer.
  • git log --after <date> --before <date> - Filter by date ( can use 2.days.ago for dynamic dates ).
  • git log -p - View diff of each commit.
  • git log --stat - View status of each commit (lines added, remove, etc).
  • git log --oneline - Each commit on one line.
  • git log --graph - Show commits as a graph.
  • git log --merges - Show only merge commits.
  • git log –S”<string>” - Search for string within files changed by commit.
  • git log --grep=”<string>” - Search for string within commit message.
  • git log -- <file 1> <file 2> - Search for commits by files changed.

Useful graph: - git log --graph --oneline --all - Shows all commits as a tree ( Useful for small projects )

Branching

  • git branch - List branches.
  • git branch -a - List all branches (local and remote).
  • git branch <branch name> - Create a new branch.
  • git branch -d <branch name> - Delete a branch.
  • git branch -D <branch name> - Force delete a branch.

Checkout

  • git checkout <branch name> - Checkout a branch.
  • git checkout - - Checkout the last checked out branch.
  • git checkout -b <branch name> - Create the branch and checkout.
  • git checkout -b <branch name> origin/<branch name> - Clone a remote branch.
  • git checkout <commit> - Note that branches are just commits and so are things like HEAD~2.
  • git checkout <commit> -- <file> - Checkout the state of a file from a particular commit.

Merging

You can merge different branches into each other. This combines the changes between the 2 branches.

First checkout the branch you want to merge into. Then

git merge <branch-name>

Git will try and automatically merge the 2 branches but if there are conflicting changes then you will have to manually resolve it and then commit a merge commit.

Stashing

Stash changes in the current working directory when trying to change branches or doing other work.

  • git stash - Stash changes in the current working directory.
  • git stash -m "Stash message" - Stash with a message.
  • git stash clear - Clear all stashes.
  • git stash list - List all stashes.
  • git stash show <stash> - Show the changes in stash.
  • git stash drop <stash> - Delete a stash.
  • git stash pop - Apply last stash and delete it.
  • git stash pop <stash> - Apply stash and delete it.
  • git stash apply - Apply last stash.
  • git stash apply <stash> - Apply stash.

Further reading: Git scm docs - Stash

Diff

  • git diff - Shows diff between CWD and last commit. (not staged changes).
  • git diff --color-words - Colors words in diff rather than different lines.
  • git diff <commit 1> <commit 2> - Difference between commit 1 and 2 (with 1 as the base). (if commit 2 is ignored it's taken to be HEAD).

Note: Branches and things like HEAD are just pointers to commits.

Reset

Form: git reset [<mode>] [<commit>] Flags: - --soft - All changes are kept in the current working tree. - --hard - All changes are discarded and it's a hard reset. - --mixed - Resets the index but not the working tree (default).

Examples:

  • git reset HEAD <file> - Unstage a file.
  • git reset --hard HEAD - Undo all changes made since last commit.
  • git reset --soft HEAD~2 - Undo last 2 commits and save changes made by both commits into current working tree.

Working with Remotes

Further reading: Git remote docs

Add remotes to an existing repo

git remote add <name> <url> Add a remote under a name. Usually looks something like: git remote add origin https://test@github.com/test/example.git.

  • git remote - List remotes. (can use -v for verbose).
  • git clone <remote url> - Clone a remote repo.
  • git pull <remote> <branch> - Pull changes in remote repo. (if remote and branch are missing the value set as upsteam is defaulted to.)
  • git push <remote> <branch> - Push changes to remote repo.
  • git pull --set-upstream <remote> <branch> - Pull the changes and set the remote.
  • git reset --hard <remote>/<branch> - Hard reset the current branch to the remote branch.

Rebase

This is usually used when you want to make merging easier. If you branch is based on commit 1 from master and now master is on commit 5 you can rebase your branch to change the base of your branch to a different commit - in this case, commit 5. This will make it easier to merge.

git rebase master - Rebase current branch with master. git pull --rebase origin master - Rebase current branch with origin master.

Enjoy the notes on this website? Consider supporting me in this adventure in you preferred way: Support me.