git not GitHub
git 101
Initial Account Configuration
git config --global --add user.name "MyName"
git config --global --add user.email "abc@gg.cc"Initialising a Git repository
git initAdding files to staging area
git add filename
git add --all
git add -A # Same as --all
git add .Restore the changes to status quo
git restore .
git restore --staged . # To restore staged changes
git rm --cached filename # To remove from staging area altogetherTo remove untracked files / directories
git clean -fd-f - force -d - directories too
See current status, staged/unstaged files etc
git status
git status --shortCommit staged changes
git commit -m "Better write good messages"
git commit -a -m "Merge without adding/staging"
git commit # Open the default editor set by $EDITORView commit history
git log
git log --oneline
git log --format=full --decorate --graphLast Commit Info
git showDifferences between commits/branch
git diff first-branch second-branch
git diff # Show un-staged changesGetting help
git commit --help
git help --allRemoving a git repository
cd intoTheDirectoryFolder
rm .gitBranch
Creating new branch
git branch new-branchCreating a orphan(empty) branch
git checkout --orphan new-branchMoving to new branch/commit(time-travelling)
git checkout new-branch
git checkout -b new-branch # New branch is created and choosed if -b is used
git checkout commithash # time travellingChanging branches, changes files accordingly. So, ls will have different result in different branches.
Merging branches
git checkout master
git merge new # merge new into masterDeleting branches
git branch -d newList all branches
git branch # List local branches
git branch -a # List all branches
git branch -r # For remote branchesSwitch to other branch
git branch new
git switch new # AlternativeCreate and switch to other branch
git switch -c newgit with GitHub
Connecting git to github
git remote -v # List all remote refs
git remote add origin https://github.com/username/reponame.git # Add new remote ref
git remote set-url origin https://github.com/username/reponame.git # For editing
git remote get-url origin # For getting
git remote rename origin upstream # Renaming
git remote remove origin # DeletingSetting master to default branch and pushing changes to it
git push --set-upstream origin master # Sending from master branch to origin
git push # For subsequent runs
git push origin branch-nameTrack changes from upstream
git fetch origin # Changes need to merged, alsoGit pull is a combination of fetch and merge
Use pull instead of fetch and merge together
git pull originUpdate local database of remote branches
git fetch --pruneSee remote log
git log origin/masterRemote track a branch
To use a different remote branch, rather than the default one.
git switch -c branchname origin/branchnameTo change tracking upstream for branch, use
# Checkout to required branch
git branch --set-upstream-to=origin/branchnameIf used once locally, from next time, one can checkout or switch to that branch only.
GitHub Flow
- Create a new Branch
- Make changes and add Commits
- Open a Pull Request
- Review
- Deploy
- Merge
Working with credentials
Cache credentials saved
git config credential.helper cache
git push http://example.com/repo.gitGCM for credentials
git config --global credential.credentialStore secretserviceRemove that GCM configuration
git config --unset credential.helperGitHub : Forking and cloning
Cloning a repo
git clone url.git # use ssh rather than https one, if ssh has been setup
git clone url.git --depth=1 # Faster , latest commit onlyINFO
Note: According to Git naming conventions, it is recommended to name your own repository origin, and the one you forked for upstream origin : fork (R/W) upstream : original (Read only)
Excluding files from tracking (.gitignore)
Git will not track files and folders specified in .gitignore. However, the .gitignore file itself is tracked by Git. It is also possible to have additional .gitignore files in subdirectories. These only apply to files or folders within that directory.
It is also possible to ignore files or folders but not show it in the distributed .gitignore file.
These kinds of ignores are specified in the .git/info/exclude file. It works the same way as .gitignore but are not shown to anyone else.
Here are some basic gitignore syntax, one might find helpful
** : All directories check
* : All file check
? : Any single character
[a-z] : regex for single character
[!abc] : not a character from abc
name : Everything with nameRed Zone
DANGER
Proceed with caution
Reverting an error
git revert HEAD --no-edit # revert last change and commit --no-edit is to use last message only
git revert HEAD~x # where x is x+1th commit one wanna rollback toSquashing
git rebase --interactive HEAD~n # n is the number of commit you want to squash
# calling above command will open a terminal
# replace `pick` with `s` to squash a commit i.e., to remove.
git rebase -i --root main # alternative if rebase from start
git push --forceAmend a change to previous commit
Amend combines changes in the staging environment with the latest commit, and creates a new commit.
git add filename # Add to staging area
git commit --amend # Used to modify the most recent commit.Amend and replace last commit
git commit --amend -m "Added lines to README.md"Moving back n commits
git reset --soft HEAD~n # Soft reset, n is the number of commit you want to reset
git reset --hard HEAD~n # Hard reset, n is the number of commit you want to reset
git push -fReset
WARNING
Not recommended to used with remote repos
git reset commithash- commithash being the first 7 characters of the commit hash we found in the log
- one can rollback the reset, if he/she knows the final hash
Merging commits of unrelated repo
To avoid merge conflicts, move the contents of one repo to separate folder before merging
git config --local rerere.enabled 1
git config --local rerere.autoupdate true
git remote add temp <path/to/repo>
git fetch --all --tags
git merge temp/main --allow-unrelated-histories
git remote remove temp