Skip to content

Instantly share code, notes, and snippets.

@sudhakar6
Last active June 25, 2024 10:29
Show Gist options
  • Save sudhakar6/fc184ce56965b92a24c16a39a44c46a1 to your computer and use it in GitHub Desktop.
Save sudhakar6/fc184ce56965b92a24c16a39a44c46a1 to your computer and use it in GitHub Desktop.
The GIT commands not used frequently and forgettable
https://levelup.gitconnected.com/15-git-commands-you-should-learn-before-your-very-first-project-f8eebb8dc6e9
https://www.freecodecamp.org/news/learn-the-basics-of-git-in-under-10-minutes-da548267cc91/
https://www.freecodecamp.org/news/7-git-commands-you-might-not-know/
SETUP USER:
If one github account needs to setup all of the branches the commands can do that job.
git config --global user.email "[email protected]"
git config --global user.name "yourGitHubusername"
If a specific github account need to be setup for specific git repo we just need to ommit --global in the command
git config user.email "[email protected]"
git config user.name "yourGitHubusername"
IF YOU WISH TO CHECK YOUR CONFIGURATION SETTINGS: git config -l
UNDOING CHANGES:
git rm file_path (To remove a file from branch)
git checkout -- finepath_or_name (to undo uncommitted changes)
git checkout -- . (to undo uncommitted changes for all files)
git revert hash_number (undo committed changes)
git revert -n hash_number (before undoing committed changes, it will give you option to modify the file then you have to commit agian explicitly by using git commit )
git reset --hard hash_number (To reset branch to a particular old commit, you have to be very careful because all changes will be gone)
then do "git push origin branch_name -f " (to forcefully push that to origin => https://stackoverflow.com/questions/39389094/git-how-to-perform-hard-push)
git reset --mixed HEAD~1 (The "--mixed" option makes sure that the changes contained in the commits being reset are NOT discarded. Instead, they are preserved as local changes in the Working Copy.)
# Unstaging a file, but leaving its actual changes untouched
git restore --staged myFile.txt
# Discarding your local changes in a certain file
git restore myFile.txt
#Undoing all of the local changes in the working copy (be careful)
git restore .
#How to undo a merge( When you merge a branch in to another and you just want to undo it: https://careerkarma.com/blog/git-undo-merge/)
git reset --merge HEAD~1 (if your last commit is merge)
git reset --merge a9fdeb5( commit hash before merge)
HEAD : Reference to recently checkedout commit
git show HEAD (shows most recent commit changes)
git show HEAD~1 (shows one commit less than most recent commit, it goes on like HEAD~2, HEAD~3)
Detached HEAD:
git checkout hash_from_older_commits (Now it says You are in detached state)
NOW YOU JUST NEED TO CHECKOUT THE BRANCH TO SET THE HEAD TO MOST RECENT COMMIT git checkout branch_name
git checkout hash_from_older_commits file_path (To take the file to one of the older commits and add it)
MERGE:
git checkout other_branch_name file_path (to "merge" specific files from another branch to current branch => https://jasonrudolph.com/blog/2009/02/25/git-tip-how-to-merge-specific-files-from-another-branch/)
Then add, commit and push regular process goes on.
git merge other_branch -s ours --no-edit (To merge other branch in to current branch, it merges only spaces and new things and git history not the real changes)
git merge --abort (to abort the merge, some it is usefule when you face conflicts want to abort merge)
LOG:
(https://git-scm.com/book/en/v2/Git-Basics-Viewing-the-Commit-History)
git log -- file_name (To check the history of one file)
git log --since="3 weeks" -p index.html (Using "-p" makes sure you see the actual changes as diffs (and not only the commit's metadata). And the "--since" option helps you zero in on a recent time frame.)
git show commit_hash (To check what files modified in a commit)
#To show only list of files modified in a commit
git show --pretty="" --name-only 8bf86a6259b900ee1e9bc728bb92b291875b15cd
#To find out history for a deleted file
git log --all --full-history -- file_path (https://www.vogella.com/tutorials/Git/article.html#retrievefiles_finddeletedfile)
#Shows commit hash, abreviated commit hash, author email, author date, commitor date, commit message subject, frm a specific date
git log --pretty=format:"%H %h %ae %ad %cd %s" --since="2020-09-10"
REBASE/SQUASH:
(https://www.internalpointers.com/post/squash-commits-into-one-git)
To combine more commits into one(SHA => hash of the commit on after you want to create your squash commit. Ex: when you have commit a,b,c,d and you wanted to sqaush b,c,d into one combile then you should use 'a' here d is the latest a is the oldest)
git rebase --interactive SHA (git rebase --interactive 8bf86a6259b900ee1e9bc728bb92b291875b15cd)
after running above commnd git will ask you to choose what commits needs to be squashed, (press 'i' to get editable/iteractive mode)for those just remove pick and put s,
after that press 'esc' and ':wq' to come out of that window and run 'git rebase --continue'.
If something goes wrong:we can also abort the squash 'git rebase --abort'
{When squashing commits, you should squash recent commits to old ones not vice versa. To squash the second commit into the first one using git rebase, you must rebase both commits. Since there are only the two, git rebase -i alone won't work. Using git rebase -i --root will. However, since there are only the two commits, and the commit you want to squash is the current commit, you can do this without using git rebase at all. Simply use the command 'git commit --amend' to combine the two commits into one. After you've amended the commit, you can then push it to your remote repository as normal.}
Rebasing when you get "cannot-squash-without-a-previous-commit-error":
After running git rebase --interactive #hash
You need keep "pick" for the one commit to make it available for other commits merge on to it, you can change it to "s" for other commits.
Also remember squash commits picking screen all commits will be shown in a revers order to the order we commit.
Changing author of a commit by using rebase: (https://www.git-tower.com/learn/git/faq/change-author-name-email/)
git rebase -i -p 755db89d3e306f62c8dfddc95f1563b8415ff538(# of the commit that is before all the commits we want to change author)
above command will show you all the commits than remove 'pick' and put 'e' for edit
then run 'git commit --amend --author="SudhakarUsernameOfGitHub<[email protected]>" --no-edit'
then run 'git rebase --continue'
(You shoould run above 2 commands for all the commits you want change author and added 'e' in the rebase)
now git rebases all commits
CREATE A NEW BRANCH AT A SPECIFIC COMMIT OF OTHER BRANCH:
Go to the branch you want create branch from, and do git log and pick the hash
git checkout -b branchName hashOfTheCommitTheNewBranchToBeCreated
DIFF:
git diff other_branch_name -- myFile.txt (Finding Out How a File is Different in Another Branch)
git diff HEAD HEAD~1 (Shows the differnce between last two commits)
git diff --name-only SHA1 SHA2 (To show only the file names wich are changed between two commits)
git switch develop (https://www.freecodecamp.org/news/7-git-commands-you-might-not-know/)
git switch - (Switch Back and Forth Between Two Branches)
Rewriting Git History - Amend, Reword, Delete, Reorder, Squash and Split
(https://www.youtube.com/watch?v=ElRzTuYln0M&list=PLfU9XN7w4tFzXhl94ryX8zT06NNI-MJbg)
git commit --amend --no-edit
("after staging files, if you dont want to create a new commit and want to add them to the last commit.
It changes hash of last commit and add new file changes also to last commit by keeping last commit message")
git commit --amend -m "New commit message." ("To update last commit message")
https://linuxize.com/post/change-git-commit-message/
RENAMING A FILE IN THE BRANCH:
$ git mv old_filename new_filename
CREATING ALIAS:
git config --global alias.ac "commit -am"
commiting with above alias: git ac "Used alias to commit this"
PUSH:
git push -f <remote> <brannch> (To force push any local branch to remote: git push -f origin master | https://stackoverflow.com/questions/10510462/force-git-push-to-overwrite-remote-files)
CONFIGURING different work and personal github accounts on your local repositories with ssh authentication: https://www.darraghoriordan.com/2021/05/04/configure-multiple-github-accounts-one-computer/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment