Skip to content

Instantly share code, notes, and snippets.

@bbuyukkahraman
Last active December 30, 2017 08:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bbuyukkahraman/c76913a487f320b00385abc7cddc116e to your computer and use it in GitHub Desktop.
Save bbuyukkahraman/c76913a487f320b00385abc7cddc116e to your computer and use it in GitHub Desktop.
Git Command for Github
# Source Control Workflows in Xcode9
- Pull/push code
- Commit
- Branching
#Pull/Push code:
Clone/Pull: download code from remote
Push: put local-changes to remote
#Commits: Snapshot
- Code changes and descriptions
--Author
--Description Message
--Time/Date
* Commit3: Fixed a bug
* Commit2: Added a button
* Commit1: Init
#Branching:
- Master Branch : Latest stable version
* Commit3: Add a button
* Commit2: Add ViewController.swift
* Commit1: Init
- Development Branch : New features or risky code
- Merging two branches
# Git Commands
Basics :
- Terminal.app //Run
$ cd myRepo //Go to your Local Repo
$ echo "# testrepo" >> README.md //Create file
$ git init //Initiate Git process. It creates hidden .git directory
$ git add README.md //Get snapshot for a file. Or use "git add ." for all files in folder
$ git commit -m "My First Commit" //Still in your local
$ git remote add origin https://github.com/bbuyukkahraman/<repo-name>.git //Define remote repo
$ git push -u origin master //Sent to github
---------------------------------
#Setup :
$ git config --global user.name "bbuyukkahraman"
$ git config --global user.email "bulent@buyukkahraman.com"
$ git config --global color.ui true //Applying color (Optional)
#Initialize repo in existing directory :
- Go to project directory
$ git init //Creates .git subdirectory | Git skeleton | Tracking not started
$ git add <file> //To start version-control for file
$ git commit -m 'Initial project version'
#Check status of files :
$ git status
#Staging (Sahneleme) files: To stage changes for commit add file(s)
$ git add filename //Add file
$ git add -A //Add all files
$ git add . //Add all files changes in a directory
$ git add -p //Choose what changes to add
#Stashing (Saklama) files:
Command save local changes away and revert working directory to match HEAD commit.
$ git stash //Stash local-changes
$ git stash save "this is your custom message" //Stash local-changes with custom message
$ git stash apply //Re-apply changes you saved in your latest stash
$ git stash apply stash@{stash_number} //Re-apply changes you saved in given stash number
$ git stash drop stash@{0} //Drops any stash by its number
$ git stash pop //Apply stash and then immediately drop it from your stack
$ git stash pop stash@{stash_number} //Release particular stash from your list of stashes
$ git stash list //List all stashes
$ git stash show //Show latest stash changes
$ git diff stash@{0} //See diff details of a given stash number
#Committing (Islemek) files :
$ git commit -m 'commit message' //Commit staged file(s)
$ git commit filename -m 'commit message' //Add file and commit
$ git commit -am 'insert commit message' //Add file and commit staged file
$ git commit --amend 'new commit message' //Amending a commit
$ git rebase -i //Squashing commits together
$ git reset --soft HEAD~number_of_commits //Squashing commits together using reset --soft
$ git commit //WARNING: this will require force pushing commits, which is OK if this is on
#Branching and merging:
$ git checkout -b branchname //Create local-branch
$ git checkout - //Switch between 2 branch
$ git push -u origin branchname //Push local-branch to remote
$ git branch -d branchname //Delete local-branch
$ git branch -D branchname //Delete local-branch (even if it has not been me)
$ git remote prune origin //Remove any remote-refs you have locally removed from remote
$ git branch -a //View all-branch (local/remote)
$ git branch -a --merged //View all-branch merged into current-branch
$ git branch -a --no-merged //View all-branch not merged into current-branch
$ git branch //View local-branch
$ git branch -r //View remote-branch
$ git rebase origin/master //Rebase master-branch into local-branch
$ git push origin +branchname //Push local-branch after rebasing master into local branch
#Fetch and checkout remote branch:
$ git fetch origin //Fetch all remote-branch
$ git checkout -b test origin/test //With remote-branch
$ git branch -rd origin/branchname //Delete remote-branch
$ git push origin --delete branchname //Delete remote-branch
$ git push origin:branchname //Delete remote-branch
#Merging branch to trunk/master
$ git checkout trunk/master //First checkout trunk/master
$ git merge branchname //Now merge branch to trunk/master
$ git merge --abort //To cancel a merge
#Update local-repo with changes from Github-repo
$ git branch --set-upstream-to=origin/foo foo //Tracking existing branch
# Resetting:
$ git reset --mixed [sha] //Mix head with give sha , lets you do things like split commit
$ git reset HEAD origin/master -- filename //Upstream master
$ git reset HEAD -- filename //version from most recent commit
$ git reset HEAD^ -- filename //version before most recent commit
$ git reset --hard sha //Move head to specific commit
$ git reset --hard //Reset staging area and working directory to match most recent com
#Git remote:
$ git remote show origin //Show where 'origin' is pointing to and also tracked branches
$ git remote -v //Show where 'origin' is pointing to
$ git remote set-url origin https://github.com/user/repo.git //Change 'origin' remote-URL
$ git remote add [NAME] https://github.com/user/fork-repo.git //Add new 'origin', Usually use to 'rebase' from forks
#Git grep:
$ git grep 'something' //Search string in directory
$ git grep -n 'something' //Search string directory and -n prints out line
$ git grep -C<number of lines> 'something' //Search string in context
$ git grep -B<number of lines> 'something' //Search string and shows lines BEFORE grepped term
$ git grep -A<number of lines> 'something' //Search string and shows lines AFTER grepped term
#Git blame:
$ git blame [filename] //Show file-alteration-history with author-name
$ git blame [filename] -l //Show file-alteration-history with author-name && SHA
#Git log:
$ git log //commit-list in repo. Shows everything
$ git log -p //commit-list show messages and changes
$ git log -S 'something' //commit-list with particular expression looking for
$ git log --author 'Author Name' //commit-list by author
$ git log --oneline //commit-list in repo in more summarised way
$ git log --since=yesterday //commit-list in repo since yesterday
$ git log --grep "term" --author "name" //Shows log by author and search for specific term inside commit message
# Checking what you are committing :
$ git diff //See all (non-staged) changes done to local-repo
$ git diff --cached //See all (staged) changes done to local-repo
$ git diff --stat origin/master //Check what changes between files committed and live-repo
#Useful commands:
$ git tag --contains [sha] //Check if sha is in production
$ git shortlog -s --author 'Author Name' //Number of commits by author
$ git shortlog -s -n //List of authors and commits to repo sorted alphabetically
$ git shortlog -n --author 'Author Name' //List of commit comments by author, also shows total number of commits by author
$ git shortlog -s -n //Number of commits by contributors
$ git checkout -- filename //Undo local changes to File
$ git cat-file sha -p //Shows more detailed info about commit
$ git log --author="Author name" --pretty=tformat: --numstat --since=month //Show number of lines added and removed from repo by author since
#Useful alias:
open .gitconfig file on home directory and include alias code
# Shows log in more consisted way with graph for branching and mergin
lg = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %
#Another Tutorial
#Tell Git who you are:
$ git config --global user.name "Sam Smith" //author-name used with commits
$ git config --global user.email sam@example.com //email used with commits
#Create a new local-repo:
$ git init //Create new local-repo
#Check out repo:
$ git clone /path/to/repository //Create working copy of local-rep
$ git clone username@host:/path/to/repository //For remote server use
#Add files:
$ git add <filename> //Add one file to staging (index)
$ git add * //Add more files to staging (index)
#Commit:
$ git commit -m "Commit message" //Commit changes to head (but not yet to remote-repo
$ git commit -a //Commit any files added with git add, and also commit any files changed since then
#Push:
$ git push origin master //Send changes to master branch of remote-repo
#Status:
$ git status //List files you've changed and those you still need to add or commit
#Connect to remote repo:
$ git remote add origin <server> //Connect to remote-repo. add server to be able to push to it
$ git remote -v //List all currently configured remote-repo
# Branches:
$ git checkout -b <branchname> //Create new branch and switch to it
$ git checkout <branchname> //Switch from one branch to another
$ git branch //List all branches in your repo, and also tell you what branch you're currently in
$ git branch -d <branchname> //Delete feature branch
$ git push origin <branchname> //Push branch to remote-repo, so others can use it:
$ git push --all origin //Push all branches to remote-repo
$ git push origin :<branchname> //Delete branch on your remote-repo
#Update from remote repo:
$ git pull //Fetch and merge changes on the remote server to your working directory:
$ git merge <branchname> //To merge a different branch into your active branch:
$ git diff //View all the merge conflicts:
$ git diff --base <filename> //View the conflicts against the base file:
$ git diff <sourcebranch> <targetbranch> //Preview changes, before merging:
$ git add <filename> //After you have manually resolved any conflicts, you mark the changed file:
#Tags:
$ git tag 1.0.0 <commitID> //use tagging to mark a significant changeset, such as a release:
$ git log // CommitId is leading characters of changeset ID, up to 10, but must be unique. Get ID using
$ git push --tags origin //Push all tags to remote repo
#Undo local changes:
$ git checkout -- <filename> //replace changes in working tree with last content in head: Changes already added to index, as well as new files, will be kept.
$ git fetch origin //Instead, to drop all local changes and commits, fetch latest history from server and point local master branch at it, do this:
$ git reset --hard origin/master
#Search:
$ git grep "foo()" //Search working directory for foo()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment