git init
= to initialise(create) a repository(.git file is added)git status
= to see the the status(commits, changes) of your repositoryrm -rf .git
= to delete your created git repository(just deletes the .git file)git clone repository_link your_name
= will clone a repository and give it your_namegit clone repository_link
= will clone a repository with default namels
= will list the items present in the current directorypwd
= will show you the present working directorycd
= to chage directory(eg- for folder = cd folder_name/ , for a drive = cd drive_name: , for a file = cd file_name etc)cd ..
= to move back in the directorygit add .
= to add all the changes in staging areagit add file_name
= to add a specific file in staging areagit rm --cached Book1.xlsx
= will remove Book1.xlsx from tracking
git diff
= differentiates between working directory and staging area.git diff --staged (or you can use --cached)
= differentiates previous commit and staging area.git rm file
= to delete a file
git mv file file_rename
= to rename a file (will change name of file to file_rename)
In linux , renaming and moving a file is same
git log
= will show all the commits made by an user with his/her name,date,email,timegit log -p
= will show the log and the files added or deleted by someone.git log -p -m(Where m is a number)
= will show the log and the files added or deleted by m people(commits)git log --stat
= will give a short summary about commits of different peoplegit log --pretty=oneline
= will show all commits in one linegit log --pretty=short
= will show all commits in short(will not show the date)git log --pretty=full
= will show author name his full commits without dategit log --since=2.days
= will show the commits done in 2 days (you can use .weeks, .years, .months, .hours)%H - commit hash
%h - abbreviated commit hash
%T - tree hash
%t - abbreviated tree hash
%P - parent hashes
%p - abbreviated parent hashes
%an - author name
%aN - author name
%ae - author email
%aE - author email
git log --pretty=format:"%h -- %an"
= will show abbreviated commit hash with (two –) in between and then author name
git restore --staged <file name>
= will unstage the given file
git checkout
= (can be used to change the current state of your repository (to move among) 1.branch 2.commit 3.tag)git checkout <commit hash>
= will move to the state of that commitgit checkout HEAD
= will move to the state of latest commit.git checkout --<file_name>
= will restore to the state when previous commit was donegit checkout -f
= will restore your whole working directorygit checkout -- .
= revert the uncommitted changes only in the current working directorygit checkout --
= list the files that will be reverted without actually making any actiongit reset
= will remove all the files from the staging areagit reset --soft [hash of the commit upto which you want to delete the commit]
= will not undo the changes but will
delete the commits above the specified hash of the commit.git reset --soft HEAD~1
= to delete the last commit [HEAD] without undoing the changesgit revert [hash of the commit]
= will undo the changes with a new commit [Revert (the commit message)]git remote
= will show if you are connected to a server(Github etc..) or notgit remote add origin <Your repo link>
= set the repository name to origin on git servergit remote -v
= will tell you from where are you pushing and pulling a filegit push -u origin master
= to push your repository from local device to the servergit push origin -d <branch_name>
= Deletes a remote branch named Step1: ssh-keygen -t ed25519 -C “your_email@example.com”
(here we are using ed25519 algorithm which is latest otherwise you can use the tranditional method using rsa but not recommended , for reference you can go to https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent)
Step2: Then click enter , then enter.
Step3: Now go to C:/Users/your user_name/.ssh/id_rsa.pub and open it through notepad.
Step4: Now copy the contents present in it (This is your ssh key)
Step5: Then go to your github account click on the topmost right corner , go to settings then SSH and GPG keys , now click on newSSH_key AND paste the key Then add your key and you are good to go.
If you have multiple GitHub accounts and want to use different accounts for specific repositories, you can configure Git to switch between them. Here’s how to set up Git to use a second GitHub account for a particular repository:
Configure SSH Key Pair for the Second GitHub Account:
First, ensure you have an SSH key pair set up for the second GitHub account you want to use:
Generate a new SSH key pair for the second GitHub account:
ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519_second_account -C "your_email@example.com"
Add the SSH key to your SSH agent:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519_second_account
Copy the public key (id_ed25519_second_account.pub
) to your second GitHub account’s SSH settings in the GitHub account settings.
Edit Your SSH Configuration:
To specify which SSH key to use for which GitHub account, create or edit your ~/.ssh/config
file. If this file doesn’t exist, create it. Add the following configuration:
# Default GitHub account (first account)
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519
# Second GitHub account
Host github-second
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_second_account
Replace github-second
with a label you choose for the second GitHub account.
Navigate to the local repository where you want to use the second GitHub account. In that repository’s directory, configure Git to use the second GitHub account by updating the origin
remote URL:
First, check the current URL of the remote:
git remote -v
Change the URL of the remote to use the second GitHub account:
git remote set-url origin git@github-second:username/repo.git
Replace username/repo.git
with the repository details you want to work with using the second GitHub account.
Now, when you interact with the specific repository, Git will use the SSH key and configuration for the second GitHub account.
Repeat these steps for other repositories where you want to use the second GitHub account. This way, you can have multiple GitHub accounts configured on your local machine and use them as needed.
git config --global alias.st status
= will set alias for git status as git st globally. (You can create your own shortcuts according to your convenience)git config --global --edit
- to edit the global configuration file [your default editor(eg- vim) opens up]git config --global --unset alias.'alias you want to delete'
- to delete any alias you wantgit config --global --edit
[alias]
s = status
a = add .
b = checkout -b
br = branch
del = delete -D
p = push
edit = config --global --edit
m = branch -M main
co = checkout
ci = commit -m
cia = commit -am
lg = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative
git restore file_name
= will switch to the state where your file was earlier when commited(all changes discarded)git branch newbranchname
= will create a new branch with name newbranchnamegit checkout -b newbranchname
= will create and switch to the new branchgit checkout master
= to switch again to master branchgit commit -a -m "message"
= to skip the staging area and directly commit
Each commit has a unique SHA-1 hash and is 160bits or (20bytes).
git commit --amend
= now we can add the changes done in the previous commit without adding a new commit.(this will open vim editor and you can start writing commit by pressing ‘i’ on your keyboard then after writing press ESC button and then write ‘:wq’ to exit ) [For more info you can go to - https://www.atlassian.com/git/tutorials/rewriting-history] [writing git commit --amend --no-edit
will save the newly added things with same last commit]git branch
= to list all the branches in your git repositorygit merge branch_name
= will merge branch in master branch
«< = conflict resolution marker
git add file_name
= this is used to add a file in staging area but also is used to mark a merge conflictgit branch -v
= will show the no. of branches and show last commit hash and commit message on the local devicegit branch -av
= will show all the branches in the repositorygit branch --merged
= will show all the branches that has already mergedgit branch --no-merged
= will show all the branches that has not mergedgit branch -d branch_name
= will delete branch that has already merged , and will give error if branch has not merged to overcome the error write git branch -D branch_name
git push origin --delete [name of the deleted branch]
= to delete the branch on the servergit branch --list
= will show the no. of branches(condition: there should be some content in the branch and are commited)touch file_name
= will create a file with name file_nameTypes of branching workflow = 1.Long Running Branches 2.Short Running Branches
git push origin bugfix
= will push a branch named bugfix in the master branch origin
[NOTE: Always remember to be in the same branch which you want to push]
git push origin bugfix:mybugfix
= will change bugfix to mybugfix in remote(Github etc)git pull remote_name
= will download(pull) all the changes done by someone who had made the repo that you cloned.git pull
= will download and apply all the changes done onlinegit fetch remote_name(eg- origin)
= will download but not apply the changes done by someone who had made the repo that you cloned.git fetch --all
= will download all the changes from all the remotes.git branch --show-current
= will show the current branch you are ongit stash
= will store the code in a temporary location(procedure followed is LIFO(last in first out))git stash list
= will show all the things that are stored in the stash.git stash -u
= will store untracked files too with the changes done recently in the files that are staged .git stash
= will stash only the changes in tracked and unstaged files.git stash pop
= will reapply the stashed itemgit stash show
= will show the no. of files present in the stashed itemgit stash show -p
= will give the detailed information about the stash items (where -p is patch , it is a type of format)git stash drop
= will delete the stash presentgit tag v1.0
= [soft tag]will store the current state (version) of your repository as verison v1.0 [Mostly used in production]git tag --list
= will show all the tags created
git tag -a v1.1 = to create a tag properly we use -agit checkout <tag name>
= to switch to the state of given tag namegit show <commit ID>
= will show the information about repo at given
cat <file>
= will show the contents present a file
To rename a branch while pointed to any branch:
git branch -m <oldname> <newname>
To rename the current branch:
git branch -m <newname>
To push the local branch and reset the upstream branch:
git push origin -u <newname>
To delete the remote branch:
git push origin --delete <oldname>
git restore <filename>
- discarding all local changes in a file
Discarding uncommitted local changes cannot be undone!
git restore <filename>
- restoring a deleted file
git restore -p <filename>
- discarding chunks/lines in a file
git restore .
- discarding all local changes
Discarding uncommitted local changes cannot be undone!
git commit --amend -m 'correct commit'
- fixing the last commit
”–amend” rewrites history! Never change history for commits that have already been pushed to a remote repository
git revert <commit hash>
- reverting a commit in the middle
Non-destructive way by creation of a new commit instead of swapping
git reset --hard <commit hash>
- resetting to an old revision
”–hard” remove the commits after
, to preserve them use "--mixed"
git restore --source <commit hash> <filename>
- resetting a file to an old revision
git reflog
and git branch happy-ending <commit hash>
- recovering deleted commits
git reflog
and git branch develop <commit hash>
- recovering a deleted branch
git branch feature/name
and git reset HEAD~1 --hard
- moving a commit to a new branch
git checkout feature/name
git cherry-pick <commit hash>
git checkout master
git reset --hard HEAD~1
# 1. How far back do you want to go? What should be the "base" commit?
# 2. $ git rebase -i HEAD~3 (if third commit from the head)
# 3. In the editor, only determine which actions you want to perform. Don't change commit data
# in this step, yet! Notice: Commits are in "reverse" order!
git rebase -i <commit hash>
and use “drop” action - deleting old commits
git rebase -i HEAD~3
# ...then use "squash" option
git commit --fixup <commit hash>
git rebase -i --autosquash HEAD~4 #(change it according to need)
Caution when rewriting history! Do NOT rewrite commit history that has already been pushed to a remote repository!
- Amending Commits
- Rebase
- Reset
- …
git archive (http://www.kernel.org/pub/software/scm/git/docs/git-archive.html) - Creates a tar or zip file of the contents of a single tree from your repository. Easiest way to export a snapshot of content from your repository.
git gc (http://www.kernel.org/pub/software/scm/git/docs/git-gc.html) - Garbage collector for your repository. Packs all your loose objects for space and speed efficiency and optionally removes unreachable objects as well. Should be run occasionally on each of your repos.
git fsck (http://www.kernel.org/pub/software/scm/git/docs/git-fsck.html) - Does an integrity check of the Git “filesystem”, identifying dangling pointers and corrupted objects.
git prune (http://www.kernel.org/pub/software/scm/git/docs/git-prune.html) - Removes objects that are no longer pointed to by any object in any reachable branch.
git daemon (http://www.kernel.org/pub/software/scm/git/docs/git-daemon.html) - Runs a simple, unauthenticated wrapper on the git-upload-pack program, used toprovide efficient, anonymous and unencrypted fetch access to a Git repository.