Version Control Git
January 13, 2023, 7:36 AMTable of Contents
Original
- https://inside.digipen.edu/main/ITFAQ:Git_FAQ (Original Documentation)
Request for Team repository
Student (Team Leader) will need to send email to helpdesk.sg@digipen.edu with the following important details.
- Name of Group
- List of team members (DigiPen email ID)
- CourseCode and Instructor.
Thereafter, your repository will be accessible for the members and instructors via
- https://git.sg.digipen.edu/projects/(groupname)
Members will also have access to project-related SCM tool
- https://trac-git.sg.digipen.edu/projects/(groupname)/timeline
How do I clone a repo?
# Clones to ./<project_name>
git clone https://git.sg.digipen.edu/projects/<project_name>
# Clones to ./<dir_name>
git clone https://git.sg.digipen.edu/projects/<project_name> <dir_name>
How do I check which branch I'm currently in?
Is my branch up to date?
Are there any unstaged changes?
What changes are ready to be commited?
Commited changes which have yet to be merged?
git status
Lists the following:
- Current branch
- Is branch up to date
- Are there any unstaged changes
- Changes in the staging index that are read to be committed
- Commited changes which have not been merged
- Useful tips!
List all unstaged changes
# Normal output
git diff
# Highlighted output
git diff --color-words
# To view changed on staged files
git diff --staged
Create a new branch and switch to it
git checkout -b <branch-name>
Rename branch
git branch -m <new-branch-name>
Add files to the staging index
git add <file_name>
Dont forget to commit!
Commit your changes
git commit -m "<descriptive but short message about my commit>"
Push changes to a remote repo
git push
Pull changed from a remote repo
git pull
Switch the the last branch I was on
git checkout -
How do I search all changes ever made in my repo?
git grep "<something you want to find>"
How do I pull a specific commit from another branch onto my branch
git cherry-pick <sha-value>
Discard local file modifications
# Reset specified path
git checkout -- <local_path>
# Also works with mutliple arguments
git checkout -- <local_path1> <local_path2>
Undo local commit
# Undo the last 2 commits, keep changes in working tree
git reset HEAD~2
# Undo the last 2 commits, discard changes
git reset --hard HEAD~2
Remove a file from GIT without removing it locally
# Or git remove --cached <filename>
git reset <filename>
Edit a commit mesage
# Starts $EDITOR to edit
git commit --ammend
# Replace the message directly
git commit --ammend -m "New message"
Add file to previous commit
git add <forgotten_file>
git commit --ammend
Clean local commits before pushing
git rebase --interactive
# If you dont have any tracking information for this branch add an upstream
git rebase --interactive origin branch
Revert pushed commits
# Revert specific commit
git revert <sha-value>
# Revert the second to last commit
git revert HEAD^
# Revert range of commits
git revert develop~4..develop~2
# To revert the changed to the working tree only
git revert --no-commit HEAD
Advanced
Avoid repeated merge conflicts
git config --global rerere.enabled true
Locate merge that broke something
# Start the bisecting session
git bisect start
# mark the current revision as bad
git bisect bad
# Mark the last known good revision
git bisect good revision
Remove sensitive data
git filter-branch --force --index-filter \\
'git rm --cached --ignore-unmatch PATH-TO-YOUR-FILE-WITH-SENSITIVE-DATA' \
--prune-empty --tag-name-filter cat -- --all
Push to multiple git repos at the same time
# This does not preserve history!
git remote set-url origin --push --add <link_repo1>
git remote set-url origin --push --add <link_repo2>
git remote update
Make sure to push the changes!
Fix a broken repo!
# Run this and see what is danging etc
git fsck --full
# These should clean those things up
git reflog expire --expire=now --all
git gc --prune=now
# Run this again to see if we have fixed it
git fsck --full
Remove all broken regs from a repo
# make a backup before attempting this
git for-each-ref --format='%(refname)' | while read ref; do git rev-list --objects $ref >/dev/null || echo "in $ref"; done
(echo HEAD; git for-each-ref --format='%(refname)') | while read ref; do git rev-list -g --objects $ref >/dev/null || echo "in $ref"; done
Delete all files and history from a rep [UNRECOVERABLE]
# Create a new empty repo with an initial commit
mkdir new
cd new
echo "This is the README" > README.md
git init
git add .
git commit -m "Add README.md (initial commit)"
# Add remote repo [the repo you want to delete] as origin
git remote add origin <url-to-remote>
# Mirror push to remote
git push origin --mirror
# Note: There will be dangling ref's left over which you can then garbage collect later if you please
# see: https://mirrors.edge.kernel.org/pub/software/scm/git/docs/git-push.html
Clone history, tags, and branches to a new repo
# Clone repo1
git clone --mirror <link_repo1> repo1_dir
# We need to retrieve all the tags from repo1
git fetch --tags
# ensure your local branches and tags are correct
git tag
git branch -a
# Set the origin urls
git remote set-url origin --push --add <link_repo1>
git remote set-url origin --push --add <link_repo2>
# Update remote tracking
git remote update
# Update the repos
git push origin --all
git push --tags