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 init
Adding 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 altogether
To remove untracked files / directories
git clean -fd
-f - force -d - directories too
See current status, staged/unstaged files etc
git status
git status --short
Commit 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 $EDITOR
View commit history
git log
git log --oneline
git log --format=full --decorate --graph
Last Commit Info
git show
Differences between commits/branch
git diff first-branch second-branch
git diff # Show un-staged changes
Getting help
git commit --help
git help --all
Removing a git repository
cd intoTheDirectoryFolder
rm .git
Branch
Creating new branch
git branch new-branch
Creating a orphan(empty) branch
git checkout --orphan new-branch
Moving 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 travelling
Changing branches, changes files accordingly. So, ls
will have different result in different branches.
Merging branches
git checkout master
git merge new # merge new into master
Deleting branches
git branch -d new
List all branches
git branch # List local branches
git branch -a # List all branches
git branch -r # For remote branches
Switch to other branch
git branch new
git switch new # Alternative
Create and switch to other branch
git switch -c new
git 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 # Deleting
Setting 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-name
Track changes from upstream
git fetch origin # Changes need to merged, also
Git pull is a combination of fetch and merge
Use pull instead of fetch and merge together
git pull origin
See remote log
git log origin/master
Remote track a branch
To use a different remote branch, rather than the default one.
git switch -c branchname origin/branchname
If 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.git
GCM for credentials
git config --global credential.credentialStore secretservice
Remove that GCM configuration
git config --unset credential.helper
GitHub : 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 only
INFO
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 name
Red 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 to
Squashing
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 --force
Amend 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 -f
Reset
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