Skip to content

Instantly share code, notes, and snippets.

@adini121
Last active January 28, 2016 12:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adini121/5e348029ddb834d6fb13 to your computer and use it in GitHub Desktop.
Save adini121/5e348029ddb834d6fb13 to your computer and use it in GitHub Desktop.
Useful git commands I encountered throughout my experience with Git

##Git commands I encountered throughout my git-experience

####Best Git practices

  1. Always review what's in the staging area before commiting to make sure you would only add/commit whats intended to be committed -> Saves you from pushing undesired objects to remote.
git status | git diff --cached
  1. Always fetch from remote before working in order to avoid (future) conflicts ####Regular git workflow for creating/adding/committing/pushing new files to (remote) repository
1. Under directory /gitfolder, create file example.txt 
2. git add example.txt 
3. git commit -m “Added example.txt” 
4. git push origin <branch under which you want example.txt> 

####Create a new repo locally AND remote too: https://gist.github.com/b766f9a65b258452d8da.git

####Sync REMOTE and FORKED repository: http://stackoverflow.com/questions/7244321/how-to-update-github-forked-repository

####Show which remote repo is your local repo commiting to:

git remote show origin 

###Everything relating Tags: ####List all tags:

git tag 

####Check which tag is currently checked out:

git describe --tags 

####Create a tag and annotate it:

git tag -a v1.4 -m 'my version 1.4'

####Push tags to remote:

git push origin <tag-name>
git push origin --tags 

Clone specific tag

This clones only the HEAD (not sure what if submodules are included)

git clone -b tag_name --single-branch depth=1 git@github.com:mozilla/olympia.git

###Everything relating branches: ####List all branches:

git branch 

####Delete branch locally + remotely

git branch -m old_branch new_branch         # Rename branch locally    
git push origin :old_branch                 # Delete the old branch    
git push --set-upstream origin new_branch   # Push the new branch, set local branch to track the new remote

####Create a new branch locally, add files to it, but its not seen in remote?

git push --set-upstream origin 
git remote update 

Or : git push -u origin <branch-name>

####Show only remote branches

git branch -r  
git ls-remote origin (*this command returns SHA hashes of latest commits of all remote branches*)

####Show local as well as remote branches

git branch -a 

####Create a branch and immediately switch to it git checkout -b <branch-name>

####DIFF a file against remote branch:

git diff <localbranch> <remotebranch> <filepath> 

####Clone only one branch:

git clone -b master --single-branch https://github.com/USER/install-scripts.git

####Clone the tip of all branches Git by default only clones HEAD of the repository - usually master. In order to clone all branches, do following:

git clone --depth 1 --no-single-branch  git@github.com:/USER/Repo.git
git fetch origin --depth 1 branchname:branchname

In this manner, tip of all specified branches can be cloned. Verified by git-log.

####Locally init a repository -> Add remote origin -> Fetch a remote branch -> Pull from it -> Add local changed -> Commit and push to this remote branch

git init
git remote add origin git@github.com:USER/test-reports.git
git fetch
git pull origin test-branch
git add .
git commit -m "test report"
git push https://USER:PASSWORD@github.com/USER/test-reports.git test-branch

####Creating a folder under some dirrectory ~/gitfolder

1. mkdir ~/gitfolder/sampleFolder 
2. add any files to ~/gitfolder/sampleFolder , say example2.txt and example3.txt 
3. cd ~/gitfolder
4. git add sampleFolder/. (adds eveything in sampleFolder: both example2.txt and example3.txt)
5. git commit -m "Created new folder and added its contents with this commit"
6. git push origin <branch>

####Git submodule cloning: git clone --recursive URL → clones with subbodules
..if you do another git pull after cloning, then:

  1. git submodule init
  2. git submodule update
    OR single command : git submodule update --init --recursive

####Uncommit a file after commiting it:

git reset HEAD <file> 

####Clone single branch:

git clone -b master --single-branch https://github.com/USER/install-scripts.git 

####Remove files such as .DS_Store and add to gitignore: find . -name .DS_Store -print0 | xargs -0 git rm -f --ignore-unmatch
Create .gitignore if not already present, add .DS_Store to it and then git commit + push

####Execute git commands into other directory

git -C /path/to/destination/dir <*usual git command*>  

e.g. git -C /Users/username/Desktop/ clone https://gist.github.com/gist-name.git

####Reverting published and unpublished (pushed and not-yet-pushed) commits This stackoverflow answer
[Another stackoverflow answer - concise version] (http://stackoverflow.com/a/22683231/4628603)

####Remove untracked files from local repository git clean -i(interactive) -f(force) -d(directory) -n(show what would be removed without removing it

####Workflow for applying changes from a particular commit to a particular branch [This Stackoverflow answer] (http://stackoverflow.com/a/17070336/4628603)

#####Specific to editing Jenkins Acceptance test suite:

Checkout a tag (E.g. 1.15) -> Now you're in detached HEAD state
Create a branch and switch to it -> git checkout -b infinity1.15
Cherry pick a commit #1 -> git cherry-pick C#1

If there are conflicts, do

git add <filepath/filename>
git cherry-pick --continue
git cherry-pick C#2
Display last commit message and commit hash

git show --summary

Get currently checked out tag/HEAD -> entire hash

git rev-parse --verify HEAD

Show changes after pull

git log -p -1 You can change -1 to -n for changes during last n commits

DIFF-ing
... your working copy and staging area:

% git diff
... staging area and the latest commit:

% git diff --staged
... your working copy and commit 4ac0a6733:

% git diff 4ac0a6733
... commit 4ac0a6733 and the latest commit:

% git diff 4ac0a6733 HEAD
... commit 4ac0a6733 and commit 826793951

% git diff 4ac0a6733 826793951
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment