Git/GitHub #
Git #
User Configuration #
- Set your identity
git config --global user.name "your_name" git config --global user.email "your_email@example.com"To keep your email address private, use your GitHub-provided no-replyemail address.
- Set default commit branch
git config --global init.defaultBranch main
- Check your settings
git config --list
Basic Operations #
- 
Clone a repository git clone <repository_url>
- 
Pull changes from the remote repository git pull
- 
Stage files in the current directory # stage a specific file git add <file_name> # stage multiple specific files git add <file_name1> <file_name2> # stage all changes in the current directory git add .
- 
Commit your changes git commit -m "<commit_message>"
- 
Push changes to the remote repository git push
Branch Operations #
- Switch to an existing branch
git checkout <branch_name>
- Create a new branch from the current branch and switch to it
git checkout -b <new_branch_name>
- Create a new branch from a specific branch and switch to it
git checkout -b <new_branch_name> <branch_name>
- List all branches
git branch -a
- Rename the current branch
git branch -m <new_branch_name>
- Delete a local branch
git branch -d <branch_name>
- Push a new branch to the remote repository
git push -u origin <branch_name>
- Delete a branch from the remote repository
git push origin -d <branch_name>
Submodule Management #
- Add a new submodule to the repository
git submodule add <repository_url> <path>
- Initialize, fetch and checkout the submodule
git submodule update --init --recursive
Bisecting Commits #
- 
Start the bisect process git bisect start
- 
Mark the current commit as bad (contains the bug) git bisect bad
- 
Mark an older commit where the bug did not exist as good git bisect good <commit_hash>
- 
Git will automatically checkout the next commit in the middle of the good and bad commits - 
If the bug is present, mark the commit as bad git bisect bad
- 
If the bug is not present, mark the commit as good git bisect good
 
- 
- 
Continue marking commits until Git identifies the first bad commit 
- 
Once the problematic commit is found, end the bisect session git bisect reset
Commit Reversion #
⚠️Warning: Reverting commits can potentially lead to loss of work. Ensure you have backups or have communicated with your team before performing these actions.
- Identify the commit
git log
- Reset to the previous commit
git reset --hard HEAD~1 # or git reset --hard <commit_hash>This will discard all changes after the specified commit. 
- Force push the changes
git push --forceThis can overwrite changes in the remote repository. 
Undo and Reset Git Commits #
⚠️Warning: Undoing commits can rewrite history and affect collaborators. Ensure you understand the implications before proceeding.
Undo the Last Commit #
- 
Keep changes in working directory git reset --soft HEAD~1
- 
Unstage changes git reset HEAD~1
- 
Discard the commit completely git reset --hard HEAD~1⚠️Warning: --hardpermanently deletes changes.
Undo a Specific Commit #
git rebase -i <commit_before_target>
Revert a Commit #
git revert <commit_hash>
Amend the Last Commit #
- 
Change commit message git commit --amend -m "New commit message"⚠️Warning: Requires force pushing if the commit has been pushed remotely. 
- 
Include additional changes - 
Stage the changes git add <file_name>
- 
Amend the commit git commit --amend⚠️Warning: Requires force pushing if the commit has been pushed remotely. 
 
- 
Reset to a Previous State #
- 
Soft reset: keeps changes staged git reset --soft <commit_hash>
- 
Mixed reset: unstages changes, keeps them in working directory git reset --mixed <commit_hash>
- 
Hard reset: discards all changes git reset --hard <commit_hash>⚠️Warning: --hardpermanently deletes changes.
Rebasing Specific Commits #
⚠️Warning: Rebasing rewrites commit history. Avoid rebasing public branches shared with others to prevent conflicts and potential data loss.
- 
Create a backup branch to prevent accidental data loss git branch backup-branch
- 
Identify the range of commits to rebase git log --onelinea1b2c3d (HEAD -> feature) feat: add new feature e4f5g6h fix: resolve bug i7j8k9l docs: update README m1n2o3p chore: update dependencies q4r5s6t refactor: improve code structure
- 
Start an interactive rebase git rebase -i HEAD~5pick q4r5s6t refactor(main): improve code structure pick m1n2o3p chore(main): update dependencies pick i7j8k9l docs(main): update README pick e4f5g6h fix(main): resolve bug pick a1b2c3d feat(main): add new feature # Rebase 123abc4..a1b2c3d onto 123abc4 (5 commands) # # Commands: # p, pick <commit> = use commit # r, reword <commit> = use commit, but edit the commit message # e, edit <commit> = use commit, but stop for amending # s, squash <commit> = use commit, but meld into previous commit # f, fixup [-C | -c] <commit> = like "squash" but keep only the previous # commit's log message, unless -C is used, in which case # keep only this commit's message; -c is same as -C but # opens the editor # x, exec <command> = run command (the rest of the line) using shell # b, break = stop here (continue rebase later with 'git rebase --continue') # d, drop <commit> = remove commit # l, label <label> = label current HEAD with a name # t, reset <label> = reset HEAD to a label # m, merge [-C <commit> | -c <commit>] <label> [# <oneline>] # create a merge commit using the original merge commit's #
- 
Save and close the editor 
 After making the desired changes, save the file and close the editor. Git will proceed with the rebase based on your instructions.
