Skip to content

GiuseppeFasanella/Git_commands

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

96 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Git_commands

A bunch of useful commands in git.

INSTALLING GIT

Linux : apt-get install git-core OR yum install git-core

USING GIT

git config --global user.name "Giuseppe Fasanella"

git config --global user.email my_email

git config --global core.editor "emacs" # Set editor as emacs

git config --list # Show settings

git help OR git help [COMMAND] OR git help add

---------------- Track a directory (You work in local)----------------

a. Go to directory

b. ls -a shows all files

c. git init # Creates the .git directory

---------------- Start tracking files ----------------

a. By type : git add *.java

b. By name : git add AndroidManifest.xml

  1. ---------------- Ignore Files ----------------

a. Create a .gitignore file

It's really useful: inside it you write ".C~" and it won't add the temporary files, for examples

My usual settings are: ".C~",".root",".txt"

------------------ Once ready:

git commit -m 'A meaningful comment'

---------------- git status ----------------

Shows the state of your files meaning if they are tracked, have been modified and the branch your on.

---------------- Stage A Modified File ----------------

Change the file and save

git diff # Shows what you changed, but haven't staged

git add AndroidManifest.xml # Stage file

git diff --cached # Shows what has been staged, but not committed

---------------- Commit the changes ----------------------- git commit -a -m 'Changed comment' # Skips staging and commit message

---------------- Remove a File ----------------

a. rm DeleteMe.txt # If you remove a file it shows as "Changed but not updated"

b. git status # If you remove a file it shows as "Changed but not updated"

c. git rm DeleteMe.txt

d. git status # Shows that the file was deleted

e. If you have committed a file to be removed you must add the -f option

f. git rm --cached DeleteMe.txt # Keep file, but remove from staging area

g. git mv DeleteMe.txt Delete.txt # Renames a file

---------------- Log Commit History ----------------

a. git log # Shows all of the previous commit messages in reverse order

b. git log --pretty=oneline # Shows commits on one line

c. git log --pretty=format:"%h : %an : %ar : %s"

I. %h - Abbreviated Hash

II. %an - Authors Name

III. %ar - Date Changed

IV. %s - First Line of Comment

d. git log -p -2 # Shows the last 2 commit changes

e. git log --stat # Prints abbreviated stats

f. git log --since=1.weeks # Show only changes in the last week

g. git log --since="2014-04-12" # Show changes since this date

h. git log --author="Derek Banas" # Changes made by author

i. git log --before="2014-04-13" # Changes made before this date

---------------- Undoing a Commit ----------------

a. git commit --amend # If you want to change your previous commit

b. Normally done if you forgot to stage a file, or to change the commit message

---------------- Unstage a File ----------------

a. git reset HEAD AndroidManifest.xml

---------------- GIT PART 2 ----------------

  1. GitHub allows you to host your code repositories online.

To push your directory to GitHub (If you have already a directory in local)

a. git init

b. git add . # Stages all new and modified files and directories

c. git commit -m 'Initial Project Version'

d. git remote add origin https://github.com/derekbanas/SimpleFragment.git #you can create a remote repository via browser on your github page

e. git push origin master

  1. git remote -v # Lists all remotes and their URLs

  2. git fetch origin # Gets data from the remote, but it doesn't merge changes with your work

  3. git pull https://github.com/derekbanas/SimpleFragment.git

a. Pulls all changes and saves them to your directory

  1. How to push changes to GitHub ----------------

a. I add .gitignore on my local machine

b. In the terminal type

I. git add .gitignore # Stage .gitignore

II. git commit -m 'Added .gitignore for Android' # Commit

III. git push # Push the changes to GitHub

  1. git remote rename origin sf # Renames remote to sf

  2. Tagging ----------------

a. Tags are used to tag files at important points in history

b. git tag -a v0.1 -m 'version 0.1' # Creates an annotated tag

c. git tag # Shows all the tags

d. git show v0.1 # Shows details about the commit that was tagged

e. git tag v0.4-lw

I. Creates a lightweight tag on a commit that stores the hash for the commit

f. git tag -a v0.01 c930a8

I. You can tag commits after the event also. When you enter this command an editor opens for you to leave a comment. The final part is the hash for the commit you want to tag.

