Difference between revisions of "Git command summary"
m (→Branching) |
m (→Branching) |
||
| Line 52: | Line 52: | ||
$'''git fetch''' | $'''git fetch''' | ||
$'''git checkout''' branch-name | $'''git checkout''' branch-name | ||
| + | |||
| + | Creating a new branch with the current local changes: | ||
| + | $'''git checkout -b''' new-branch-name | ||
===Renaming a branch=== | ===Renaming a branch=== | ||
Revision as of 13:28, 7 November 2017
Configuration
Editing the local configuration (.git/config):
$git config --edit
Editing the global configuration (~/.gitconfig):
$git config --global --edit
Editing the system configuration ($(prefix)/etc/gitconfig):
$git config --system --edit
Retrieve the locations (and name value pairs) of all git configuration files:
$git config --list --show-origin
Retrieve the location (and name value pairs) of the system git configuration file:
$git config --list --system --show-origin
Retrieve the locations of all git configuration files (it fails when there's a space in the file name):
$git config --list --show-origin | awk '{print $1}' | uniq
Set git up to use Sublime Text
Create a batch/bash script that executes Sublime like this:
$subl -new-window -wait parameters to script
-new-windowmakes Sublime open a new window for the edit.-waitmakes sure that Sublime has focus until you close the file, this means that the script will start Sublime and wait, in stead of starting Sublime as a sub-process and then go on.
Make git call the script file (I assume it's not in your path):
$git config --global core.editor script incl. path
Cloning
Clone a GitHub project:
$git clone URL from GitHub
This will create a directory for the cloned code in the current directory, and pull all the code from GitHub and into the directory.
Branching
List branches:
$git branch
Creating a branch:
$git branch branch-name
Switch branch:
$git checkout branch-name
Deleting a branch:
$git branch -d branch-name
Synchronize branch list (-p or --prune will remove any branches that no longer exist on the remote):
$git fetch -p
Switching to a new branch that was created remotely:
$git fetch $git checkout branch-name
Creating a new branch with the current local changes:
$git checkout -b new-branch-name
Renaming a branch
Renaming a branch locally and remotely is done in these 3 steps:
1. Rename the local branch to the new name
$git branch -m old-name new-name
2. Delete the old branch on remote - where <remote> is eg. origin
$git push <remote> --delete old-name
3. Push the new branch to remote
$git push <remote> new-name
Get newest files
Synchronize with repo:
$git fetch
Get newest files:
$git merge
Synchronize and get in one command:
$git pull
Remove file(s)
Remove a file from both the git repo and the file system
$git rm filename
Remove a file from the git repo only, keeping it on the file system
$git rm --cached filename
Remove changes
Remove changes to the current branch:
$git reset --hard HEAD
Remove changes to a single file:
$git checkout -- filespec
Throw away local commits
$git reset --hard origin/branch_name
Staging
This should be done for both new and changed files
$git add file-name
Please also see the section on Committing for an alternate method when no new files have been added.
Merging
If changes has been made to the master which you want to merge into your branch:
$git merge master
Committing
Commit changes:
$git commit -m "Commit message goes here."
If you only have changed files and no new added files, you can do this without adding the changed files:
$git commit -am "Commit message goes here."
Push committed changes:
$git push
Diffing
To see a list of changes made to the files on disk:
$git diff
To see a list of changes made to the files on disk, ignoring differences in white spaces at the start and end of the line:
$git diff -w
To see a list of changes that have been staged:
$git diff --cached
Update changed code
To update code that has changed on the master branch, you need to 'stash' the changed code, pull the updated code and 'unstash' the stashed code. You may also have to switch branches in the process:
$git stash $git checkout master $git pull --rebase $git checkout branch-you-came-from $git stash apply
Practical aliases for your .gitconfig file
Diff while ignoring starting and ending white spaces:
dif = diff -w
Get a graph overview of how the various branches and commits connect chronologically:
lola = log --graph --decorate --pretty=oneline --abbrev-commit --all
Setting up Sublime Text as the git editor
Windows
Create a .bat-file (C:\bin\subl.bat) that executes Sublime Text
@"c:\Program Files\Sublime Text 3\subl.exe" -new-window -wait %1 %2 %3 %4 %5 %6 %7 %8 %9
Set up git to execute the .bat-file by inserting this in the global configuration file
[core] editor = C:/bin/subl.bat
A good start for a .gitignore file
# Compiled source # ################### *.com *.class *.dll *.exe *.o *.so # Packages # ############ # it's better to unpack these files and commit the raw source # git has its own built in compression methods *.7z *.dmg *.gz *.iso *.jar *.rar *.tar *.zip # Logs and databases # ###################### *.log *.sql *.sqlite # OS generated files # ###################### .DS_Store .DS_Store? ._* .Spotlight-V100 .Trashes ehthumbs.db Thumbs.db