Git Command Reference

Git Clean

Git Clean - Removes untracked files from your working directory

  • This is really more of a convenience command, since it’s trivial to see which files are untracked with git status and remove them manually. Like an ordinary rm command, git clean is not undoable, so make sure you really want to delete the untracked files before you run it.
  • https://www.atlassian.com/git/tutorial/undoing-changes#!clean

Git Rebase

Git Rebase - Forces git to push everything to master

git rebase <remote/repo/branch>

Git Remote

Git Remote - Add a remote repo

git remote add [alias] [url]
git remote add origin https://github.com/username/reponame.git

view all remotes and their addresses

git remote -v  

delete a remote repo

git remote rm origin  

git remote rm <destination>

Git Show

Git Show - Show code changes.

Git Add

Git Add - Add files to the GIT index to be tracked.

  • Add all files in the directory
git add 

git add <filename> - add file to files being tracked by git

  • Only need to add once. After that file will always be added to .git local repo

git add -u - looks at all the currently tracked files and stages the changes to those files if they are different or if they have been removed.

  • It does not add any new files, it only stages changes to already tracked files.

git add -A

  • same as git add
git add -u   

git rm --<filename>

  • remove an added file

Git Pull

Git Pull - pull all files from the repo

git pull origin master  

git pull --rebase origin master

Example, pull all files from the master branch of a repo on github

sudo git pull <reponame> master  

FORCE pull everything

  • Pull just one file - git pull <file>
  • See fetch section.

Git Push

Git Push - Push changes to the remote repository

git push origin master  

Example: When the repository is named repo1

sudo git remote -v
sudo git push repo1 master  
  • Check name of repo

git remote (tell git what the name of the remote server is)

git remote add nameOFremote path
git remote rm nameOFremote
remove a remote repo

git push nameOFremote branch

  • pushes changes to remote repo
  • ex.git push test master (remote repo = test, master branch)

Options for git push

  • -u link local repo with remote repo

If you cannot connect (permission error)

  • http://help.github.com/ssh-issues/
  • try command ssh -vT [email protected]
  • this will show if keys are being found
  • sudo uses a different key than the current user so connection may fail if you are using sudo. In almost all cases you should not be using the sudo command with git. If you have a very good reason you must use sudo, then ensure you are using it with every command (it's probably just better to use su to get a shell as root at that point). If you generate ssh keys without sudo, then when you try to use a command like sudo git push, you won't be using the ssh key you generated.
  • Compare the key fingerprints to github
    • ssh-keygen -lf ~/.ssh/jrkey.pub

list files to be pushed

git diff —stat origin master

force a push to the remote repo

git push -f

git push from server example

  • Sometime we get lazy, fix something directly on the server and push it back to the repo:
git add file.ext
git commit -m ‘fixed css margins’ (notice don’t need filename for the commit statement, don’t need origin master for the push statement)
git push

Git Diagrams

In software development, Git is a distributed revision control and source code management (SCM) system with an emphasis on speed. Git was initially designed and developed by Linus Torvalds for Linux kernel development in 2005.

Every Git working directory is a full-fledged repository with complete history and full version tracking capabilities, not dependent on network access or a central server.

Git is free software distributed under the terms of the GNU General Public License version 2.

fHM7iFBEtL1qKHCuNIp9z

Git Init

Git Init - Initialize the Repo

  • Initialize from WITHIN the directory that want to work. just type "git init"
  • bare

Git Fetch

Git Fetch <Remote/Repo Name> - The command goes out to that remote project and pulls down all the data from that remote project that you don’t have yet.

git fetch <Remote/Repo>

  • ex. git fetch origin master (does not change local working copy)

git diff master origin/master - Check the difference between your local copy of the master branch and the files that you fetched

git fetch --all - FORCE local to pull down remote files and overwrite all local files ( ** two dashes before all)

git fetch —all
git reset —hard origin/master

Git pull just one file

git fetch
git checkout -m <revision> <yourfilepath>
git add <yourfilepath>
git commit

http://blog.interlinked.org/tutorials/git.html

Check difference between workspace (Working Directory) and Index

git diff (will show actual file content changes)

Check difference between working directory and local repo

git diff HEAD (will show actual file content changes)

git log

get commits
git diff (commit number)

git merge

Merge you local repo with your local files (workspace). After fetching, you'll need to use merge to actually update your local files.

git merge origin/master (Here the origin = remote repo)

Check the difference between your local copy of the master branch and the files that you fetched

git diff master origin/master

Git Commit

Git Commit - Commit all changes to your local repo

  • GIT keeps it own copy of all the files that have been added to GIT using git add . When a change is made to a file, that change must be added to GIT's version of the files (local repo). This is done with a commit.
  • Add comment to file
  • Each commit is stored as a hash (view with GIT log)
  • files changed will show as 'modified: " under git status

commit one file

git commit path/filename

git reset

  • remove file from staging area

git reset --hard

  • put all changes back to last commit. Reverts changes of actual file. Be careful!

commit -a

  • commit all changes, don't use staging area

commit -m

sudo git commit -m 'removing jrtest.txt'(commits are LOCAL)
  • add message

Delete a commit

git reset --soft HEAD~1
  • This will revert the last commit.
  • run it again to get the second to last commit, or change the “1” to the number of commits you want to delete. ex, 3 for the last 3 commits.

Git Clone

git clone <url>

  • copy an entire project to local

To only get the most recent version of the files, suppress the default checkout of all files with

git clone -n git://path/to/the_repo.git --depth 1
  • depth 1 option means get only most recent version.

Put files in correct directory

  • ~~~ ./directory
  • Or add the directory name to end of statement and it will put all files in that directory. *The directory should not already exist. Run the command from the parent of the target directory. The user running this command must have permissions to create directories (without sudo)
git clone [email protected]:username/gitname.git ./directory

MAC issues

  • Possibly will show files as deleted. Use git status to check.
  • use git reset --hard to reset status of files.

Git Checkout

Git Checkout - Checkout a branch or paths to the working tree

use "git checkout -- ..." to discard changes in working directory)

checkout a branch so others can't edit it

checkout a file on a remote branch

git checkout remote/branch path/to/file
git checkout some_remote/branch32 conf/en/myscript.conf
git checkout some_remote/branch1 conf/fr/load.wav

Checkout a file on a remote branch to update the local repo

git checkout <commit>
git checkout c2dd73f

list all files being tracked by git

git ls-files

list all files for a specific branch

git ls-tree -r master --name-only
git ls-tree -r HEAD --name-only

Git Ignore

Git Ignore - tell git to ignore some files

  • ex. a settings file that only relates to the production server
  • Create the file .gitignore
  • add files to be ignored to this file
  • remove them from git for being tracked
git rm --cached <file>

Reset Head

git reset HEAD sites/default/files/sites/all/default/files/video/filename.mp4
  • remove a file that has already been committed.

https://help.github.com/articles/ignoring-files

http://gitready.com/beginner/2009/01/19/ignoring-files.html