githubEdit

GIT

Initialize Repository

  • git init

  • git config user.mail

  • git config user.name

Setting name and email globally

git config --global user.name "Your Name"
git config --global user.email [email protected]

After doing this, you may fix the identity used for this commit with:

git commit --amend --reset-author

Check status, add files and Commit

git add .
git commit -m "<comment>"
git status
git remote -v

Add Remote Repository

If remote branch is main

Update Remote Repository/Change from HTTPS to SSH and vice versa

Git Credential

Remove Outdated branch (deleted remotely) from local database

Remove settings

Git Log and Git show and other commands to view files at specific commit

  • git log: view commits

  • git show $commit:$path/to/file: view content of file at commit

  • git ls-tree -r $commit --name-only: view list of all files in that commit

  • git checkout $commit: move back to specific commit

    • remember to move back to current commit: git checkout main (or any other main branch)

  • git show --name-only [$commit]: view what files changed at specific commit

  • git diff $commit1 $commit2 [--path/to/file]: view difference between 2 commit, whole repo if file path is not provided

  • git diff $commit [--path/to/file]: view difference between commit and current working tree

    • HEAD: current commit

    • HEAD~1: commit before current commit

    • HEAD~2: 2 commits before current commit

Configure Proxy for Git

Remove files from previous commit which was commited

Use git show to find which commit has that file

After finding which commit has file.txt, reset to 1 commit before that so that it doens't have that file:

  • git reset HEAD~2

Add all current file to git and commit, then push to github as usual

  • git add .

  • git commit -m "Some message"

Undo changes

  • git restore $file: Restore files that wasn't stage

  • git reset $file: remove files from staged list

  • git log –oneline

    • git log –branches=*

  • git ls-files: list staging index

    • git ls-files -s: ls file in staging sate

  • git commit --amend: with staging changes, this will have Git open the configured system editor and let you modify the last commit message. The new changes will be added to the amended commit.

Undo a commit with git checkout

  • git checkout: undo a commit

  • The git checkout command simply updates the HEAD to point to either the specified branch or commit. When it points to a branch, Git doesn’t complain, but when you check out a commit, it switches into a “detached HEAD” state. In a detached state, any new commits you make will be orphaned when you change branches back to an established branch. Orphaned commits are up for deletion by Git’s garbage collector. The garbage collector runs on a configured interval and permanently destroys orphaned commits. To prevent orphaned commits from being garbage collected, we need to ensure we are on a branch by using git checkout -b $newbranch. to create a new branch and switch to that state. The repo is now on a new history timeline in which the previous commit no longer exists. At this point, we can continue work on this new branch in which the previous commit no longer exists and consider it ‘undone’. Unfortunately, if you need the previous branch, maybe it was your main branch, this undo strategy is not appropriate

  • git checkout $commithash: change HEAD to commit

  • git checkout $branch: change branch

  • git checkout -b $newbranch: new branch from current branch (current HEAD)

  • git checkout -b $newbranch $oldbranch: new branch from an old branch

  • git fetch -all: switch to branch on remote repository, need to fetch it first

    • git checkout -b $branch or

    • git checkout -b $newbranch: create a new branch locally

    • git reset –hard origin/$branchname: and reset it to $branch on remote repo

Undo a commit with git revert

  • git revert: should be use for undo a public (shared) commit because it doesn’t delete any commit, but just create a new one with removed changes from last commit

  • git revert HEAD: create a new commit with the inverse of the last commit. This adds a new commit to the current branch history with the state is the state of commit before the previous commit (n-2). Unlike checkout strategy, the same branch continues to be used. This solution is a satisfactory undo. This is the ideal ‘undo’ method for working with public shared repositories

Undo a commit with git reset

  • git reset: should be considered for local repos

  • git reset $file: remove $file from staging index

  • git reset –hard $HashofCommit: It will reset to $HashofCommit and delete any later commits of this commit like it has never existed. Doing a reset is great for local changes however it adds complications when working with a shared remote repository, because if someone is working on a later commit of this commit, they will get an error when committing

  • git reset –mixed: The git add command is used to add changes to the staging index. A --mixed reset will move any pending changes from the staging index back into the working directory.

Undo a commit with git clean

  • git clean: operates on untracked files, not undo-able as git clean will make a hard filesystem deletion, similar to executing the command line rm utility

  • git clean -n: dry run git clean -f $patharrow-up-right: initiates the actual deletion of untracked files from the current directory

  • git clean -dn: also delete directory, dry run

  • git clean -x: remove file specified in .gitignore, by default just file not listed in .gitignore

  • git clean -i: interactive mode

Using 2 Git accounts

SSH Configuration

When pushing repository to github, may need to remove cached ssh-key so git can use the right key for authentication

Checking Log and Diff

  • git log [$commit]

  • git diff [$commit]: compare commit to latest commit

  • git diff: compare working directory with staged area

  • git diff --staged: compare staged changes with last commit

  • git log --follow --patch $pathtoonefile: check what was changed on specific file

Git Stash

  • Temporarily save changes in working directory without committing them. This is useful when there's a need to switch branches or work on something else without losing current modifications.

  • git stash [-m "message"] = git stash push [-m "message"]: save changes to stash

  • git stash list: list stashes

  • git stash apply [stash@{0|1}]: restore stashed change without removing it from stash list, [] is for specific stash

  • git stash pop: restore a stash and remove it from stash list

  • git stash drop stash@{0}: remove a specific stash from stash list

  • git stash clear: remove all stashes in stash list

  • Sample work flow:

Advanced Stashing

  • By default git stash only stashes tracked file (added with git add command) -> to include untracked files, which includes tracked and untrackec files, use: git stash -u

  • To include all files (tracked, untracked and ignored files), use: git stash -a

  • git stash branch fix-branch stash@{0}: create fix-feature branch with stashed changes applied

  • git stash push -m "Stash only this file" -- example.txt: stash specific file instead of all files

  • git stash --keep-index: stashes everything except staged files

  • git format-patch stash^{tree} --stdout | git send-email [email protected]: send stash to a team member, this let them view diffs or check out the state locally

  • Example workflow:

Reference

  • A successful git branching model: nvie.com/posts/a-successful-git-branching-model/

  • https://git-scm.com/book/en/v2/Git-Tools-Credential-Storage

  • https://www.heady.io/blog/how-to-manage-multiple-github-accounts

  • https://www.atlassian.com/git/tutorials/using-branches/git-checkout

  • https://www.atlassian.com/git/tutorials/undoing-changes

  • https://www.atlassian.com/git/tutorials/undoing-changes/git-reset

  • https://git-scm.com/docs/git-reset

  • Git Basics – Recording Changes to the Repository: https://git-scm.com/book/en/v2/Git-Basics-Recording-Changes-to-the-Repository

  • Appendix C: Git Commands – Basic Snapshotting: https://git-scm.com/book/en/v2/Appendix-C%3A-Git-Commands-Basic-Snapshotting

Last updated