This is a simple guide of getting started. Commands can have a multitude of switches and be combined to make the most amazingly magical things. But this is more focused on "manual basic" stuff.
Download from https://git-scm.com/
Ensure to select option to:
- Use bash console and integrate in command prompt
- For line endings (if on Windows), "Commit Unix line endings, Checkout Windows" (
core.autorcrlf=true
)
git gui
opens a UI that you can use to rever changes, stage, commit etc. You can right-click and move parts of files into the staging area.
gitk
lets you inspect the history and to e.g. reset back to a certain commit. Tip: You can pass a specific reflog
e.g. branch
to it, e.g.: gitk develop
.
Under your "home directory", there's a .gitconfig
file. Open it with a text editor. Then hack away. Here is a sample that:
- Defines my user info
- Defaults to push my current branch
- Hooks in Notepad++ as my core editor (used e.g. for writing commit messages, doing interactive rebase etc)
- Hooks in my diff- and merge tool of choice (Beyond compare)
[user]
name = Daniel Wertheim
email = daniel@none.com
[push]
default = current
[core]
editor = 'C:/Program Files (x86)/Notepad++/notepad++.exe' -multiInst -nosession -noPlugin
autocrlf = true
[diff]
tool = bc4
[merge]
tool = bc4
[difftool "bc4"]
cmd = 'C:/Program Files (x86)/Beyond Compare 4/BComp.exe' \"$LOCAL\" \"$REMOTE\"
[mergetool "bc4"]
cmd = 'C:/Program Files (x86)/Beyond Compare 4/BComp.exe' \"$LOCAL\" \"$REMOTE\" \"$BASE\" \"$MERGED\"
trustExitCode = true
You can create custom aliases that you can use in git bash
. E.g. I used gs
as an alias for git status
.
You create aliases to bash by editing %USERPROFILE%\.bashrc
. My recommendation is to start learning the commands before going into aliasing everything. Some sample content:
alias cls=clear
alias gouser='cd $USERPROFILE && clear'
alias godev='cd c:\\Dev && clear'
alias exp='explorer .'
alias gs='git status'
alias gss='git status -s'
alias gco='git commit -a -m $1'
alias gl='git log --oneline --graph --all --decorate'
alias gm='git merge --no-ff'
Some more advanced: http://haacked.com/archive/2014/07/28/github-flow-aliases/
Have not tried it myself, I manage without, but there are "libs" for it:
Don't. Just don't push ;-) Maybe start with an online tutorial like: https://try.github.io/levels/1/challenges/1
Quick usual sample for getting started. (Step through info exists below.)
git init demo
cd demo
git remote add origin https://username@github.com/username/demo
git add README.md
git commit -m 'Initial commit'
git push -u origin master
git remote add origin https://username@github.com/username/demo
The convention is to use origin
for the default remote one.
You can of course have MANY. Like a colleagues. Or if you have a public GitHub clone, a normal convention is upstream
for the original cloned repo.
Inspect them:
git remote
git remote -v
Local branches:
git branch
Remote branches:
git branch -r
Create a new branch locally
git branch develop
git checkout develop
or using one line:
git checkout -b feature/mytask
If you are in the branch you want to rename:
git branch -m feature/my-new-name
git branch -d feature/mybranch
If it has not been merged into master
, it will nag. If you really want to delete it:
git branch -D feature/mybranch
One way:
git push origin :feature/mybranch
Another:
git push origin --delete feature/mybranch
Some areas:
- Working directory
- Index (stage)
- HEAD (points to tip of branch)
Files aren't tracked by Git yet. This usually indicates a newly created file.
Files with changes that have not been prepared to be committed.
Files are ready to be committed.
File has been deleted and is waiting to be removed from Git.
Frequently check the status:
git status
or
git status -s
Use git add
to get files staged:
Stage new files and changes:
git add .
Individual:
git add readme.md
Expressions:
git add '*.cs'
Stage everything (new, changes, deletes)
git add . -A
or
git add --all
Used to persist staged changes locally.
git commit -m 'Comment'
The comment can span multiple lines. Is terminated by '
.
git commit -m `
Create rocket
Allows creation of rockets:
- Plain
- Super-seeking-death-rocket
'
Add and commit already tracked files:
git commit -a -m 'Oh dear'
If you have staged something that you want to regret:
git checkout myfile.txt
If you staged a new file:
git reset myfile.txt
If you want it gone, either delete it manually and stage the delete, or:
git rm myfile.txt -f
If it is commited, then rm
then commit
:
git rm myfile.txt
git commit -m 'bye, bye'
git pull
is the combination of git fetch + git merge
. You can do individual get fetch
calls. This is a non-destructive command. You can then e.g. start to inspect what you might be merging, by a subsequent call to git merge
.
git fetch
git log FETCH_HEAD
git diff origin
Tip. You can always refer to a specific remote: git fetch origin develop
. Works in more commands like: git pull
, git diff
...
Merge will merge others changes with your changes as they happen, in logged order. Rebase will rewind your changes to a common known/shared commit, then apply the remote changes then apply your changes.
So e.g. in Git flow:
At develop: git pull develop
At your feature: git rebase develop
Tip Try git rebase -i develop
.
You can also do stuff like: git pull --rebase
if you are collaborating with others on the same feature branch. On develop
and master
, this should not be needed.
When merging, if fast forward
is possible, then that is the default strategy. That means, your changes can be added on top (appended last in the log) and the header (reflog) can be switched to point to your last commit.
At develop: git merge feature/myfeature -m 'Close #32'
If you do want to force a non fast forward
, so that you e.g. see how your feature branch is merged back into develop:
At develop: git merge --no-ff feature/myfeature -m 'Close #32'
Getting stuff up to your remote:
git push -u origin develop
After this, since -u
will tell git to link branch to remote branch, when ever you are in develop
, you can shorten to:
git push
When you have working changes and want to get those changes reset but kept, you can stash (or put them in a branch):
git stash
git stash list
git stash apply
There are more commands to stash
. E.g. the destructive git stash clear
. Inspect the documentation/help.
To see changes...
git diff (same as: git diff HEAD)
git diff --staged
git diff develop
git diff HEAD^
git diff master
Check what has happened:
git log
git log --oneline
git log --oneline --graph --all --decorate