Skip to content

LearnFL/ref-git

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 

Repository files navigation

GIT reference

Created from multiple sources for personal use.

  1. 👨‍💻 Editer
  2. 📡 Initialize
  3. 🚀 Push
  4. 📸 Stage
  5. 📲 Stash
  6. 🏈 Clone-fetch-pull
  7. 🎣 Cherry-pick
  8. 🔎 Debug changes
  9. 🚨 Undo changes
  10. 🔐 Merge
  11. 📀 Rebase
  12. 🌴 Branch
  13. 🚦 Public to private Fork

Editer 👨‍💻

➡️ Save

Mac CTRLX or CTRLS to save.
Windows CTRLX or CTRLC to save
Linux CTRX (and Y to confirm) to save

Windows GIT Bash press i to enter inline insert mode. Type the description at the very top, press esc to exit insert mode, then type :x! (now the cursor is at the bottom) and hit enter to save and exit. If typing :q! instead, will exit the editor without saving (and commit will be aborted)

➡️ Commands

:qa! (exit and abort)
:wq (save and exit)
:w Enter (save)
:q (close file)

Initialization 📡

git init
git config --global user.name <user name used on github>
git config --global user.email <email used on github>
Create .gitignore (node_modules, .parcel-cache, dist, .env, .DS_store)

Pushing to remote 🚀

👉 Don't create readme.md when creating a repositary as it will cause a conflict.

git remote add origin <repo link>
git push origin master (or any other branch)
git push origin new-feature (if want to push all branches)
git push --force {remote} {branch} (DANGEROUS)
git push --force-with-lease {remote} {branch} (SAFE push with force)
git push --all (push all local branches)
git push -f <remote> <local branch>:<remote branch>
git push origin -u <newname> (push and set upstream)
git remote remove origin (Remove origin)
git remote remove <remote_name>
git remote set-url origin git://new.url.here (change origin)
git remote show <remote_name> (shows info about remote)
git remote -v (list all remotes)
git remote set-url (chnage remote url)

👉 After renaming local branch:

git push origin new_branch_name:master (now changes will go to master branch but your local branch name is new_branch_name)

Stage 📸

git add [file name] (Stage)
git add {file-name-1} {file-name-2} (stage multoiple files)
git add *.{file_extension} (only certain files)
git add . (stage all new and modified files AND NOT deleted files)
git add -A (stage all anew and mofified AND deleted files)
git add -p OR git add --patch (allows you to review each change made one at a time)
git commit -m “[descriptive message]” (Commit)
git commit {file-name-1} {file-name-2} -m "Add commit message here" (commit specific file)
git commit (opens VIM; to save commit: click Esc, type :wq, click Enter)
git commit --amend -m "Add the improved message here" (ammend previous commit)
git commit -am "Add message here" (stage and commit in one line)

git show <commit_hash> (shows info)
git status (Shows modified and staged files)
git diff (Diff of what is changed but not staged)
git diff --staged (diff of what is staged but not yet committed)

➡️ Output of patching

👉 Before we advance, let’s break down the output quickly:
diff --git a/planets.csv b/planets.csv - this means there are changes between two files; a) the original file, and b) the modified one
index 846b016..5739dbf 100644 - this uniquely identifies this specific change (basically an ID)
-- a/planets.csv - this is the original file of the planets.csv file
+++ b/planets.csv - this is the modified version of the planets.csv file
@@ -1,9 +1,9 @@ - this is the “hunk” of changes. A hunk is a portion of code that has been added or modified. So the hunk in this example would be the 9 lines in the CSV file (1 header, 8 rows).
In the -1,9 section, the 1 means the hunk starts at line 1 in the original version, and the 9 means the file contains 9 lines. The same principle applies to the +1,9 for the modified version. We can decide to advance with this patch by typing y into the terminal, or we can reject the changes by typing n. There are other options included but that will be for a future article, so stay tuned for that! By typing y, we advance to the next change that needs reviewing:

Stash 📲

Save a change in a temporary location:

In Git, stashing allows you to save changes in a temporary location and then resume when you’re ready to continue from where you left off with them. This is used mainly when you have changes that are staged, modified or even untracked that you’re not ready to commit yet even though you need to make other changes elsewhere e.g. in another branch. Stashing will make your working directory clean enough for you to work on different changes while you have others stored elsewhere.

git stash
git stash save {custom_name_for_stash} (shash with name)
git stash list (list stashes)
git stash apply (apply most recebt stash)
git stash apply {stash-id} (apply specific stash)
git stash drop
git stash drop {stash-id}
git stash pop (apply and delete)

Clone fetch pull 🏈

➡️ FETCH from remote branch WITHOUT merging with local branch:

Git fetch gathers any commits from the target branch that do not exist in the current branch and stores them in your local repository. However, it does not merge them with your current branch. This is particularly useful if you need to keep your repository up to date, but are working on something that might break if you update your files. To integrate the commits into your current branch, you must use git merge afterwards.

git fetch
git fetch {remote}
git fetch [alias] (fetch down changes from all the branches from that Git remote)
git diff master origin/master

➡️ PULL retrieves all changes AND merges them:

Git pull tries to automatically merge after fetching commits. It is context sensitive, so all pulled commits will be merged into your currently active branch. git pull automatically merges the commits without letting you review them first. If you don’t carefully manage your branches, you may run into frequent conflicts.

git pull (fetch and merge any commits from the tracking remote branch)
git pull {remote} {branch}
git pull --all
git pull --rebase (rebase while pulling)

➡️ CLONE:

git clone [url]

Cherry-pick 🎣

For picking an individual commit from one branch and pasting it into another As the name implies, git cherry-pick lets you select specific commits from one branch and apply them to another, without merging the changes from the source branch.

# Switch to the branch you want to apply the selected commits to
git checkout {target_branch}
# Apply the commit to the target/child branch
git cherry-pick {commit_hash}

Cherry pick multiple commits.

# Switch to the branch you want to apply the selected commits to
git checkout {target_branch}
# Apply the commits to the target/child branch
git cherry-pick {commit_hash_1} {commit_hash_2}

Cherry pick a merge commit from the source branch.

# Switch to the branch you want to apply the selected commits to
git checkout {target_branch}
# Apply the commits to the target/child branch
git cherry-pick -m {parent_no} {commit_hash}

Debug changes 🔎

git log --oneline (see commits)
git log --since="time period" (filter by time period)
git log --since="earlier date" --until="later date" (filter by date range)
git log --author={name_of_user} (filter by author)
git log --author="{name_of_user_1}|{name_of_user_2}" (filter by author)
git log -p OR git log --patch (Filter the log commits by multiple authors)
git log branchB..branchA (show the commits on branchA that are not on branchB)
git log --follow [file] (show the commits that changed file, even across renames)
git log --stat -M (show all commit logs with indication of any paths that moved)
git status
git ls-tree --full-tree --name-only -r HEAD (see all tracked files)
git worktree list (list working tree)
git show <commit_hash> (shows info)
git diff --staged (diff of what is staged but not yet committed)
git diff --color-words (to make it easier to see)
git diff --name-only (show names of modified files)
git diff --name-status (show names & status of modified files)
git diff --stat (stats of modified files)
git diff -p {commit_hash_1} {commit_hash_2} (compare)
git diff -patch {commit_hash_1} {commit_hash_2} (compare)
git diff branchB...branchA (show the diff of what is in branchA that is not in branchB)
git blame <file_name> (Shows who last modified each line) git grep "{string_of_text}" (search for a key word in repo)
git grep "{string_of_text}" {sub_directory_path}/ (search for a key word in sub dir)
git grep "{string_of_text}" {branch_name} (search for a key word in branch)
git grep --cached "{string_of_text}" (search for a key word in staging area)
git grep --untracked "{string_of_text}" (search for a key word in untracked file)
git grep -c "{string_of_text}" (Search for the number of matches for a term)

👉 REFLOG
Lists the changes made in Git
The git reflog command shows the list of changes made in the Git repository. This command is useful for recovering lost commits caused by merges, rebases and resets, among other actions. The main difference between the git reflog and git log command is that git reflog shows all the commits and changes of any branch within your repository, including the ones created, modified and deleted. git log only shows the complete commit history of the current (or specified) branch, and therefore doesn’t include commits from other branches or deleted ones.

git reflog
git reflog {branch_name}
git reflog -n {number_of_entries}
git reflog show --all (log for all references)
git reflog --pretty=oneline
git reflog --pretty=short
git reflog --pretty=medium
git reflog --pretty=full

Undo changes 🚨

CREATE COPY OF BRANCH FOR SAFETY, AND TEST STRATEGY!

REMOVE commit from remote -> reset local and force push.

➡️ Delete file or directory

git rm {file-name} (permanently delete file from git)
git rm -f {file_name}
git clean -n (Dry run for removing untracked files)
git clean --dry-run (Dry run for removing untracked files)
git clean -f (remove untracked files)
git rm -r --cached . (removes files from staging area/index and undoes ADD)
git rm -r {directory-path}/ (remove directory)
git rm --cached *.log (remove staged files with .log format from the index)

➡️ Unstage

git ls-tree --full-tree --name-only -r HEAD (Show tracked files)
git rm --cached {file_name} (Remove from staging, and undo ADD. Removes the copy of the file from the index / staging-area/ index, without touching the working tree copy. )
git rm -r --cached (removes from index, Undo ADD)
$ git rm --cachedand$ git update-index (temporary untrack file)
$ git update-index --assume-unchanged (temporary untrack file)
git restore --staged index.html (Undo ADD. Git copies the file from the HEAD commit into the index, without touching the working tree copy. The index copy and the HEAD copy now match, whether or not they matched before. A new commit made now will have the same copy of the file as the current commit.)

➡️ Uncommit

git reset [file] (Not commited file, unstage, but retain changes)
git reset --soft HEAD^ (Uncommit but keep changes)
git restore --staged

➡️ Restore a file(s) to original/previous state

Restores an existing file in the working directory to its previous state. It reverts all unstaged and uncommitted files in the working directory to the version they had in the last commit. This is useful for reversing accidental modifications made to files, similar to how Ctrl + Z is used. It’s also important to note that this does not affect the staging area or the commit history, so it is generally recommended to discard staged changes using git reset HEAD if you need to record the changes made in the commit history too.

git restore {file_name} (restore a single file)
git restore {file_name_1} {file_name_2}
git restore . (all files)

➡️ RESET takes commit you want to keep (deletes commit and will put this branch behind, will have to use push --force).

In Git, the git reset command is used to undo changes made in the working directory and/or staging area. This is achieved by the HEAD pointer moving to a previous commit, which means any changes since that commit will be permanently deleted.

git reset --soft {commit_hash} (Reset the commit history only (soft reset)🟢)
This reverts to the commit specified by the user while leaving the changes untouched in the working directory and staging area. In other words, this approach only changes the commit history and not the staging area or working directory.

git reset {commit_hash} OR git reset --mixed {commit_hash} (Reset the commit history and staging area only (mixed reset)🟡)

This undoes any changes to the commit history and the staging area by resetting the HEAD pointer to the previous (or specified) commit, and therefore will permanently wipe out all staged changes that have not been committed.

git reset --hard {commit_hash} (Reset the commit history, working tree and staging area (hard reset)🔴)

This approach resets any changes made to the commit history, staging areas and the working directory. This is done by resetting the HEAD pointer to the specified commit. ⚠️Warning: This could impact the work of other colleagues if you’re working on the same repository, so only use this if you know exactly what you’re doing, otherwise consider safer alternatives.

Article by Stephen David-Williams on medium

git reset HEAD^ (Uncommit and unstaged but keep my file tree)
git reset --hard HEAD^ (Reset changes to previous commit)
git push -f

➡️ REVERT takes commit you want to undo, creates an opposit commit and you can push.

git revert HEAD
git revert {commit_hash}
git revert -m {parent_number} {commit_hash} (Revert a merged commit)
git revert -i HEAD~n (Revert last commit. Change n to the number of your choice)

Strategy 1️⃣

Reset and use --force to push.

Strategy 2️⃣

Create a branch, use reset, then merge and push.

Strategy 3️⃣

Use revert and push.

EXAMPLE 1️⃣

👉 If not commited the change:

  1. cmd: git reset --hard HEAD (reverts changes to most recent commit)
  2. cmd: git checkout HEAD^ -- working_file (restore the file with the previous version)

👉 If committed:

  1. cmd: git log (shows all commits, choose desired commit an d type :q to exit log)
  2. cmd: git reset --hard <id of commit>
  3. cmd: git checkout -- filename.txt (undo single file)
  4. cmd: git restore filename.txt (undo single file)
  5. cmd: git checkout COMMIT_HASH FILE (undi sibgle file to previous commit)
EXAMPLE 2️⃣

git revert HEAD (creates new commit which is opposite of an existing commit)
git revert < commit number>
git revert first-bad-commit^..last-bad-commit

EXAMPLE 3️⃣: undelete file

git checkout deleted_file

EXAMPLE 4️⃣: Discard newly added files

git status
git clean -n (dry run to see files)
git clean -f (Removes untracked files)
git clean -fd (Removes untracked directory)

EXAMPLE 5️⃣: Rever changes made to working copy, not staged and not commited

git checkout . (or: git restore .)

Remote is behind (push desired commit)
git push --force origin 606fdfaa33af1844c86f4267a136d4666e576cdc:master

Merging 🔐

git checkout master
git merge new-feature (merges specified branch into your current branch)
it merge [alias]/[branch] (fetch down all the branches from that Git remote)
git reset --hard {merge_commit_hash} (reverse merging)
git revert -m {parent_number} {merge_commit_hash} (reverse by creating a new commit)

Using the git revert command by typing git revert -m 1 HEAD@{0} into the terminal opens up an interactive session via Vim like so:
The custom message provided by the system should suffice so we can advance by typing Esc + :wq and then hit Enter, which creates the new commit that reverses these changes.

Rebase 📀

Moves or combines a sequence of commits to a new base commit.
In Git, rebasing means integrating the changes made on one branch into another, similar to merging. But instead of maintaining the commit history of the two branches, rebasing rewrites the commit history to produce a cleaner, linear progression of changes. So rebasing is appending all the commits of one branch to the commits of another one. This makes the commit history of the principal branch a single, clean line of commits but may be difficult to debug if conflicts occur. ⚠️Warning: Use with caution as other developers may be working on the branches you’re rebasing with. Resolving conflicts caused by rebasing can be a painful experience (trust me…I know what I’m talking about)😖

git rebase {branch_name}
git rebase --continue (continue after resolving conflicts)

Branch 🌴

To create a branch locally and remote:
Create locally ➡ checkout to new branch ➡ push to remote.

git branch (shows all branches)
git branch new-feature (cerating new brtanch named new-feature)
git checkout -b (create new branch and switch)
git checkout new-feature (switching to a new branch)
git branch -r (show remote)
git branch -a (show all)
git branch --show-current
git switch other-branch
git switch - # Switch back to previous branch, similar to "cd -"
git switch remote-branch # Directly switch to remote branch and start tracking it

➡️ Rename local branch

git branch -m <newname>
git branch -m <oldname> <newname>
git branch -M <newname> (force rename)
git branch -m old_branch_name new_branch_name

After renaming local branch push like this:
cmd: git push origin new_branch_name:master (now changes will go to master branch but your local branch name is new_branch_name)

➡️ Rename remote branch

git push origin :old-name new-name (Delete the old-name remote branch and push the new-name local branch)
git push origin -u new-name (reset upstream)
push --set-upstream origin new_name (another way to set upstream)

➡️ Delete local branch

  1. cmd: git branch -d <branchname>
  2. cmd: git branch -D <branchname>

➡️ Delete remote branch

  1. git push <remote_name> --delete <branch_name>
  2. git push <remote_name> :<branch_name> (short command)

Public to private Fork 🚦

  1. Create empty private repo
  2. Bare clone a public repo, and mirror push it to the private repo.
$ git clone --bare https://github.com/exampleuser/public_repo.git
$ cd public_repo.git
$ git push --mirror https://github.com/yourname/private_repo.git
$ cd ..
$ rm -rf public-repo.git
  1. Register remote repository with public repositoryPermalink
$ git clone https://github.com/yourname/private_repo.git
$ cd private_repo
$ git remote add public https://github.com/exampleuser/public_repo.git
Fetching from public repoPermalink
$ git pull public master 
$ git push origin master

Releases

No releases published

Packages

No packages published