g. git push sf v0.1 # You can also push tags. In this way you'll have different releases of your code in github

I. The tag shows up under releases on GitHub

II. git push sf --tags # You can also push all tags at once

h. You can set aliases to save time

I. git config --global alias.co commit

II. Now you can type git co to commit

i. Clone a GitHub Repository (If you have created a repository on-line or you have forked an existing one)

I. Go to the directory you want to use

II. git clone https://github.com/derekbanas/google-api-nodejs-client.git

---------------- GIT PART 3 ----------------

  1. Branching allows you to take a project in your own direction without effecting the main code. You use them to make sure you don't introduce unstable code to the master branch.

  2. When you commit a project :

a. Each file is given a hash code

b. A tree object that contains those files and the associated hash codes receives a hash code

c. A commit object stores a reference to the tree and other data like the author, commit comment, a reference to the previous commit and other data.

  1. The default branch is called the master. As you make additional commits the current newest version is referred to as the master.

  2. If you create a branch you can go in a different direction with the project without effecting the master until you merge. Each branch creates a new pointer to a committed version of files and doesn't make another copy of the files.

  3. We can actually create many branches, but be careful while doing this because it can get to be hard to merge multiple branches. A pointer known as HEAD can be pointed at any committed version or to any branch with the checkout command.

  4. When you are finished with your branch you can merge back into the master commit and move on. You can also do all of this locally or on a remote.

  5. ---------------- Simple Branch Example ----------------

a. git checkout -b fix20 # Create a branch and switch to it

I. Same as : git branch fix20 git checkout fix20

b. Change AndroidManifest.xml as you need and whish

c. git commit -a -m 'Added Branch fix20' # Commit the change to the branch, but not to master

d. git checkout master # Switch to master

e. git push origin fix20 # Push the branch to GitHub

f. git fetch origin # If someone else fetches from the server they get a reference to the branch on the server but not all the files

g. git checkout -b fix20 origin/fix20 # Retrieves the branch fix20

h. git branch # Shows all branches

i. git branch --merged # Shows all merged branches

j. git branch --no-merged # Shows unmerged branches

k. git branch -v # Shows all branches and their last commits

* Points out the branch currently checked out

l. (from master) git merge fix20 # Merge the branch version with the master (This is called fast-forward merge) git push # Push the change to GitHub

m. git branch -d fix20 # You can delete merged branches with this

n. git branch -D fix22 # Deletes unmerged branches

o. git push origin :fix20 # Deletes the branch on GitHub

p. git branch -m newBranchName # Renames a branch

  1. ---------------- Multiple Branch Example ----------------

a. a. git checkout -b fix21 # Create a branch and switch to it

b. Edit AndroidManifest.xml

c. git commit -a -m 'Added Branch fix21'

d. git checkout master # Switch to master

e. Look at Manifest to see that nothing changed

f. git checkout -b 'hotfix' # Create a new branch

g. git commit -a -m 'Added Hot Fix' # Commit the Hot Fix

h. git checkout master # Switch to master

i. git merge hotfix # Merge the hotfix version with the master

j. git branch -d hotfix # Delete the hotfix branch

k. git checkout fix21 # Switch to fix21 branch

l. git checkout master # Make sure you are in master

m. git merge fix21 # Merges the branch and master if there are no conflicts

n. If there is a conflict resolve it

o. git branch -d fix21 # Delete the unneeded branch git branch -D fix21 # To force delete

p. git mergetool # You can merge with a graphical tool

I. Backup : Contents of the file before calling the merge tool

II. Base : The common ancestor of the files being merged

III. Local : Version being pointed at by HEAD

IV. Remote : The branch being merged into head

  1. ---------------- Rebasing Example ----------------

a. Rebasing moves a branch to a new ( master / base ) commit. This is also referred to as a fast forward merge. Just never rebase commits that have been pushed to a public repository

b. git checkout -b fix22

c. Edit AndroidManifest.xml

d. git commit -a -m 'Changed the comment to 10'

e. git checkout -b hotfix

f. Edit another file other then AndroidManifest

g. git commit -a -m 'Edited file....'

h. git checkout master

i. git merge hotfix

j. git branch -d hotfix

k. git checkout fix22

l. git rebase master # Move branch to new master commit

m. git checkout master

