Used initial phase | Used on daily basis | Used rarely(Advanced) |
---|---|---|
git init | git add | git clean |
git config | git commit | git blame |
git remote | git checkout | git show |
git push | git rm | |
git pull | git revert | |
git log | git reset | |
git status | git rebase | |
git stash | git merge |
- Files to be concerned:
- Commands
- Common Scenarios
- Scenario: Revert one or more specific files from recent commit to it's previous or specific state
- Scenario: Revert the recent commit to it's previous state as things broke
- Scenario: Find out author and commit details for specific block of code
- Scenario: Squash multiple commits related to PEP8 changes into one
- Scenario: Permanently remove local commits using git reset with (--mixed, --soft, --hard)
- .gitignore
- .git\config
Convert a directory into a git repository
Configure Git command and repository settings
- git config --list
- git config --global user.name "Suresh K L"
- git config --global user.email "Suresh K L"
--global use global config file # C:\Users\skoolanx\.gitconfig
--system use system config file # C:\Program Files\Git\mingw64\etc\gitconfig
--local use repository config file # .git\config
git config --global alias.co checkout git config --global alias.br branch git config --global alias.ci commit git config --global alias.st status
Add remote git repository URLs for fetch/push git remote -v
Add the contents to track
Save the changes with meaningful message as a checkpoint
git commit --amend --reset-author
Push local change to remote tracking branch
Pull the updates from remote tracking branch
Investigate git history
Find out modifications by author name
Show the working tree status
Save changes temporarily to keep the working tree clean
Show the changes happened to a commit
- Switch b/w branches
- Chekcout a commit to try out
- Revert commited changes by creating a new revert commit
- To be used when commits are published to remote
- Reset the uncommited changes
- Reset the history by moving the HEAD
- *Recommended to use only when commits are local
- It cannot be used to reset commit anywhere.
- It works like a paper roll. A paper can be rolled only from the end and not anywhere in between
> git reset HEAD file4.txt
> git reset HEAD~3 file4.txt # Reset changes made to file by 3 commits from where HEAD is now
- --mixed(default): Resets the index but not the working tree
- --soft: Does not reset index or working tree. But resets the HEAD to <commit>
- --hard: Resets index and working tree
- Get all the commits from a branch to working branch
- Replay all the new commits in current branch on top of
- Merge all the commits from one branch to another
- Creates a new commit as Merge commit
- Cleanup the working directory
- Find out who and when code was modified
> git blame file4.txt
657b9ad2 (Test User 2019-03-15 19:21:32 +0530 1) "file4.txt"
00000000 (Not Committed Yet 2019-03-15 19:54:41 +0530 2) Something different
00000000 (Not Committed Yet 2019-03-15 19:54:41 +0530 3) "file4.txt"
00000000 (Not Committed Yet 2019-03-15 19:54:41 +0530 4) "file4.txt"
> git show 657b9ad2 --name-only
commit 657b9ad2e0b90dd5495b141bc446396e90fc5aae
Author: Test User <TestUser@domain.com>
Date: Fri Mar 15 19:21:32 2019 +0530
Added file4.txt
file4.txt
- Create a new file and commit changes
echo New > file.txt
git add file.txt
git commit -m "Commit 1"
- Modify the newly added file with some more new changes
echo Additional changes >> file.txt
git add file.txt
git commit -m "Commit 2"
- Reset changes made in step 2 to step 1 with a new commit
git checkout HEAD~1 -- file.txt
git add file.txt
git commit -m "Commit 3"
echo First modification > file.txt
git add file.txt
git commit -m "Commit 1"
echo Second modification >> file.txt
git add file.txt
git commit -m "Commit 2"
echo Third modification >> file.txt
git add file.txt
git commit -m "Commit 3"
git revert HEAD --no-commit
git commit -m "Commit4: Revert Commit 3 to Commit 2"
echo Line 1 > file.txt
git add file.txt
git commit -m "Commit 1"
echo Line 2 >> file.txt
echo Line 3 >> file.txt
echo Line 4 >> file.txt
echo Line 5 >> file.txt
git add file.txt
git commit -m "Commit 2"
echo Line 6 >> file.txt
echo Line 7 >> file.txt
git add file.txt
git commit -m "Commit 3"
git blame --color-lines -L 1,5 file.txt
echo list = [1,2,3,] > file.py
git add file.py
git commit -m "Commit 1: Created list of numbers"
echo list = [1, 2, 3,] > file.py
git add file.py
git commit -m "Commit 2: Added space around operators"
echo numbers = [1, 2, 3,] > file.py
git add file.py
git commit -m "Commit 3: Renamed variable name"
> git log --oneline
2f408e4 (HEAD -> scenario) Commit 3: Renamed variable name
043cf10 Commit 2: Added space around operators
295f8d5 Commit 1: Created list of numbers
895e7da (master) Revert "Modified file1.txt"
c5e5a93 Modified file1.txt
d4ccf91 Add file1.txt
- After running below command, default editor of git opens
> git rebase -i 895e7da
pick 295f8d5 Commit 1: Created list of numbers
pick 043cf10 Commit 2: Added space around operators
pick 2f408e4 Commit 3: Renamed variable name
# Rebase 895e7da..2f408e4 onto 895e7da (3 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# . create a merge commit using the original merge commit's
# . message (or the oneline, if no original merge commit was
# . specified). Use -c <commit> to reword the commit message.
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
~
~
--INSERT--
- Here modify the first column with required actions
- Below changes indicate we want to squash Commits 2 & 3 into 1, having the commit message of Commit 1 only
pick 295f8d5 Commit 1: Created list of numbers
fixup 043cf10 Commit 2: Added space around operators
fixup 2f408e4 Commit 3: Renamed variable name
:wq
- Now check the logs to see how the squash has happened
> git log --oneline
1d46f11 (HEAD -> scenario) Commit 1: Created list of numbers
895e7da (master) Revert "Modified file1.txt"
c5e5a93 Modified file1.txt
d4ccf91 Add file1.txt
echo First modification > file.txt
git add file.txt
git commit -m "Commit 1"
echo Second modification >> file.txt
git add file.txt
git commit -m "Commit 2"
> git log --oneline
cf9f64f (HEAD -> scenario) Commit 2
200b7c4 Commit 1
895e7da (master) Revert "Modified file1.txt"
c5e5a93 Modified file1.txt
d4ccf91 Add file1.txt
- Remove the previous commit from history
- But retain the changes made in previous commit in working tree
> git reset --mixed HEAD~1
Unstaged changes after reset:
M file.txt
> git status
On branch scenario
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: file.txt
no changes added to commit (use "git add" and/or "git commit -a")
> git log --oneline
200b7c4 (HEAD -> scenario) Commit 1
895e7da (master) Revert "Modified file1.txt"
c5e5a93 Modified file1.txt
d4ccf91 Add file1.txt
git add file.txt
git commit -m "Commit 2"
> git log --oneline
8b84203 (HEAD -> scenario) Commit 2
200b7c4 Commit 1
895e7da (master) Revert "Modified file1.txt"
c5e5a93 Modified file1.txt
d4ccf91 Add file1.txt
> git status
On branch scenario
nothing to commit, working tree clean
- After this command, the changes made to file in Commit 2 is completely lost
> git reset --hard HEAD~1
HEAD is now at 200b7c4 Commit 1
> git status
On branch scenario
nothing to commit, working tree clean
> git log --oneline
200b7c4 (HEAD -> scenario) Commit 1
895e7da (master) Revert "Modified file1.txt"
c5e5a93 Modified file1.txt
d4ccf91 Add file1.txt
echo Second modification >> file.txt
git add file.txt
git commit -m "Commit 2"
> git log --oneline
f32f940 (HEAD -> scenario) Commit 2
200b7c4 Commit 1
895e7da (master) Revert "Modified file1.txt"
c5e5a93 Modified file1.txt
d4ccf91 Add file1.txt
git reset --soft HEAD~1
> git log --oneline
200b7c4 (HEAD -> scenario) Commit 1
895e7da (master) Revert "Modified file1.txt"
c5e5a93 Modified file1.txt
d4ccf91 Add file1.txt