- 
Complete the rebase 
 After handling all actions, Git will complete the rebase process. If there are conflicts, Git will pause and prompt you to resolve them.
Conflict Resolution #
- 
Identify the conflict git status # or git diff
- 
Open the conflicting file <<<<<<< HEAD Your changes ======= Changes from the branch you are merging >>>>>>> branch-nameRemove the conflict markers and decide what the final content should be. 
- 
Add the resolved file to the staging area git add <file_name>
- 
Commit the resolved changes git commit
- 
Continue with your merge or rebase git merge --continue # or git rebase --continue
Git Stash for Branch Switching #
- 
Save your current changes git stash
- 
View the stashed changes git stash list
- 
Switch to the target branch git checkout <target_branch>
- 
Apply and remove the latest stash git stash pop
Git Large File Storage #
- 
Install Git LFS git lfs install
- 
Use Git LFS to track specific file types. For example, to track all .psdfilesgit lfs track "*.psd"
- 
After you add files to be tracked by LFS, ensure you add them to the repository by staging the .gitattributesfilegit add .gitattributes git commit -m "chore(main): add Git LFS tracking for large files"
- 
Git LFS automatically handles large files as you push them to the repository 
GitHub #
SSH Key Setup #
- 
Generate a new SSH key ssh-keygen -t ed25519 -C "your_email@example.com"To keep your email address private, use your GitHub-provided no-replyemail address.
- 
Copy the SSH public key to your clipboard cat ~/.ssh/id_ed25519.pubThen select and copy the contents of the id_ed25519.pubfile displayed in the terminal to your clipboard.
- 
Add a new SSH authentication key to your account 
GPG Key Setup #
- Download and install the GPG command line tool
- Generate a GPG key pair
gpg --full-generate-key
- Enter your user ID information
When asked to enter your email address, ensure that you enter the verified email address for your GitHub account. To keep your email address private, use your GitHub-provided no-replyemail address.
- List the long form of the GPG keys
gpg --list-secret-keys --keyid-format=longSome GPG installations on Linux may require you to use gpg2 --list-keys --keyid-format LONGto view a list of your existing keys instead. In this case you will also need to configure Git to usegpg2by runninggit config --global gpg.program gpg2.
- Copy the long form of the GPG key ID you’d like to use
$ gpg --list-secret-keys --keyid-format=long /Users/hubot/.gnupg/secring.gpg ------------------------------------ sec 4096R/3AA5C34371567BD2 2016-03-10 [expires: 2017-03-10] uid Hubot <hubot@example.com> ssb 4096R/4BB6D45482678BE3 2016-03-10In this example, the GPG key ID is 3AA5C34371567BD2.
- Paste the text below, substituting in the GPG key ID you’d like to use
gpg --armor --export 3AA5C34371567BD2 # Prints the GPG key ID, in ASCII armor formatCopy your GPG key, beginning with -----BEGIN PGP PUBLIC KEY BLOCK-----and ending with-----END PGP PUBLIC KEY BLOCK-----.
- Add a new GPG key to your account
- Set your primary GPG signing key in Git
git config --global user.signingkey 3AA5C34371567BD2
- Configure Git to sign all commits by default
git config --global commit.gpgsign true
Initialize a New Git Repository #
echo "# <repository_name>" >> README.md
git init
git add README.md
git commit -m "init(main): create repository and add README.md"
git branch -M main
git remote add origin <repository_url>
git push -u origin main
Common Commit Message #
- 
feat:– For new features or significant additions.- Example: feat: add user authentication module
 
- Example: 
- 
fix:– For bug fixes or resolving issues.- Example: fix: resolve login error on homepage
 
- Example: 
- 
docs:– For documentation changes.- Example: docs: update README with setup instructions
 
- Example: 
- 
chore:– For routine tasks, maintenance, or minor setup changes.- Example: chore: set up project structure
 
- Example: 
- 
refactor:– For refactoring code without changing its functionality.- Example: refactor: optimize data processing logic
 
- Example: 
- 
style:– For formatting changes that do not affect the code’s functionality (e.g., fixing indentation).- Example: style: apply consistent code formatting
 
- Example: 
- 
test:– For adding or updating tests.- Example: test: add unit tests for authentication service
 
- Example: 
- 
perf:– For performance improvements.- Example: perf: enhance database query efficiency
 
- Example: 
- 
build:– For changes that affect the build system or dependencies.- Example: build: update webpack configuration
 
- Example: 
- 
ci:– For CI/CD pipeline changes.- Example: ci: update GitHub Actions workflow
 
- Example: