A small collection of most frequentely used git
commands for your day-to-day work and common tasks.
master => THE main branch, should always represent the stable version of the project
remote => a branch on the GitHub remote server
origin => misleading term, it's just the default alias for the address of the
remote repository, i.e. https://github.com/lpacher/lae.git
. This URL is returned by git config --get remote.origin.url
% git <command> [options] [target]
Short help:
% git <command> -h
Complete manual page:
% git <command> --help
Example:
% git clone -h
% git clone --help
Some useful aliases and configurations:
% git config --global alias.root "rev-parse --show-toplevel" # create a "git root" alias to easily locate the Git top-level directory
% git config --global alias.co 'checkout' # allows to use "git co" instead of "git checkout"
% git config color.ui true # enable better display with colors
% cd Desktop
% git clone https://github.com/lpacher/lae.git [target directory]
NOTE: This is equivalent to:
% cd Desktop
% mkdir lae (or mkdir <target directory>)
% cd lae
% git init
% git remote add origin https://github.com/lpacher/lae.git
% git pull origin master
There are many different commands for this:
% git branch --remote
or
% git remote show origin
or
% git ls-remote
WARNING: Always fetch latest updates from the remote server with git fetch
before running these commands, otherwise new branches
created on the server will not be listed.
% git checkout <branch name>
WARNING: Be sure to commit all your local changes to tracked files before changing working branch !
% git branch
NOTE: The * indicates the current working branch.
% git checkout <branch name>
% git log [with many options available]
This shows you all modified files, committed files and untracked files:
% git status [--verbose]
Retrieve all updates (commits and new branches) published by other people into the remote server:
% git fetch
Use instead
% git fetch origin <branchName>
to fetch only from the specified branch.
NOTE: Fetching from a repository grabs all the new remote-tracking branches and tags without merging those changes into your own branches.
% git add /path/to/filename1
% git add /path/to/filename2
% git add /path/to/filename3
% git commit -m "Commit message for those 3 modified files"
% git add /path/to/filename4
% git commit -m "Commit message for the 4th modified file"
NOTE: You can add as many commits as you wants, also multiple-commits on the
same file for different changes. All commits will be pushed into remote repository after a git push
.
% git reset /path/to/filename
To undo git add .
use instead
% git reset
without the dot.
% git checkout -- /path/to/filename
In case your last-commit message contains typos/missing information and thus requires changes you can amend it :
% git commit --amend
Then enter the new commit messagge.
In case you already issued a git commit
but you want to add modified file(s) to the last commit :
% git add /path/to/filename [/path/to/another/filename]
% git commit --amend
If you don't want to change your commit message, you can run the --amend
command with the --no-edit
flag :
% git commit --amend --no-edit
% git push -f origin <branchName>
% git mv oldFileName newFileName
% git commit -m "Why you changed the file name"
WARNING: DO NOT USE a standard UNIX mv
on tracked files !
% git rm /path/to/filename
% git rm -r /path/to/directory
% git commit -m "Why you removed the file/directory"
WARNING: DO NOT USE a standard UNIX rm
on tracked files !
% git pull origin <branchName>
This is equivalent to:
% git fetch
% git merge
Git is smart-enough to try to merge all non-conflicting differences seen in a file that has been modified both by you and by someone else. However, in case of a merge conflict you have to fix it yourself. To do this, open the file/files for editing and manually fix conflicts enclosed between
<<<<<<< HEAD
Local version of the code creating the conflict
=======
Remote version of the code creating the conflict
>>>>>>> [other/branch/name]
After having fixed all conflicting lines, save the file and perform a git commit
to mark the conflict as resolved.
NOTE: There are also many text-based and GUI-based merging tools. Just call git mergetool
after having configured the tool to be used, e.g.
% git config --global merge.tool vimdiff
% git push origin <branch name>
To rename a branch, either use :
% git branch -m <newBranchName> # inside the branch you want to rename
or
% git branch -m <branchName> <newBranchName> # from any other different branch
To push this branch-renaming to the remote server, then use :
% git push origin :<oldBranchName> <newBranchName>
% git push origin -u <newBranchName>
% git checkout devel
% git branch -d <branchName> # removes your LOCAL branch <branchName>
% git push origin --delete <branchName> # removes the REMOTE branch <branchName>
WARN: You cannot delete a checked-out branch ! Change working branch before deleting your feature branch.
% git checkout master
% git pull origin devel
% git push origin master
% git tag -a <tagName> -m "Tag message"
% git push origin <tagName>