Git Basics - seljukgulcan/git-notes GitHub Wiki

Getting a Repository

git init

git clone <url>

Basic Flow

Remember that each file in your working directory can be in one of two states: tracked or untracked. Tracked files are files that were in the last snapshot; they can be unmodified, modified, or staged. Untracked files are everything else – any files in your working directory that were not in your last snapshot and are not in your staging area. When you first clone a repository, all of your files will be tracked and unmodified because Git just checked them out and you haven’t edited anything.

When we commit a file, the version of the file at the time we run git add command is committed.

Lifecyle of status of files

git status -s

git status --short

gives brief status of files.

MM tracked-added-modified.txt
M  tracked-added.txt
 M tracked-modified.txt
AM untracked-added-and-modified.txt
A  untracked-added.txt
?? untracked.txt

First letter shows staged area, second letter shows working tree area (First one is green, other one is red). For example AM means, file is untracted and it is added and it is modified after adding the file so there are changes not added to staged area.

Gitignore

Glob patterns work: http://tldp.org/LDP/GNU-Linux-Tools-Summary/html/x11655.htm

Some examples:

# no .a files
*.a

# but do track lib.a, even though you're ignoring .a files above
!lib.a

# only ignore the TODO file in the current directory, not subdir/TODO
/TODO

# ignore all files in the build/ directory
build/

# ignore doc/notes.txt, but not doc/server/arch.txt
doc/*.txt

# ignore all .pdf files in the doc/ directory and any of its subdirectories
doc/**/*.pdf

GitHub maintains a fairly comprehensive list of good .gitignore file examples for dozens of projects and languages at https://github.com/github/gitignore if you want a starting point for your project.

git diff

git diff for comparing working directory with staging area (last commit if file is not added to staged area before)

git diff --cached or git diff --staged for comparing stage area with the last commit

git diff [--staged|--cached] [<filepath>]


git commit [-a] [-m <commit_message>]

git commit opens core.editor to edit your commit message before committing files.

git commit -a adds modified files (tracked files) to stage area and then commits.

git rm <path> removes the file from working directory and index (so it is no longer tracked)

git rm --cached <path> removes the file from index only (it stays on working directory)

git mv <source> <destination> to rename or move a file

Undoing things

git commit --amend is for making a edit to last commit (You commit and forgot to add a file)

git reset <path> is for unstaging a file without changing its content.

git checkout -- <path> for undoing changes on the file.

Remotes

git remote [-v] Lists remotes (-v also shows url of the remotes).

git remote add <shortname> <url>

git remote show <remote> Inspect a remote. This also shows added or deleted remote branches.

Local branches may be synced with remote branches (i.e. remote branches to be used when git pull and git push commands run). All these relations can be seen with git remote show <remote>

git remote rename <old_remote_name> <new_remote_name> renames remote name. Remote tracking branches also renamed.

git remote (rm | remove) <remote> Removes a remote.

Tags

git tag to list.

Note that the command above lists all tags in the repository. To list tags related to a branch, use :

git tag --merged <branch>

git tag -l "v2.0.* list all version tags starting with "v2.0."

There are two types of tag: lightweight and annonated. Lightweight is simple git tag <name> maps latest snapshot with this name, no other information is stored. Annonated tag also hold message, tag date and tagger information. git tag -a <name> -m <message>

git tag <tag> <commit> to tag a previous commit. <commit> hashsum of the commit or a part of it (First 4 characters at least?).

git push --tags This will transfer all of your tags to the remote server that are not already there. (TODO: to all remote servers?)

git push origin [tagname] shares tag with a remote.

git checkout -b version2 v2.0.0 creates a new branch at a specific tag with.

Git Alias

git config --global alias.ci commit

After that git ci and git commit are the same.

Examples:

git config --global alias.last 'log -1 HEAD'

git config --global alias.visual '!gitk' (to run external software)

Note: Surround with " in windows not with '

⚠️ **GitHub.com Fallback** ⚠️