IMPORTANT!
git config --global user.email janedoe@rocknrollrobots25.com"0. Note that the "--global" indicates that this applies to all your repositories. If you only want it set for a single repository, remove the "--global" option.
List remote repository URLs and their shortcut names
List all available branches
Add a new shortcut name for a remote repository URL
Create a new local branch that is the same as someone else's remote branch
git checkout -b <my_new_branch> <remote>/<branch_name>
If you haven't done it already, call craig's repository the shortcut name 'upstream'
Check difference between your local git directory and your remote repository (your https://github.com/<yourname>/<yourrepo.git> )
First do a fetch to download new commits from a remote repository into your local repository
Then do one of the following types of diff after you've done the fetch :
git diff remotename/branchname:remote/path/file1.txt local/path/file1.txt #
To view the differences going from the remote file to the local file. Note that "remotename/branchname", "remote/path", and "local/path", should be replaced with your real names and paths.git diff <my branch> origin/master -- <my relative path>
says to diff between what was last committed to your branch and origin/master. From the git-diff man page... git diff [--options] <commit> [--] [<path>...]
This form is to view the changes you have in your working tree relative to the
named <commit>. You can use HEAD to compare it with the latest commit, or a
branch name to compare with the tip of a different branch.
git diff [--options] <commit> <commit> [--] [<path>...]
This is to view the changes between two arbitrary <commit>.
You want to diff between the working copy and origin/master.
git diff origin/master -- <my relative path>
git diff --name-status <remote-branch> <local-branch>
Display the state of the working directory and the staging area. It lets you see which changes have been staged, which haven't, and which files aren't being tracked by Git
The git add command adds a change in the working directory to the staging area. It tells Git that you want to include updates to a particular file in the next commit.
The git commit
command commits the staged snapshot to the project history. In other words, after you add, modify, or delete a file, it doesn't really take effect until you do a "git commit"
After you've fetched origin master, you can merge it into your current branch
git merge branchname
# takes new commits from the branch branchname
, and adds them to the current branch.Fetch
git pull
is basically the same as git fetch; git merge origin/master
.Steps to take if you want to make a new change in your local repository
git checkout -b yourNewBranchName)
Steps to take if you want to take a branch from someone's github repository
git remote add theirusername https://github.com/theirusername/reponame.git
git fetch theirusername
git checkout -b mynamefortheirbranch theirusername/theirbranch
Different kinds of GIT diffs
git diff --summary FETCH_HEAD
) tells git to diff the working directory files (includes uncommitted files) against the FETCHed branch's HEAD and report the results in summary format. Summary format gives an overview of the changes, usually a good way to start@
is HEAD
, which is where you are now (committed changes), so you are comparing HEAD
with the upstream your branch is tracking. You can use @{push}
instead of upstream to get diff between the branch you are set to push to. The HEAD in Git is the pointer to the current branch reference, which is in turn a pointer to the last commit you made or the last commit that was checked out into your working directory. That also means it will be the parent of the next commit you do. It's generally simplest to think of it as HEAD is the snapshot of your last commit.Difference between local and remote repository
The git fetch command imports commits from a remote repository into your local repo without merging those changes into your own branches.
Difference between remote and local files
How to change the URL for origin
.git/config
and change the URLs thereWhat to do if delete file without "git remove" (now it is in repo, but not local. How to remove it from repo)
git rm --cached <list of deleted files>
How to Revert uncommited changes
git clean -i
for an interactive mode# Remove all untracked files and directories. (`-f` is `force`, `-d` is `remove directories`)
git clean -fd
git reset --hard
This command reverts the repo to the state of the HEAD revision, which is the last committed version. Git discards all the changes you made since that point.
Other times, you may want to only discard the changes to one file in the repo. You can do this with the checkout command:
git checkout -- path/to/the/file.txt
Use the checkout command with two dashes, then the path to the file for which you want to revert to its previous state.
git diff
(no parameters)
Print out differences between your working directory and the index (staging area).
git diff --cached
:
HEAD: the current commit your repo is on. Most of the time HEAD
points to the latest commit in your branch, but that doesn't have to be the case. HEAD
really just means "what is my repo currently pointing at".
Print out differences between the index and HEAD (current commit).
git diff HEAD
:
Print out differences between your working directory and the HEAD.
git diff --name-only
Show only names of changed files.
git diff --name-status
Show only names and status of changed files.
git diff --color-words
Word by word diff instead of line by line.
Check the difference between the working directory in my current branch against the working directory of another branch in a specific file? For example, if I'm working in branch A on file 1, I want to compare the difference with file 1 on branch B.
git diff branchA branchB -- file.cs
put the two names of your branches and optionally use the -- filename
option to specify one file
git diff branchA branchB
git diff branchA branchB file.cs
- diff on that 1 filegit diff branchA branchB some/path/in/rep some/other/path
- diffs two directoriesbuild.gradle
to the same version on the master
branch:git diff master build.gradle
Diff between different files on different branches
git diff branch1:full/path/to/foo.txt branch2:full/path/to/foo-another.txt
git difftool
and then drop the branch2:
and that will allow you to edit a file in the current working tree (to bring over changes from branch1 git difftool branch1:full/path/to/foo.txt full/path/to/foo-another.txt
git diff
--name-status branch1..branch2 // shows you which files differ between branch1 and branch2
git show TREEISH:path/to/file >path/to/local/file
Another method:
1) Ensure you're in branch where you need a copy of the file. for eg: i want sub branch file in master so you need to checkout or should be in master git checkout master
2) Now checkout specific file alone you want from sub branch into master,
git checkout sub_branch file_path/my_file.ext
here sub_branch
means where you have that file followed by filename you need to copy.
experiment's app.js
from experiment
to master
branch without merging git checkout master # first get back to master
git checkout experiment -- app.js # then copy the version of app.js
# from branch "experiment"
another way: git show experiment:path/to/app.js > app.js //
need to use the full path from the root directory of the repoBut, for git checkout
or git show
, you can actually reference any revision you want, as illustrated in the SO question "git checkout revision of a file in git gui":
$ git show $REVISION:$FILENAME
$ git checkout $REVISION -- $FILENAME
would be the same is $FILENAME is a full path of a versioned file.
$REVISION
can be as shown in git rev-parse
:
experiment@{yesterday}:app.js # app.js as it was yesterday
experiment^:app.js # app.js on the first commit parent
experiment@{2}:app.js # app.js two commits ago