- git config on GNU-Linux with details on how to set up ssh
- mysys git makes gnu bash available on windows
I use git both on windows and on Ubuntu / Debian GNU-Linux.
The commands I've used to upload content to github.com/paul4forest/forestproductsdemand are:
git remote add origin https://github.com/paul4forest/forestproductsdemandAlternatively "git commit -a"" is a replacement for "git add"" and "git commit". What is the difference between pull and clone: "I like to think of 'clone' as "make me a local copy of that repo" and 'pull' as "get me the updates from some specified remote."
git pull origin master
git commit -m "Explanatory message"
git push origin master
The commands to setup a fresh repository from bitbucket :
mkdir /path/to/your/projectSee also this discussion on why do I need to set upstream?
git remote add origin ssh://email@example.com/username/bbreponame.git
# Upload and set the local changes as upstream
git push -u origin master
Commands to copy an existing repository from bitbucket :
git clone firstname.lastname@example.org/username/bbreponame.git
Go back in timeDisplay the modification log
git logDisplay the log of a particular branch (after a fetch for example)
git log origin/masterDisplay a compact log for one file or one directory only
git log --abbrev-commit --pretty=oneline path_to_fileIdentify the commit identity in the log and copy its sha number. Then to go back to this state for the whole folder:
git reset --hard commit_shaTo go back to this state for only one file, see git checkout
git checkout commit_hash path_to_file/file_nameNo commit hash to get to get the file back to the latest commit.
Chekout the older revision of a file under a new name
git show commit_sha:filename > new_file_nameSee also alias and git grep below.
HelpGet help on a command (will start a web browser):
git init --help
Configure user name and emailDisplay your user name, email and remote repositories
git config -lTo change username and email
git config --global user.name "Your Name"
git config --global user.email email@example.comSetting your email in git explains how to change the email for the current repository only.
BranchingTo start work in a new branch:
git branch new_branch_nameTo compare a file between 2 branches:
git checkout new_branch_name
git diff branch1 branch2 file_nameTo merge changes back to the master branch:
git checkout masterIf there were conflicts, they will be presented in this way:
git merge branch1
"The area where a pair of conflicting changes happened is marked with markers
>>>>>>>. The part before the
=======is typically your side, and the part afterwards is typically their side."
I might need to delete a branch at some point:
git branch -d branchnameDelete a remote branch (stackoverflow question)
git push --delete origin tempDeleting your master branch.
If I am on a detached head, it is recommended to create a temporary branch (stackoverflow).
git branch tempDelete uncommitted changes in current working directory:
git checkout temp
git add -a
git commit -m "description of changes"
git checkout master
git merge temp
git checkout branch_name .See also below git clean.
Add minor change to the previous commit (git commit --amend):
git commit --amend
TaggingCreating an annotated tag
git tag -a v1.4 -m 'my version 1.4'You can add a tag after the fact. To tag an earlier commit, specify the commit checksum or part of it:
git log --pretty=onelineDelete a tag
git tag -a v1.2 -m 'version 1.2' 9fceb02
git tag -d tag_name
A regular push command won't push a tag (bitbucket), to push all your tags :
Display changesTo view modified files that have not been committed and to view commit history you can use:
git statusShows the changes between the working directory and the index.
git log --pretty=oneline
Shows the changes between the index and the HEAD
git diff --cachedShows all the changes between the working directory and HEAD
git diff HEADThe 3 lines above were copied from this question on git diff.
Show when the tip of branches have been updated
Alternatively, call the repository browser with:
gitkTo view a shorter version of the log file, and get an idea at where I am in the history:
git log --graph --decorate --all --pretty=onelineYou can define an alias for git log as explained be Fred here:
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr)%C(bold blue)<%an>%Creset' --abbrev-commit"The new alias can then be used with
git lgUse tags to specify important points in history, such as software versions.
Working with filesGet back a file to the last commit
git checkout path_to_file/file_nameGet back a file to a previous commit, using the commit hash
git checkout 4fb987f175210c09daaa4d0240070ffc9641120b path_to_file/file_nameRename a file
git mv file_name file_name_newChange the case of a file on a windows FAT 32 system:
git mv load.r load2.RSometimes the vi editor starts. To exit the vi editor:
git mv load2.R load.R
ESCIf a file or folder has been renamed outside of git, I get this warning:
$ git add .Therefore I think I should always run "git add --all "
warning: You ran 'git add' with neither '-A (--all)' or '--ignore-remo
whose behaviour will change in Git 2.0 with respect to paths you removed
Paths like 'docs/efi/efi_logo_rgb_small_siw.jpg' that are
removed from your working tree are ignored with this version of Git.
* 'git add --ignore-removal
', which is the current default,
ignores paths you removed from your working tree.
* 'git add --all
' will let you also record the removals.
Show what will be deleted with the -n option:
git clean -f -nThen - beware: this will delete files - run:
git clean -fAlternatively clean in interactive mode:
git clean -i
Search textSearch all files in the subdirectory "subdir" for lines containing the words "factor" and "item". Show 2 lines of context (2 leading and 2 trailing lines).
git grep -e item --and -e factor -C 2 -- subdir/Stackoverflow: How to search committed code in the git history?
Bulk replace stringsUse git grep to replace strings in many files in the directory :
git grep -l 'original_text' | xargs sed -i 's/original_text/new_text/g'
Save local modification temporarily
# I had edited the current file in between so needed to use
git stash # save local modifications away
git checkout __commit__hash__
# do some stuff in there ...
# Get back to most recent version of the code
git checkout branch_name
git stash pop # reload local modifications
.gitignoreTo ignore all files in a folder but not the folder itself. Put this .gitignore into the folder, then
git add .gitignore
/* !/foo /foo/* !/foo/bar
RemoteWhen a repository is connected to several remote repositories, to change the default git remote, push with :
git push -u origin masterThen later push of that branch to that remote can be made simply with:
Another command without specifying the remote and the branch
$ git push -u
fatal: The current branch master has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin master
After I run this set upstream flag, I can push to the remote server. Then I get this message
[...]I'll have to figure out what this does.
* [new branch] master -> masterBranch master set up to track remote branch master from origin.
Using the gh-branch to publish project documentation on githubSO Answer to the question "How to add a git repo as a submodule of itself? (Or: How to generate GitHub Pages programmatically?)": An alternative to using Git Submodules to generate GitHub Pages is to use Git Subtree Merge Strategy.
In fact I didn't use quite that strategy and I instead cloned a temporary copy of my repository. Created the gh-page branch. Pushed it to github. Then I went back to the original repository (where I have a few large untracked data files I find handy to keep for analyses purposes).
Then within the inst folder, I cloned only the gh-branch. To clone only one branch:
Then I renamed the folder to "web", so that I had a inst/web folder, tracking the gh-branch. inst/web is ignored in the main repository.
git clone -b mybranch --single-branch git://sub.domain.com/repo.git
- Power Your Workflow With Git (towards 43 minutes, an animation of branches and merging) A recommendation if you work in a team, don't use pull, but use fetch + merge.
- Getting Started with GitHub + Git