Once you have created an account on github, you will need to set it up so that you can use it with your linux account. First Step is to create a ssh private/public keys for using in github.
cd ~/.ssh
ssh-keygen -t rsa -C 'salasiapro@yahoo.com'
Now save this as id_rsa_github in the .ssh folder in your home directory You will now have in your .ssh folder a file called id_rsa_github This is the file we will need to copy the contents over to github.com under your ssh keys (provide link here). Now we create a config file in the .ssh folder to tell git which key you use when connecting to github gedit ~/.ssh/config
and add the contents
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_github
Setup Global Information for github
git config --global user.name "Firstname Lastname"
git config --global user.email "your_email@youremail.com"
git config --global github.user username
git config --global github.token 0123456789yourf0123456789token
Note you will use the token you get from the github Now its time to start a new git project.
Create a new project on git
Mine is called firstgitproject On the teminal, go to the local folder of your project. cd ~/Projects/firstgitproject
First Time Commit
git init
touch README
git add *
git commit -m 'first commit'
git remote add origin git@github.com:yourgitusername/firstgitproject.git
git push -u origin master
Warning, starting git 2.0 (mid 2013), git add will not include unstaged (deleted) files. If you want to stage deleted files under your current path then you need to use git add -u .
For the subsequent commits
you do git add file_that_has_changed
git commit or git commit -a
git push origin master
Adding a second remote
git remote add [alias] [url] note alias has to be unique (so if you used origin in the first remote, you will need to use something else for the alias)
Also not that url is preferable in the format of user@host:repo.git
Configure to push to multiple remotes
git remote set-url origin --push --add user1@host1:repo1.git
git remote set-url origin --push --add user2@host2:repo2.git
Display Configured remotes
git remote -v show
Changing remote to a different location
Delete the current remote reference with git remote rm origin Add the new remote git remote add origin (URL to new remote)
Pushing
initial push to new remote git push origin +HEAD
Cloning
Clone an already existing project https git clone https://github.com/nssy/Project ssh git@github.com:nssy/Project.git
Branching
Take a look at https://git-scm.com/book/en/v2/Git-Branching-Basic-Branching-and-Merging
Create and switch
git checkout -b new_branch_name
Long Form of creating and switching
git branch experiment
git checkout experiment
Make Changes git add .
git commit -m "my first branch commit"
git push origin experiment
Or if set globally to push current branch git push
Adding Remote Branches to local repo
git branch remote_branch origin/remote_branch
Deleting Branches
git branch -D branchname
git push origin --delete branchname
or git push [remotename] [localbranch]:[remotebranch]
If you leave off the [localbranch] portion, then you’re basically saying, “Take nothing on my side and make it be [remotebranch].” in short git push origin :branch
Start Tracking a Remote Branch
git branch --set-upstream new-remote-branch-name origin/new-remote-branch-name
Fetching & Pulling
The fetch commands only updates the origin/master. This means that you will need to merge the local branches manually. Note: Fetch does not affect your local files. It is recommended to always fetch often (Any time you want to work on your project). git fetch git pull mirrors the remote branch and the local branch you are currently in. It is equivalent of a fetch and a merge git pull
Merging
Switching back to branch you want to merge (for my case master) git checkout masterthen do a merge git merge experiment
git push
this will update the master branch with all the code from experiment branch
Fetch & Merge
git checkout master
git remote add kneath git://github.com/kneath/jobs.git
git fetch kneath
git merge kneath/error-page
git push origin master
Merging (Advanced)
supose we have two branches main is called nssy-dev and the updated one is called experimental. Further suppose that changes have been made on the same file in both branches. In such a case merging needs to be done in such a way that we end up an up to date file with both accepted changes from both branches. (Warning: Running git merge with uncommitted changes is discouraged: while possible, it leaves you in a state that is hard to back out of in the case of a conflict.) Begin the merge process git checkout nssy-devgit merge experimental Now you need to view all the changes git mergetool Merge Types merge tool candidates: meld opendiff kdiff3 tkdiff xxdiff tortoisemerge gvimdiff diffuse ecmerge p4merge araxis bc3 emerge vimdiff Abort a merge git merge --abort
Patch and Apply
The fetch and merge approach works great when you’re working on a team or repeatedly applying changes from the same small group of people. Another approach that’s a bit quicker in one-off cases is to use git-am. Every pull request has a .patch URL where you can grab a textual patch file to feed into the git-am command: git checkout master
curl https://github.com/github/jobs/pull/25.patch | git am
git push origin master
Take into account that if you are using a private repository, the patch cannot be downloaded anonymously by curl, so you have to save it through your browser, and execute: git checkout master
git am /location/of/patch/25.patch
git push origin master
Configuration
Set git Push only the current branch (globally) git config --global push.default current
Read more on Configuration
http://kernel.org/pub/software/scm/git/docs/git-config.html
Exclude folder of files from a commit
git rm -r --cached folder/* or you can create the file .gitignore and add the path to exclude into .gitignore (as a line)
Shows all the changes between the working directory and HEAD
git diff HEAD
Shows all the changes between the working directory and HEAD (which includes changes in the index). This shows all the changes since the last commit, whether or not they have been staged for commit or not.
Common Errors
If you receive an error like this: error: Your local changes to ‘project_name’ would be overwritten by merge. Aborting. Please, commit your changes or stash them before you can merge. You can ‘stash’ your local changes and revert them later by doing: git stash save "comment goes here - why am I saving this?" Then you can do a pull again. To restore the stash from earlier do: git stash listgit stash show stash@{0}or if you need to see as stash as a patchgit stash show -p stash@{0} Using Stashes git stash popNB: pop removes the particular stash from list of stashesgit stash applyNB: apply leaves a copy of the stash Delete Stashes git stash drop stash@{0} Clear stashes git stash clear
Reset Last commit
git reset --hard HEAD or git reset --hard HEAD~1 for an even earlier reset git reset --hard HEAD~nwhere n is the number of commits behind you want to move to or git reset --hard HEAD^^ then git push origin HEAD --force
Commands to explore
git log --graph --oneline --all --decorategit mergetool
Differenciate between branches/commits
git diff origin/master..master
View Commit log for a branch
git log --oneline origin/master
Move to a previous specific commit
Please be cautious about this as all subsequent commits will be removed. But new files will remain intact.
Uncommited changes will also be overwritten.git reset --hard db322e6where db322e6 is the commit id
Fix a commit before pushing
Make your changes then;
git add .
git commit --amend -m "your new message"Fix author information. Sometimes this is necessary when you find yourself commiting before setting up your git username and emailgit commit --amend --reset-author
Fix an older commit (that has been pushed)
Please be cautious about this as all subsequent commits will be removed. But new files will remain intact.
Uncommited changes will also be overwritten.git reset --hard -c <sha1>
git commit -m "The message you wanted to use"
git push -f
Then now do a full add and pushgit add
git commit -sm "Some intelligent message"
git push
Tagging
Listing Your Tags
git tag
Create Annotated Tags
git tag -a v1.4 -m 'my version 1.4'signed Annotated tagsgit tag -s v1.4 -m 'my version 1.4'
Create Lightweight Tags
git tag v1.4-lw
Tag a commit
git tag -a v1.2 -m 'version 1.2' 9fceb02
Sharing Tags
By default, the git push command doesn’t transfer tags to remote servers. you need to specify already created tag when pushing to remote git push origin v1.5
Push all tags
git push origin --tags
Delete tag
git tag -d v1.1
Enable Pushing of git commits & tags simultaneously git config --global push.followTags true
Git Aliases
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
git config --global alias.visual '!gitg'
git config --global alias.a "add -A"
git config --global alias.logg "log --graph --decorate --oneline --abbrev-commit"
More Aliases (For viewing pretty Logs)
git config --global alias.lg "log --graph --pretty=format:'%Cred%h%Creset - %C(bold blue)(%cr) %C(bold cyan)%ad %C(bold white)%d%Creset %s %C(bold yellow)<%an>%Creset' --abbrev-commit --date=short"
git config --global alias.l "log --date-order --date=short --graph --full-history --pretty=format:'%x08%x09%C(red)%h %C(bold cyan)%ad %C(bold blue)(%cr) %C(bold yellow)<%aN>%C(reset)%C(bold yellow)%d %C(reset)%s'"
More Aliases (A more useful git diff)
git config --global alias.truediff "diff --ignore-space-at-eol -b -w --ignore-blank-lines"
Other useful Aliases (My own Preference)
git config --global alias.master "checkout master"
git config --global alias.ss "stash save"
git config --global alias.sp "stash pop"
Amend with the same message (very convenient)
git config --global alias.amend "log -n 1 --pretty=tformat:%s%n%n%b | git commit -F - --amend"
Unstage a file
git config --global alias.unstage "restore --staged"
Other useful commands
git remote -v
git branch -r
git branch -a
git checkout -b branch_name origin/branch_name
git push origin --delete branch_to_delete
git log -p branch_name..origin/branch_name
git log --format="%an" version1..HEAD | sort -u
grep git commits for a certain word
If you want to find all commits where commit message contains given word, use git log --grep=word for all branches git log --all --grep=word If you want to find all commits where “word” was added or removed (to be more exact: where number of occurences of “word” changed), i.e. search the commit contents, use so called ‘pickaxe’ search with git log -Sword to look for differences whose added or removed line matches “word” (also commit contents). git log -Gword
check out a pull request
When someone sends you a pull request from a fork or branch of your repository, you can merge it locally to resolve a merge conflict or to test and verify the changes before merging it
Reference: Checking out pull requests locally
Long story short, you do
git fetch origin pull/1/head:pr1-author-x
git will create a new branch pr1-author-x in your local clone with the contents of pull/1/head
Migrating origins
Say you have a local git repository or you want to move all git info eg. commits branches etc.
Create a bare project on the server you want to host and get the git url for it
eg.
git@gitlab.com:nssy/imported-project.git
Go to local git folder
Now change the remote with the command:
git remote set-url origin git@gitlab.com:nssy/imported-project.git
You can view the remotes with the command:
git remote -v
After that you will need to push all the branches
Start by pushing to master with the command:
git push -u origin master
After that you will need to checkout all other subsequet branches and push the as you wish
Useful Resources
GIT Basics on Tagging
Git config powerup with aliases, diff & log
Advanced Alias Examples
2.7 Git Basics – Tips and Tricks
More advanced aliases