n. git merge fix22

  1. ---------------- Reverting Vs. Resetting Example ----------------

a. Some times you want to eliminate a previous commit, but you still want to keep the commit for integrity reasons. Revert undoes changes made in that commit and makes a new commit. Reset actually deletes the commit which can cause problems.

b. Do something that will be undone

c. git commit -m 'Made a change that I will undo'

d. git revert HEAD # You are back to where you started, but the commit was made

e. Reset eliminates previous commits and you can never get them back. You really should never use it actually.

f. git reset someFile # Removes a file from the staging area, but leave the working directory unchanged

g. git reset # Reset the staging area to match the most recent commit while leaving the working directory unchanged

h. git reset aCommit # Move back to this previous commit, reseting the staging area, but not the working directory

i. git reset --hard # Reset both the staging area and working directory to match the most recent commit

j. git reset --hard aCommit # Move back to the commit listed and change staging and working directory

  1. ---------------- Clean Example ----------------

a. Clean removes untracked files from your directory and is undoable.

b. git clean -n # Shows which files will be removed

c. git clean -f # Remove untracked files

d. gir clean -df # Remove untracked files and untracked directories in the current directory

e. git reset --hard # Undoes changes on all tracked files git clean -df # Removes all untracked files

---------------- GIT PART 4 ----------------

  1. Here I'll demonstrate a workflow option with git called Fork 7 Pull. I'll also answer some questions I've received like how to work with multiple GitHub accounts on the same computer.

---------------- GENERATING SSH KEYS ----------------

  1. SSH keys allow you to identify trusted computers without the need for passwords and here I'll show you how to generate multiple codes for multiple GitHub accounts.

a. ssh-keygen -t rsa -C "Your Email Address" # Generates the key

  1. Then you have to define the name of the file you want to save the key in

  2. A public key and a randomart image are generated. The randomart image is provided because it is easier to recognize then a random string of numbers.

  3. cd ~/.ssh # Takes us to the location of our keys

  4. I'll open the public key with vim

  5. Now I'll copy the entire key from ssh-rsa till the end with my email

  6. Got to GitHub and sign in

  7. Click on account settings

  8. Click on SSH

  9. Give the key a name, paste in the key and click add key

  10. Your public key is then listed

---------------- CREATING MULTIPLE GITHUB ACCOUNTS ----------------

  1. Now I'll create another ssh key for a completely new account on GitHub ssh-keygen -t rsa -C "Your Email Address" # Generates the key

  2. Give it a name

  3. A public key and randomart are generated again

  4. cd ~/.ssh # Takes me to the location for the keys ls # Lists everything in the directory

  5. Go to GitHub again using a different GitHub account and click Account Settings

  6. vim newthinktank.pub # Get the new key that was generated

  7. Copy the key

  8. After you click SSH Keys in the sidebar on GitHub, paste the key in, give it a title and click Add Key

  9. We used a unique name for our keys so we have to tell ssh about them. ssh-add ~/.ssh/derekbanas ssh-add ~/.ssh/newthinktank

  10. touch ~/.ssh/config # Creates the empty file config vim config # Open config

  11. We are defining which account we want to work with by associating a keyword to our 2 different hosts.

Host github.com HostName github.com User git IdentityFile ~/.ssh/derekbanas

Host github-ntt HostName github.com User git IdentityFile ~/.ssh/newthinktank

  1. Change to the directory you want to use on GitHub

  2. This is the account on GitHub that holds the original master files

26 - 28. After I edit some files I stage them and commit them

  1. git remote add myorigin git@github-ntt:newthinktank/CrazyTipCalc.git

a. Create an alias for our remote directory

b. github-ntt # I identify myself using the newthinktank ssh key

c. newthinktank/CrazyTipCalc.git # The specific files I want on GitHub

  1. git push myorigin master When you try to push to GitHub you may see this warning. You can verify you are pushing to GitHub by comparing the public keys

31 - 34. You can see GitHubs public keys here https://help.github.com/articles/what-are-github-s-ssh-key-fingerprints

35 - 37. Log into GitHub using my derekbanas account and search for the directory I have associated with my newthinktank account

38 - 41. Get the URL from GitHub and I can clone it git clone https://github.com/newthinktank/CrazyTipCalc.git

About

A bunch of useful commands in git.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published