Action |
Command |
REPOSITORY
|
git init --bare <repository_name>
git init --bare myproj.git
git init
|
CONFIGURATIONS
|
git config --global user.name "My Name"
git config --global user.email myemail@example.com
git config <key>
git config user.name
git config --global core.editor
git config core.editor "'C:\Program Files (x86)\Notepad++\notepad++.exe' -multiInst -nosession"
git config --global core.editor emacs
git difftool --tool-help
git config diff.tool vimdiff
git config alias.<command_alias> 'execute_command'
git config alias.addall 'add .'
|
WORK STATUS
|
git status
git status -u
git status -s
git status --ignored
git gui
|
CODE DIFFERENCE
|
git diff
git diff <file_name>
git diff <from_commit_id>
git diff <from_commit_id> <to_commit_id>
git diff <from_commit_id> <to_commit_id> -- <file_name>
git diff HEAD HEAD^ -- <file_name>
git gui
git diff --staged <file_name>
git difftool
git difftool <file_name>
:qa
|
STAGING FILES
|
git add <file_name>
git add <file_name1> <file_name2> <file_name3>
git add .
git add mydir/.
git add *.c
git add mydir/\*.c
git add -u
git add -u mydir/.
git reset HEAD <file_name>
git checkout -- <file_name>
|
COMMITING FILES
|
git commit
git commit -m "My message."
git commit --amend
git commit --amend -m "My message."
git commit -a -m "My message."
git commit -v
|
COMMIT LOGS
|
git log
git log <file_name>
git log -- <file_name>
git log -p
git log -4
git log --stat
git log --graph
git gitk
git log --pretty=oneline
git log --pretty=format:"%h - %ar by %an : %s"
git log --since=1.week
git log --until=1.week
git log --until=3.day
git log --until=72.hour
git log -S "Matching my_string within file."
git log --grep "Matching my_string in a commit message."
git log --author="Matching my_author string."
git help log
|
SEE COMMIT FILES
|
git diff-tree --no-commit-id --name-only -r <commit_id>
git diff-tree --no-commit-id --name-only -r 4e3761c25
|
SSH KEYS
|
ssh-keygen -t <algorithm> -b <bitsize> -C <comment>
ssh-keygen -t rsa -b 4096 -C drupal@drug.com
|
CLONING REPOSITORY
|
git clone <url>
git clone <url> <dir_path>
git clone <url> .
git clone --branch <branch_name> <url>
|
COLLABORATION
|
git remote
git remote -v
git remote show <remote_name>
git remote show origin
git remote add <remote_name> <url>
git remote add my_remote https://github.com/drupal.git
git remote rename <old_remote_name> <new_remote_name>
git remote rename origin ori
git remote set-url <remote_name> <new_url>
git remote set-url my_remote https://newgithub.com/drupal.git
git remote set-url --push <remote_name> DISABLED
git remote set-url --push upstream DISABLED
git remote remove <remote_name>
git remote remove my_remote
git fetch <remote_name>
git pull <remote_name> <branch_name>
git push <remote_name> <branch_name>
git pull
git push
|
BRANCH
|
git branch -a
git branch -v
git branch -r
git branch <branch_name>
git branch <branch_name> <commit_id>
git checkout <branch_name>
git checkout -b <branch_name>
git checkout -b branch1
git checkout -b <branch_name> <remote_name>/<branch_name>
git checkout -b my_branch my_remote/remote_branch1
git checkout -b <branch_name> <commit_id>
git checkout -b branch2 87e0e48
git branch -d <branch_name>
git branch -dr <branch_name>
git push <remote_name> -d <branch_name>
git push <remote_name> <branch_name>
git push -u <remote_name> <branch_name>
git checkout <branch_name>
git checkout master
|
CHECKOUT
|
git checkout <branch_name>
git checkout master
git checkout -- <file_name>
|
MERGE BRANCH
|
merge <branch_name>
|
TAGGING
|
git tag
git tag -l <pattern>
git tag -l "v1.1*"
git show <tag_name>
git tag <tag_name>
git tag v1.1.0
git tag -a <tag_name> -m <message>
git tag -a v1.1.0 -m "App version 1.1.0."
git tag -a <tag_name> <commit_id>
git tag -a v1.0.0 9fceb02
git push <remote_name> <tag_name>
git push origin v1.0.0
git push <remote_name> tag <tag_name>
git push <remote_name> --tags
git push origin --tags
git tag -d <tag_name>
git push <remote_name> :refs/tags/<tag_name>
git checkout <tag_name>
git checkout -b <new_branch> <tag_name>
|
PATCH
|
git diff > <patch_file>
git diff > drupal.patch
git add <new_file>
git diff --cached > drupal.patch
git diff --cached --binary > drupal.patch
cd path/to/directory
git diff --relative > drupal.patch
git format-patch <from_commit_id> --stdout > <patch_file>
git format-patch <from_commit_id> <to_commit_id> --stdout > <patch_file>
git format-patch f655d15 727ecaa --stdout > drupal.patch
git apply drupal.patch
git apply -v drupal.patch
git apply --stat drupal.patch
git apply -v --check drupal.patch
git am --signoff drupal.patch
git apply -R drupal.patch
curl <patch_url> | git apply
curl <patch_url> | git apply -R
|
CONFLICT
|
<<<<<< HEAD
Current text Line-1
Current text Line-2
Current text Line-n
======
Incoming text Line-1
Incoming text Line-2
>>>>>> 82e7c0bbf3726fb2f134e123450fb837bd6f16fa
Step 1. (In our case)
- If you want to keep your CURRENT changes,
then remove all lines between '======' and '>>>>>> 82e7c0b'
- If you want to keep INCOMING changes,
then remove all lines between '<<<<<< HEAD' and '======'
Step 2.
Remove specific 3 lines '<<<<<< HEAD', '======' and '>>>>>> 82e7c0b'.
Step 3.
Save file.
Step 4.
Stage all resolved files with 'git add'.
Step 5.
Finally, commit the accepted changes.
|
HEAD
|
cat .git/HEAD
|
TRAVERSE COMMITS (with HEAD)
|
* 727ecaa (HEAD -> master) Commit-5th on master.
* 0321e55 Merged branch 'branch1' to 'master'
|\
| * 87e0e48 (branch1) Commit-3rd on branch1.
| * bf6f1c0 Commit-2nd on branch1.
| * e40d985 Commit-1st on branch1.
* | 4ffd6c6 Commit-4th on master.
|/
* c64d3aa Commit-3rd on master.
* f655d15 Commit-2nd on master.
* a4e8c36 Commit-1st on master.
git show --oneline HEAD
git show --oneline HEAD~
git show --oneline HEAD~1
git show --oneline 727ecaa~1
git show --oneline HEAD~2
git show --oneline 727ecaa~2
git show --oneline HEAD^
git show --oneline HEAD^1
git show --oneline 727ecaa^1
git show --oneline HEAD^2
git show --oneline 727ecaa^2
git show --oneline 0321e55^2
git show --oneline HEAD^1~1
git show --oneline HEAD^1~2
git show --oneline HEAD~1^1
git show --oneline HEAD~1^2
|
HELP
|
git help
git help -a
git help <command>
git help add
git <command> -h
git add -h
git help -g
git help <concept>
git help tutorial
git help everyday
git help revisions
git help workflows
|
GIT IGNORE
|
/file
/README.txt
file
*.txt
!drupal.txt
cache/
notes/*.txt
notes/**/*.txt
!/web
/web/*
!/web/modules
/web/modules/*
!/web/modules/custom
git status --ignored
git help ignore
|
SEARCH
|
git grep -n "Search occurance of my_string per file."
git grep -c "Count occurance of my_string per file."
|
REMOVE FILES
|
git rm <file_name>
git rm log/\*.log
git rm --cached README
|
RENAME
|
git mv <file_name> <file_newname>
git mv README.MD README.TXT
|
UNDOING THINGS
|
git checkout -- <file_name>
git reset HEAD <file_name>
git reset --hard HEAD <file_name>
git update-ref -d HEAD
git reset <commit_id>
git reset --hard <commit_id>
git config --edit [OR]
git config --global --edit
git commit --amend --reset-author
git revert <commit_id>
Git revert HEAD
commit cb0a734b217150b0bab0615038b6192db2e4ac71 (HEAD -> master)
Author: drupaldrug
Date: Thu Jun 18 22:38:15 2020 +0530
Revert commit message.
This reverts commit ce01158390af2abc5502cc1a0da3bb5710d3e561.
commit ce01158390af2abc5502cc1a0da3bb5710d3e561
Author: drupaldrug
Date: Thu Jun 18 22:35:16 2020 +0530
My commit message.
git log --oneline
727ecaa (HEAD -> master) Commit-3rd on master.
f655d15 Commit-2nd on master.
a4e8c36 Commit-1st on master.
git reset f655d15
git log --oneline
f655d15 (HEAD -> master) Commit-2nd on master.
a4e8c36 Commit-1st on master.
git merge 727ecaa
git log --oneline
727ecaa (HEAD -> master) Commit-3rd on master.
f655d15 Commit-2nd on master.
a4e8c36 Commit-1st on master.
|
TROUBLESHOOT & OTHER
|
git --version
clear
git gui
git gitk
git blame <file_name>
|
BOOKS
|
Download Pro Git E-Book
|
GITHUB
|
Check Github Status
|
Command line instructions
ReplyDeleteYou can also upload existing files from your computer using the instructions below.
Git global setup
git config --global user.name "<name>"
git config --global user.email "<email>"
Create a new repository
git clone <repository_url>
cd <project>
touch README.md
git add README.md
git commit -m "add README"
git push -u origin master
Push an existing folder
cd existing_folder
git init
git remote add origin <repository_url>
git add .
git commit -m "Initial commit"
git push -u origin master
Push an existing Git repository
cd existing_repo
git remote rename origin old-origin
git remote add origin <repository_url>
git push -u origin --all
git push -u origin --tags
STASH
ReplyDeleteStashing takes your modified tracked files and staged changes — and saves it on a stack of unfinished changes that you can reapply at any time (even on a different branch).
// Stash current changes.
git stash
// List all stash.
git stash list
// Pop latest/top stash and apply.
git stash apply
// Pop particular stash and apply.
git stash apply n
// Show stash summary all/particular
git stash show
git stash show stash@{n}
// Show stash patch format all/particular
git stash show -p
git stash show -p stash@{n}
// Delete latest/top stash.
git stash drop stash@{0}
// Delete particular stash.
git stash drop stash@{0}
Multiple SSH keys:
ReplyDelete➜ ssh-keygen -t rsa -b 4096 -C "youremail@yourdomain.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/dev/.ssh/id_rsa): /Users/dev/.ssh/id_rsa_yoyo
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/dev/.ssh/id_rsa_yoyo.
Your public key has been saved in /Users/dev/.ssh/id_rsa_yoyo.pub.
The key fingerprint is:
SHA256:/fdsdsdga4EITXUc/K5nRyUfdsferfrefdfdst4jtzkeAI youremail@yourdomain.com
The key's randomart image is:
+---[RSA 4096]----+
| .+. o+|
| . .+ +o|
| .. . B o|
| E . O. .o = |
| o S *.o o.*|
| = O +...++|
| o . o....|
| .=..o|
| +o .*|
+----[SHA256]-----+
Now use your yoyo public key to upload to git agent
➜ cat ~/.ssh/id_rsa_yoyo.pub
// Create config for .ssh for mapping repositories
➜ vim ~/.ssh/config
#yoyo account
Host github.com-yoyo
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_yoyo
// Now come back to project
➜ cd myproject
➜ git init
Initialized empty Git repository in /myproject/.git/
// Generally we have repository as git@github.com:yoyo/yoyo_proj.git
// But since we are mapping it with ~/.ssh/config, remote should look somewhat like
// git@github.com-<specific_account>:yoyo/yoyo_proj.git
➜ git remote add origin git@github.com-yoyo:yoyo/yoyo_proj.git
➜ git remote -v
origin git@github.com-yoyo:yoyo/yoyo_proj.git (fetch)
origin git@github.com-yoyo:yoyo/yoyo_proj.git (push)
// Now u can collaborate
➜ git pull origin develop
https://gist.github.com/jexchan/2351996
Deletehttps://superuser.com/a/1473365
DeleteToggle branch checkout, use hyphen
ReplyDeletegit checkout -
If wrongly pulled code and facing conflict issues
ReplyDeletegit merge --abort
Similarly,
git rebase --abort
git show commit_id
ReplyDeleteshow changes in the commit
Pull branch from different repository
ReplyDeletegit remote add another_repo another_repo_fork_link
git fetch another_repo
git checkout -b my-branch
git pull another_repo branch
git fetch another_repo branch_name
Deletegit checkout branch_name
Also works
git fetch remote rbranch:lbranch
Deletegit checkout lbranch
...where rbranch is the remote branch or source ref and lbranch is the as yet non-existent local branch or destination ref you want to track and which you probably want to name the same as the remote branch or source ref. This is explained under options in the explanation of .
Checking out pull requests locally "git fetch origin pull/ID/head:BRANCH_NAME"
Deletehttps://docs.github.com/en/pull-requests/collaborating-with-pull-requests/reviewing-changes-in-pull-requests/checking-out-pull-requests-locally
Cherry picking is to choose a commit from one branch and apply it to another
ReplyDeletegit checkout my_branch
git cherry-pick commit_hash_from_other_branch
[merge conflicts if any]
[git cherry-pick --continue]
Show commited files
ReplyDeletegit show --pretty="" --name-only 81ecf27095e2ddf3
git show --name-only HEAD
Deletesyntax error near unexpected token `('
ReplyDeleteIf a file has special characters put them in quotes
git add "myfile(x86).yml"
Convert your remote repository to bare repository
ReplyDeleteExecute the following command in your remote repository folder
git config --bool core.bare true
git diff accepts an optional exclude
ReplyDeletegit diff -- ":(exclude)thingToExclude"
You might want to add some wild cards
git diff -- ":(exclude)*/thingToExclude/*"
Target specific file types
git diff -- ":(exclude)*/$1/*.png"
https://stackoverflow.com/a/57892848
IMPORTANT: Updates that commit ID; consequently if any tag will be cleared, though will exists.
ReplyDeletegit commit --amend --reset-author
OR
git commit --amend --author="myuser <mymail@drupal.org>"
How GIT handles symlinks:
ReplyDelete➜ git ls-files -s docroot/simplesaml
120000 fd1b2965bc501ee3bbd680c2a8ce07b5e24551d3 0 docroot/mysymlink
➜ git cat-file -p fd1b2965bc501ee3bbd680c2a8ce07b5e24551d3
../vendor/simplesamlphp/simplesamlphp/www/%
Also check in file : .git/objects/fd/1b2965bc501ee3bbd680c2a8ce07b5e24551d3
More details: https://stackoverflow.com/a/18791647
Rename branch
ReplyDeleteIf you want to rename a branch while pointed to any branch, do:
git branch -m
If you want to rename the current branch, you can do:
git branch -m
https://stackoverflow.com/a/6591218
# Debug git command if over SSH.
ReplyDelete# Debug level 1, 2 or 3 with -v, -vv or -vvv.
GIT_SSH_COMMAND="ssh -v" git push origin my-branch
https://stackoverflow.com/a/35017258
Deletehttps://askubuntu.com/a/620985
DeleteIn case, connection to github is lost due to git hook taking more time to execute.
ReplyDelete"Connection to github.com closed by remote host."
Adding following settings in .ssh\config would send a null packet to the server every 60 seconds (keeping the connection alive) for 30 rounds. This would buy you 30 minutes of connection.
Host *
ServerAliveInterval 60
ServerAliveCountMax 30
https://stackoverflow.com/a/65818657
A successful Git branching model https://nvie.com/posts/a-successful-git-branching-model/
ReplyDeleteReference- https://microsoft.github.io/code-with-engineering-playbook/source-control/git-guidance/
ReplyDeleteGit commands- https://confluence.atlassian.com/bitbucketserver/basic-git-commands-776639767.html
ReplyDeleteGit was built in 5 days - https://graphite.dev/blog/understanding-git
ReplyDeleteInside .git - https://jvns.ca/blog/2024/01/26/inside-git/
ReplyDelete