git rebase <branch_name>: moves the base commit for the current branch to start from the given
branch-name. This is usually followed by a merge operation with branch-name which would result in a fast-forward merge operation.
git resetdo?: StackOverflow
git resetmoves the current branch to another position. And it can optionally updated the staging area and working directory.
git reset --hard: you loose your work (uncomiited changes); it doesn't change branches; modifies your work tree and loose your work.
git reset --hardis equivalent to
git reset --hard HEAD.
git resetis equivalent to
git reset --mixed; it resets the staging area (index) but not the working tree. Use this when you realize you made some bad commits but you want to keep all the work you have done so you can fix it up and recommit.
git reset --softdoesn't touch the index or the working-tree.
git reset --mergewas added recently; and is intended to help you abort a failed merge.
HEAD~is short for
HEAD~1and means the commit's first parent.
HEAD~2means the commit's first parent's first parent. Think of
HEAD~nas "n commits before HEAD" or "the nth generation ancestor of HEAD".
HEAD^1) also means the commit's first parent.
HEAD^2means the commit's second parent. Remember, a normal merge commit has two parents - the first parent is the merged-into commit, and the second parent is the commit that was merged.
git reset: schacon.github.io
git branch -d branch_name: to delete a local branch. The
-doption is an alias for
--delete, which only deletes the branch if it has already been fully merged in its upstream branch.
git branch -D branch_name: Also used to delete a branch (but forcefully). Use
-D, which is an alias for
--delete --force, which deletes the branch "irrespective of its merged status".
git push remote_name --delete branch_name: To delete a remote branch.
git push origin --delete feature1: deletes a remote branch pointed by
originwith the branch name
git fetch: StackOverflow
fetch, Git gathers any commits from the target branch that do not exist in your 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 master branch, you use
git rm --cached file1: to remove
file1from staging area. You could also get similar result using
git reset HEAD file1.
git branch -m oldName newName: to rename a git branch frol
git commit --amend: changing the most recent commit message
git commit --amend -m "New commit message": changing the most recent commit message in a sinlge line.
git log?: StackOverflow
Git logis a record of your commits. The
reflog, as the Pro Git book states, is a record of your
references(basically, your branch pointers and your
HEADpointer), and which commits they have been pointing at. On a side note,
git logcan also show you reflog information, but you have to pass a special option flag as an argument to it,
git reflog show <branch_name>: Git reflog for a specific branch.
Submodulesare Git repositories nested inside a parent Git repository at a specific path in the parent repository’s working directory
.gitmodulesfile located at the root of the parent repository.
git submodule update --init --recursive: if you want to load submodules for the current repository.
git submodule add -b master <url>: add a submodule to your current repository and track the
masterbranch of that submodule.
git submodule updatecommand to set the submodules to the commit specified by the main repository. This means that if you pull in new changes into the submodules, you need to create a new commit in your main repository in order to track the updates of the nested submodules.
git submodule deinit -f -- mymodule
rm -rf .git/modules/mymodule
git rm -f mymodule
git commit -m "comment"
git log: shows the commit history
git add .: add all new and modified files to our staging area.
git diff: to find the diff between the files in the working tree and the staging area.
git diff --staged: find the diff between the files in the staging area and our most recent commit.
git diff <branch1..>..<branch2>: to find the diff between branch1 and branch2
git rm <file_name>: remove a file from git (removes both from the working tree and the staging area).
git checkout -- s1: replace the working tree file s1 with the contents of the s1 file from the staging area.
git reset head s1: replace the s1 in the staging area with the s1 from the commit history.
git checkout 9ddb -- s2: get the file s2 from the commit 9ddb9 to both the staging area and the working tree
.gitignore: contains the files and the folders that you don't want the git to track. You will have to commit the
.gitignorefile to the git repo like any other file.
git stash: save the state of the working directory and the staging area (index) temporarily.
git stash save "message": stash the changes with a useful message.
git stash list: to see the list of current stashes.
git stash list -p: see the list of stashes and also observe the changes occured in each of the stashes.
git stash apply: reapplies the changes from the most recent stash
git clone <url>: to clone a remote repository on to our local machine.
git clone -depth=1 <url>: Clone the repository located at url and only clone the history of commits specified by the option depth=1. The clone of the url is made and only the most recent commit is included in the new cloned Repo. Shallow cloning is most useful when working with repos that have an extensive commit history.
git config --local user.name <name>
git config --local user.email <email>
git remote: displays the names of the remote repositories in our system.
git remote -v: displays the names as well as the full location of the remote repositories in our system.
origin/masteris a specialized remote tracking branch whose purpose is to tell us what the
masterbranch looks like at the origin.
origin/master) is not the same as the standard local branch (like
master). For example, if we check out the remote tracking branch with the command
git checkout origin/master, it would result in detatched head.
git fetch origin: checks for any updates in the remote repository pointer by
git merge origin/master: merge our master branch with the
masterbranch of the remote repository.
git pull: combines the
git mergeinto a single command.
git push origin master: push the changes made on the local repository into the
masterbranch of the remote repository. Keep in mind, this push relies on authentication (like using ssh keys).
git remote add upstream <url>: adds a git remote repository by the name upstream connected to the specified url.
git remote remove upstream: to remove a remote repository by the name
git fetch upstream: checks for any updates from the remote repository
git branch -a: to display both local and remote branches.
git branch -r: display only remote tracking branches.
.gitignorewill prevent untracked files from being added to the set of files tracked by git, however git will continue to track any files that are already being tracked.
git add the_file(s).
git rebase --continueor whatever command git said to do when you completed.
master. For both remote and local computer.
originis a custom and most common name to point to remote.
origin/masteris just a pointer to refer
masterbranch in the remote repo.
.bin) to source control.
git reset head -- <file_name>
git reset head -- <folder_name>
git reset --hard HEADand
git checkout .: StackOverflow
git checkoutcommand: Atlassian
git checkout -b <new_branch_name> <existing-branch>: By default
git checkout -bwill base the new-branch off the current
HEAD. An optional additional branch parameter can be passed to git checkout. In the above example,
existing-branchis passed which then bases
existing-branchinstead of the current HEAD.
git checkout master~2 Makefile: reverts the
Makefileto two revisions back. This wouldn't update the
HEADin anyway. But it would update the
Makefileboth in the staging area as well as in the working tree.
git checkout hello.c: restore
hello.cfrom the index.
git diff: Changes in the working tree not yet staged for the next commit.
git diff --cached: Changes between the index and your last commit; what you would be committing if you run "git commit" without "-a" option.
git diff HEAD: Changes in the working tree since your last commit; what you would be committing if you run
git commit -a.
git diff test: Changes in the working tree is compared with the tip of the
git diff HEAD -- ./test: Compare changes in the current working tree with the tip of the current branch, but limit the comparison to the file
git diff HEAD^ HEAD: Compare the version before the last commit and the last commit.
masterbranches (both commands would give the same result):
git diff topic master
git diff topic..master
git diff topic...master: Changes that occurred on the master branch since when the topic branch was started off it.
Head. It is a pointer to a branch. Since
headpoints to a branch and not directly to a commit, it is sometimes called a symbolic pointer. In Git terminology, the
headpointer tells us what we have checked out. By default, the
headpointer points to the
Masterbranch and it tells us that we have checked out the
git log --all --decorate --oneline --graph: to the git commit history as a graph.
alias graph="git log --all --decorate --oneline --graph"
git branch <branch_name>: create new branch at the location where the
git branch: to show all our branches
git checkout <branch_name>: to move to a different branch. It moves the
headto point to the new branch.
masterbranch to some other branch we want to merge.
git branch --merged: shows which branches have been merged with the current branch.
git branch -d <branch_name>: to delete a branch.
git merge <branch_name>: merges the current branch with the given branch. It could either be a fast-forward merge or a 3-way merge.
git checkout -b dev: Equivalent to
git branch dev: create a new branch by the name
git checkout dev: move to the
devbranch just created.
git commit -a -m "commit message": Equivalent to running:
git addon all the filenames that existed on the latest commit.
git commit -m "message"
git merge --abort: to abort the merge process when you get a conflict. When we have a merge conflict while trying to merge, the
git statuscommand will show the message "You have unmerged paths"
headpoints to a branch which in turn points to a commit. But when the
headpoints to a commit directly, we have the detached head problem. This is what happens when we do
git checkout <commit_hash>.
git checkout <some_branch>which will make the
headpoint again to a valid branch.
headpoints to currently and make the
headpoint to that branch.
create branch Branch1
git checkout Branch1
headpointer is no longer detached using
git log --all --decorate --oneline --graph
3-way-mergeadvantageous over a
git clone --recurse-submodules -j8 <url>
git submodule update --init --recursive: To update the submodules for already clone repositories.