Skip to content

Instantly share code, notes, and snippets.

@dibyanshusinha
Last active March 13, 2023 11:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dibyanshusinha/abdc3365d6ca5b98d416e0bbc06a5fef to your computer and use it in GitHub Desktop.
Save dibyanshusinha/abdc3365d6ca5b98d416e0bbc06a5fef to your computer and use it in GitHub Desktop.
Git Commands

Submodules:

git submodule update --init --recursive

CHERRY-PICKING

git fetch remote-name

git cherry-pick -x 8989ee0 (-x to keep a ref of the existing commit from where we are cherry-picking)


git pull is like running git fetch then git merge git pull --rebase is like git fetch then git rebase


SQUASHING

git rebase -i HEAD~n git rebase -i shaofcommit from where you want to start

Get into interactive mode by typing "i" pick oldest commit let it be as it's with pick prefixed For all below replace pick with s

:wq enter to save and quit or :x to exit

Next write the comment that you want to have in the next screen


  1. git commit
  2. git fetch remote-name
  3. git rebase remote-name/branch
  4. Force push

you may reorder if needed

REVERT VS RESET Reset deletes your commit history revert adds one to reach a prev state git reset --hard 0ad5a7a6

Force to a previous Commit git reset --hard -----> Local git push -f origin branchname -----> Push to Remote git log ---------> to get sha


git revert 8f937c6 git push origin branch

In case last commit was merge commit 8f937c683929b08379097828c8a04350b9b8e183 Merge: 8989ee0 7c6b236

git revert 8f937c6 -m 1 will get you the tree as it was in 8989ee0, and git revert -m 2 will reinstate the tree as it was in 7c6b236.


Let's say the bad commit (dddf) is not the top commit, but a slightly older one, e.g. the second last one. We want to remove it, but keep all commits that followed it. In other words, we want to rewrite the history and force the result back to current state.

git rebase -i dddf^ This will open an editor and show a list of all commits since the commit we want to get rid of and then force push


Fix a typo in one of the commits

This works almost exactly the same way as prev case, but instead of removing the line with the bad commit, simply replace its pick with edit and save/exit. Rebase will then stop at that commit, put the changes into the index and then let you change it as you like


A list of my commonly used Git commands

If you are interested in my Git aliases, have a look at my .bash_profile, found here: https://github.com/joshnh/bash_profile/blob/master/.bash_profile

--

Getting & Creating Projects

Command Description
git init Initialize a local Git repository
git clone ssh://git@github.com/[username]/[repository-name].git Create a local copy of a remote repository

Basic Snapshotting

Command Description
git status Check status
git add [file-name.txt] Add a file to the staging area
git add -A Add all new and changed files to the staging area
git commit -m "[commit message]" Commit changes
git rm -r [file-name.txt] Remove a file (or folder)

Branching & Merging

Command Description
git branch List branches (the asterisk denotes the current branch)
git branch -a List all branches (local and remote)
git branch [branch name] Create a new branch
git branch -d [branch name] Delete a branch
git push origin --delete [branch name] Delete a remote branch
git checkout -b [branch name] Create a new branch and switch to it
git checkout -b [branch name] origin/[branch name] Clone a remote branch and switch to it
git branch -m [old branch name] [new branch name] Rename a local branch
git checkout [branch name] Switch to a branch
git checkout - Switch to the branch last checked out
git checkout -- [file-name.txt] Discard changes to a file
git merge [branch name] Merge a branch into the active branch
git merge [source branch] [target branch] Merge a branch into a target branch
git stash Stash changes in a dirty working directory
git stash clear Remove all stashed entries
git update-index --assume-unchanged path/to/file.txt Stops watching changes locally
git update-index --no-assume-unchanged path/to/file.txt Starts watching changes localls

Sharing & Updating Projects

Command Description
git push origin [branch name] Push a branch to your remote repository
git remote add [any_alias_for_remote_fork_repo] [fork_repo_url] git push origin [branch name] Add a new remote fork to pull changes from a fork/diff remote
git pull [the_alias_for_remote_fork_repo] [branch name] Pull changes from a diff remote repository to the current branch
git push -u origin [branch name] Push changes to remote repository (and remember the branch)
git push Push changes to remote repository (remembered branch)
git push origin --delete [branch name] Delete a remote branch
git pull Update local repository to the newest commit
git pull origin [branch name] Pull changes from remote repository
git remote add origin ssh://git@github.com/[username]/[repository-name].git Add a remote repository
git remote set-url origin ssh://git@github.com/[username]/[repository-name].git Set a repository's origin branch to SSH

Reset to Upstream What if things are out of whack and you just want to reset your branch to the upstream version, losing anything that may be committed to your fork that you don't intend to pull request upstream? Follow these steps, originally described here:

ensures current branch is master

git checkout master

pulls all new commits made to upstream/master

git pull upstream master

this will delete all your local changes to master

git reset --hard upstream/master

take care, this will delete all your changes on your forked master

git push origin master --force

Inspection & Comparison

Command Description
git log View changes
git log --summary View changes (detailed)
git log --oneline View changes (briefly)
git diff [source branch] [target branch] Preview changes before merging
git reflog View recorded references to all changes to the git
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment