Basic commands of Git

Get Started. It's Free
or sign up with your email address
Basic commands of Git by Mind Map: Basic commands  of Git

1. A Gist containing some good rules link

2. With graph (useful for branches)

3. Repository

3.1. Undoing multiple commits by moving the HEAD Pointer. Rewinding the HEAD pointer back in time, so that it records from there going forward.

3.1.1. git reset The changes are still in the Staging Area and in the Working Directory git reset --soft [SHA-value] The changes are still only in the Working Directory git reset --mixed [SHA-value] The changes are not tracking and the Working Directory is clean git reset --hard [SHA-value]

3.2. Making a new commit which undoes all the changes from an existing one

3.2.1. git revert [SHA-value]

3.3. Amending commits

3.3.1. git commit --amend -m “[message]”

3.4. Retrieving old version from the current branch

3.4.1. git checkout [SHA-value] -- [file_name]

3.5. Sending the changes to the Remote Repository

3.5.1. git push

4. Staging Area

4.1. Viewing the changes

4.1.1. git diff --staged [file_name]

4.1.2. git diff --staged --color-words [file_name]

4.2. Unstaging changes or sending back to the WD

4.2.1. git reset HEAD [file_name]

4.3. Sending the changes to the Repository

4.3.1. git commit -m "[message]"

5. Working Directory

5.1. Viewing the changes

5.1.1. git diff [file_name]

5.1.2. git diff --color-words [file_name]

5.1.3. Showing the difference between the directory at some point in time and my current working directory git diff [SHA-value]

5.1.4. Getting a comparison between at the previous point in time and the working directory with a specific file. git diff [SHA-value] [file_name]

5.1.5. Getting a comparison between two different points in time git diff [SHA-value]..[SHA-value]

5.1.6. Getting a comparison between two different points in time with a specific file git diff [SHA-value]..[SHA-value] [file_name]

5.1.7. Getting a comparison with a tree-ish git diff [SHA-value]..[tree-ish]~[n]

5.1.8. Ignoring whether or not someone changed spaces git diff --stat --summary [SHA-value]..[tree-ish]~[n] git diff --ignore-space-change [criteria] git diff -b [criteria]

5.1.9. Ignoring all space, every single change that could be made to space git diff --ignore-all-space [criteria] git diff -w [criteria]

5.1.10. Comparing with the latest commit git diff --cached

5.2. Undoing changes from the current branch

5.2.1. git checkout -- [file_name]

5.3. Sending the changes to the Staging Area

5.3.1. git add [file_name]

5.3.2. git add .

5.4. Deleting file(s) and sending to the Staging Area

5.4.1. git rm [file_name]

5.5. Moving or renaming file(s) and sending to the Staging Area

5.5.1. git mv [file_name]

5.6. Sending the changes to the Repository (tracked files)

5.6.1. git commit -am "[message]"

5.7. Deleting untracked files

5.7.1. Testing run git clean -n

5.7.2. Removing files git clean -f

6. Initializing a Project

6.1. git init

7. Reporting difference between WD,SA and R

7.1. git status

8. Viewing commits

8.1. Displaying all commits

8.1.1. git log

8.2. With a limit of commits

8.2.1. git log -[n]

8.3. Searching commits by date

8.3.1. git log --since="[yyyy-mm-dd]"

8.3.2. git log --until="[yyyy-mm-dd]"

8.3.3. git log --since="2 weeks ago" --until="3 days ago"

8.3.4. git log --since=2.weeks --until=3.days

8.4. Searching commits by author

8.4.1. git log --author = "[name]"

8.5. Searching commits by commit message

8.5.1. git log --grep = "[message]"

8.6. Getting a compressed list in one line

8.6.1. git log --oneline

8.6.2. git log --oneline -[n]

8.6.3. git log [SHA-value]..[SHA-value] --oneline

8.7. Give the logs that affect to a particular file

8.7.1. git log [SHA-value].. [file_name]

8.8. Showing a diff of what changed in every file

8.8.1. git log -p

8.9. Getting the statics about the changes in each file

8.9.1. git log --stat --summary

8.10. Getting the one-line log with full SHA

8.10.1. git log --format=oneline

8.11. Getting the log information in email format

8.11.1. git log --format=email

8.12. Getting the log in raw format

8.12.1. git log --format=raw

8.13. Try this format

8.13.1. git log --oneline --graph --all --decorate

8.14. Showing the full SHA, author, date, the commit message and the diff of a commit

8.14.1. git log --format=graph

8.14.2. git show [SHA-value]

8.14.3. git show [tree-ish] e.g. HEAD

9. Ignoring files to track or files you don't want to add

9.1. Project directory level

9.1.1. nano .gitignore

9.2. User directory level

9.2.1. Linux / Unix nano /Users/[user_name_directory]/.gitignore_global git config --global core.excludesfile ~/.gitignore_global

9.2.2. Windows Use your preference text editor, create a file with .gitignore_global extension git config --global core.excludesfile ~/.gitignore_global

9.3. What files to ignore?

9.3.1. A collection of useful .gitignore templates rules link

9.4. Ignoring tracked files

9.4.1. It's going to remove from the Staging Area, not from Repository and my Working Directory either git rm --cached [file_name]

9.5. Tracking empty directories

9.5.1. touch [directory_path/directory_name_to_track]/.gitkeep

