Git Notes
GIT CLI Notes
-
Initialization :
-
init :
git init
in specific repository`
-
view all files in current repo :
ls -a
-
-
Remote work :
-
know your current URLs
git remote -v
-
set url [FETCH]
git remote set-url www.new-url.here
-
set url [PUSH]
git remote set-url www.new-url.here
-
see remote commit graph :
git log origin/master --oneline --graph --all
-
FETCH :
git fetch {to-do after fetching : git status}
-
PULL :
git pull <repository> <branch>
git pull {combines
fetch&
merge:
git merge+
git merge FETCH_HEAD}
-
git pull --allow-unrelated-histories {if trying to push existing local repo to newly created remote repo}
-
MERGING OPTIONS :
--ff
[default] => performs Fast-Forward merge if possible otherwise does merge commit.--no-ff
=> performs merge commit only. (also if FF merge is possible)--ff-only
=> performs FF merge if possible otherwise cancel merge operation--rebase [--perserve-merges]
=>git rebase master
performs rebasing with rewriting commit history
-
PUSH :
git push [-u] <repository> <branch>
git push --set-upstream-to <origin/remote-branch> <local-branch>
git branch --set-upstream-to origin/master
-
-
View SHA-1 value :
git show HEAD
-
git show master
-
if multiple commits :
git show HEAD~
[to view parent commit details]
-
Tagging :
-
view tags :
git tag
-
add tag with message :
git tag -a -m 'includes feature 1' v1.0
-
view tag information :
git show v1.0
-
As we have to explicitly push tags to remote :
git push origin v1.0
-
add tag to previous commits :
git tag -a -m 'pre-released version' temp HEAD~
-
delete tag :
git tag -d temp
-
push all tags to remote :
git push --tags
git push <remote> --tags
-
delete tag from remote :
git push --delete origin temp
-
-
Clone a project :
git clone PROJECT_URL_HERE
-
Adding file to staging area :
git add fileA.txt
{ add all files to staging area :
git add .
}
-
Commit file with given message :
git commit -m 'Feature X added in this commit'
-
Edit commit message with amend :
git commit --amend -m 'add fileC.txt'
{suppose in previous commit, we have a typo in commit msg i.e. we written 'ad fileC.txt' then if we want to change it to 'add fileC.txt' then we will use above command.}
-
Edit commit files without changing the commit message :
- {add modified files to staging area then commit as below...}
git commit --amend --no-edit
- {add modified files to staging area then commit as below...}
-
Know current status of files :
git status
-
Logging :
-
View log :
git log
[to skip : Control+Z]
-
View git log of remote also :
git log --all
-
View commit graphs :
- see remote commit graph :
git log origin/master --oneline --graph --all
- see local commit graph :
git log master --oneline --graph --all
- see all commits :
git log --oneline --graph --all
- see remote commit graph :
-
-
View SHA-1 values :
git reflog
-
Branches :
-
Know current branches :
git branch
-
Display local and tracking branches :
git branch --all
-
Switch branch :
git checkout featureX
-
Create branch :
git branch featureX
-
Create and Switch branch :
git checkout -b featureX
-
Delete branch :
git branch -d featureX
-
View Branch graph :
git log --oneline --graph
git log --oneline --graph --all
-
Force delete branch without merging :
git branch -D tempBranch
-
Get back deleted branch with its SHA-1 value :
git branch tempBranch c7d909c
-
Get logs from specific branch :
git log origin/master --oneline
git log origin --oneline
[will give same result as above]
-
Set default remote tracking branch :
git remote set-head origin BRANCH_NAME_TO_BE_DEFAULTED_REMOTLY
-
delete branch on remote :
git push -d <remote_name> <branch_name>
-
-
Merge :
-
no fast-forward with commit message [with force FF commit] :
git merge --no-ff BRANCH_NAME_HERE -m 'merged BRANCH_NAME_HERE with no ff'
-
Simple Fast-Forward merge :
git merge featureX
[switch on the branch to do merging upon selecting that branch as base]
-
Abort Merge conflicts :
git merge --abort
-
REBASE :
- checkout feature branch
- perform rebase command with master(or whatever branch)
or
- checkout feature branch and perform rebasing
METHOD 1 : -
git checkout featureX
-git rebase master
METHOD 2 : -
git rebase master featureX
-
when merge conflicts occurs : > git status {to check what happened} > modify the required affected files > add those files to staging area and then continue rebasing...
- Skip this patch in REBASE :
git rebase --skip
- ABORT REBASE :
git rebase --abort
- CONTINUE REBASE :
git rebase --continue
- Skip this patch in REBASE :
INTERACTIVE REBASE : -
git rebase -i <after-this-commit>
-
SQUASH MERGE :
git checkout master
git merge --squash featureX
git commit
> accept or modify the squash messagegit branch -D featureX
-
-
SOME GENERAL TERMINAL COMMANDS :
- delete whole line :
Control+U
- Put back whole line deleted by above command :
Control+Y
- Delete a word :
Control+W
- delete whole line :
-
Pushing existing local repo to new remote repo :
git branch --set-upstream-to origin/master
git pull --allow-unrelated-histories
-
delete the commit :
git reset --hard <SHA-1 value>
-
See shortlog
git shortlog
-
See number of commits :
git rev-list HEAD --count
git rev-list --count <revision> {
HEAD,
masterfor particular branch/head}
git rev-list --all --count
-
Upload large files through
git lfs
- Track files :
git lfs track "*.mp4"
- now simply follow the conventional steps.
- If you get an error that "this exceeds Git LFS's file size limit of 100 MB" when you try to push files to Git, you can use git lfs migrate instead of filter branch or the BFG Repo Cleaner, to move the large file to Git Large File Storage. For more information about the git lfs migrate command, see the Git LFS 2.2.0 release announcement.
- Track files :