9.5.2. [directory_path/directory_name_to_track]> type nul > .gitkeep

10. Navigating the commit tree

10.1. What is a Tree-ish?

10.2. How Git can reference a commit?

10.2.1. full SHA-1 hash

10.2.2. short SHA-1 hash Small Project Current commit Parent commit Grand parent commit Great grand parent commit [n] parent commit Medium project Large project

10.2.3. HEAD Pointer Current commit git ls-tree HEAD Parent commit git ls-tree HEAD^ git ls-tree HEAD~1, git ls-tree HEAD~ Grand parent commit git ls-tree HEAD~2 Great grand parent commit git ls-tree HEAD~3 [n] parent commit git ls-tree HEAD~[n]

10.2.4. Branch reference Current commit git ls-tree master Parent commit git ls-tree master^ git ls-tree master~1, git ls-tree master~ Grand parent commit git ls-tree master~2 Great grand parent commit git ls-tree master~3 [n] parent commit git ls-tree master~[n]

10.2.5. Tag reference

11. Branching

11.1. Viewing all the branches in our local repository

11.1.1. git branch

11.2. Creating a new branch

11.2.1. git branch [branch_name]

11.3. Where the HEAD pointer points to?

11.3.1. cat .git/HEAD Result: ref: refs/heads/[current_branch_name] Checking the heads directory Take a look what's in the [current_branch_name] branch

11.4. Switching branches

11.4.1. git checkout [new_branch_name] Examples for naming git branches

11.4.2. Creating a new branch and switch at the same time git checkout -b [branch_name] git checkout -b [branch_name] [alias_for_remote_repository]/[remote_branch_name]

11.5. Comparing branches

11.5.1. git diff [branch_name]..[branch_name]

11.5.2. git diff --color-words [branch_name]..[]

11.6. Renaming branches

11.6.1. git branch -m [old_branch_name] [new_branch_name]

11.7. Deleting branches

11.7.1. git branch -d [branch_name_to_delete]

11.7.2. git branch -D [branch_name_to_delete]

11.8. Configuring the command prompt to show the branch

11.8.1. Windows Create a new file on NotePad and save it as .bash_profile: export PS1='Your-Name[\W]$(__git_ps1 "(%s)"): ' In order to run that file: source ~/.bash_profile

11.8.2. Unix Download the file from Command Line: curl -o ~/ \ Edit the .bash_profile and ADD the following code: if [ -f ~/ ]; then source ~/ export PS1='Your-Name[\W]$(__git_ps1 "(%s)"): ' fi

11.9. Showing all branches that are completely included into the checkout (current) branch that we are on

11.9.1. git branch --merged

11.10. Merging code

11.10.1. git merge [branch_name] Fast-Forward git merge --no-ff [branch_name] git merge --ff-only [branch_name] True merge or Non-Fast-Forward merge

11.10.2. Merging conflicts Resolving merge conflicts Abort the merge Resolve the conflicts manually Merge tool

12. Saving changes in the stash

12.1. git stash save "[message]"

12.2. Viewing stashed changes

12.2.1. git stash list

12.2.2. git stash show [email protected]{n}

12.2.3. git stash show -p [email protected]{n}

12.3. Retrieving stashed changes

12.3.1. git stash pop [email protected]{n}

12.3.2. git stash apply [email protected]{n}

12.4. Deleting stashed changes

12.4.1. git stash drop [email protected]{n}

12.4.2. git stash clear

13. Remote Repository

13.1. Listing all remote repositories

13.1.1. git remote

13.1.2. git remote -v

13.1.3. cat .git/config

13.1.4. ls -la .git/refs/remotes ls -la .git/refs/remotes/[alias] cat .git/refs/remotes/[alias]/master

13.1.5. git branch -r

13.1.6. git branch -a

13.2. Adding a remote

13.2.1. git remote add [alias_remote_repo] [URL]

13.3. Removing a remote

13.3.1. git remote rm [alias]

13.4. Creating a remote branch

13.4.1. git push -u [alias] [branch_name]

13.5. Creating non tracking remote branch

13.5.1. git push [alias] [branch_name]

13.6. Cloning from remote server repository

13.6.1. git clone [URL] <alias>

13.7. Viewing changes

13.7.1. git diff [alias]/[branch_name]..[branch_name]

13.8. Converting remote branch from non remote branch

13.8.1. git config branch.[non_tracking_branch_name].remote [alias]

13.8.2. git config branch.[non_tracking_branch_name].merge refs/heads/master

13.8.3. git branch --set-upstream [non-tracking_branch_name] [alias]/[non-tracking_branch_name]

13.9. Pushing changes to the remote repository

13.9.1. git push

13.10. Synchronizing remote repo to local repo

13.10.1. git fetch

13.11. Merging changes

13.11.1. git merge [alias]/[branch_name]

13.12. Synchronizing and merging in one step

13.12.1. git pull

13.13. Checking out remote branches

13.13.1. git branch [new_branch_name] [alias]/[remote_branch_name]

13.14. Deleting remote branch

13.14.1. git push [alias_remote_repo_name] :[remote_branch_name]

13.14.2. git push [alias_remote_repo_name] --delete [remote_branch_name]

14. Setting up aliases

14.1. git config --global alias.[alias] “[git_command]”