Skip to content
Kevin C edited this page Mar 8, 2018 · 7 revisions

GIT

This is a summary of typical git commands.

Also see the Git and Github Wiki.

Git Bash for windows

On window, install Git Bash

Setting User information

git config --global user.name "YOUR_USER_NAME_HERE"
git config --global user.email "YOUR_EMAIL_HERE"

Clearing User information

To remove the user.name and user.email configuration:

git config --global unset user.name
git config --global unset user.email

git clone

git clone copies a repository to a new local diretory

git clone <repository URL>

2017 Steamworks

git clone https://github.com/LdhssRobotics/frc-steamworks-robot-2017.git

2018 Power UP

git clone https://github.com/LdhssRobotics/frc-power-up-robot-2018.git

Typical Work Flow

The following commands summarizes a typical work flow:

git checkout -b <BRANCH_NAME>
<modify code>
git add <file>
git commit -m <description>
git push origin <BRANCH_NAME>

Create a branch

Use git checkout -b to switch to a new branch

git checkout -b <BRANCH_NAME>

Example:

user@ubuntu-frc:~/git/frc-power-up-robot-2018(master)$ git checkout -b compile-fix
Switched to a new branch 'compile-fix'
user@ubuntu-frc:~/git/frc-power-up-robot-2018(compile-fix)$ 

Modify your code

Code, compile test ...

View your changes

Use git status to view the files that were added, modified, or deleted

Example:

user@ubuntu-frc:~/git/frc-power-up-robot-2018(compile-fix)$ git status
On branch compile-fix
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

	modified:   src/Robot.h

no changes added to commit (use "git add" and/or "git commit -a")

View your code changes

Use git diff to view your code changes.

git diff <filename>

A '-' shows a deletion, or what was the previous version of the line of code A '+' shows an addition, or what is the current version of the line of code

Example:

user@ubuntu-frc:~/git/frc-power-up-robot-2018(compile-fix)$ git diff src/Robot.h
diff --git a/src/Robot.h b/src/Robot.h
index 37b257a..0fc8fc3 100644
--- a/src/Robot.h
+++ b/src/Robot.h
@@ -8,7 +8,7 @@
 #ifndef SRC_ROBOT_H_
 #define SRC_ROBOT_H_
 
-#include "WPIlib.h"
+#include "WPILib.h"
 #include "Commands/Command.h"
 #include "LiveWindow/LiveWindow.h"
 #include "RobotMap.h"
user@ubuntu-frc:~/git/frc-power-up-robot-2018(compile-fix)$ 

Add changes

Use git add prepare the modified files for submission

git add <filename>

Example:

user@ubuntu-frc:~/git/frc-power-up-robot-2018(compile-fix)$ git add src/Robot.h

Commit your change

Use git commit to commit your change

git commit -m <describe the code changes>

Use the "-m" option to add a commit message.

You can also set a default git editor that will be used for commit messages.

Example:

user@ubuntu-frc:~/git/frc-power-up-robot-2018(compile-fix)$ git commit -m "change #include WPIlib.h --> #include WPILib.h"
[compile-fix 6b42495] change #include WPIlib.h --> #include WPILib.h
 1 file changed, 1 insertion(+), 1 deletion(-)
user@ubuntu-frc:~/git/frc-power-up-robot-2018(compile-fix)$ 

View the change history

Use git log to changes made to the repository

Example:

user@ubuntu-frc:~/git/frc-power-up-robot-2018(compile-fix)$ git log
commit 6b42495544b39bdfc1efe399289f6832f999c4ee
Author: Kevin <fac153frc@gmail.com>
Date:   Mon Jan 22 22:20:15 2018 -0500

    change #include WPIlib.h --> #include WPILib.h

commit 2ddc6f2cac5b509743eda7024dbaaf223eafecac
Author: Kevin <fac153frc@gmail.com>
Date:   Mon Jan 22 21:26:40 2018 -0500

    add .gitignore

Upload your change to the server

Use git push to submit your change to the github server

git push origin <your_branch_name>

Example:

user@ubuntu-frc:~/git/frc-power-up-robot-2018(compile-fix)$ git push origin compile-fix
Username for 'https://github.com': 
Password for 'https://<your github username>@github.com': 
Counting objects: 4, done.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 363 bytes | 0 bytes/s, done.
Total 4 (delta 3), reused 0 (delta 0)
remote: Resolving deltas: 100% (3/3), completed with 3 local objects.
To https://github.com/LdhssRobotics/frc-power-up-robot-2018.git
 * [new branch]      compile-fix -> compile-fix
user@ubuntu-frc:~/git/frc-power-up-robot-2018(compile-fix)$ 

Updating your local repository from the remote repository

git checkout master
git fetch origin master
git pull

Use git checkout to switch to the master branch

Use git fetch to download the latest changes from the remote repository

Use git pull to apply the latest changes to the local master branch

Example:

user@ubuntu-frc:~/git/frc-power-up-robot-2018(build-fixes)$ git checkout master
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.

user@ubuntu-frc:~/git/frc-power-up-robot-2018(master)$ git fetch origin
remote: Counting objects: 35, done.
remote: Compressing objects: 100% (25/25), done.
remote: Total 35 (delta 20), reused 25 (delta 10), pack-reused 0
Unpacking objects: 100% (35/35), done.
From https://github.com/LdhssRobotics/frc-power-up-robot-2018
 * [new branch]      Drivetrain_2 -> origin/Drivetrain_2
 * [new branch]      SwingArm   -> origin/SwingArm

user@ubuntu-frc:~/git/frc-power-up-robot-2018(master)$ git pull
Already up-to-date.
user@ubuntu-frc:~/git/frc-power-up-robot-2018(master)$ 

Updating your branch with the latest from master

git checkout <BRANCH_NAME>
git merge master

Use git merge to update your working branch with the lastest from master

Example:

user@ubuntu-frc:~/git/frc-power-up-robot-2018(master)$ git checkout build-fixes 
Switched to branch 'build-fixes'
Your branch is up-to-date with 'origin/build-fixes'.

user@ubuntu-frc:~/git/frc-power-up-robot-2018(build-fixes)$ git merge master
Auto-merging src/RobotMap.cpp
CONFLICT (content): Merge conflict in src/RobotMap.cpp
Auto-merging src/Robot.h
Auto-merging src/OI.h
Removing Debug/src/Subsystems/ExampleSubsystem.o
Removing Debug/src/Subsystems/Arm.o
Removing Debug/src/Robot.o
Removing Debug/src/OI.o
Removing Debug/src/Commands/MyAutoCommand.o
Removing Debug/src/Commands/ExampleCommand.o
Removing Debug/src/Commands/AutoModes/LeftAutoMode.o
Removing Debug/src/Commands/AutoModes/CentreAutoMode.o
Removing Debug/FRCUserProgram
Automatic merge failed; fix conflicts and then commit the result.
user@ubuntu-frc:~/git/frc-power-up-robot-2018(build-fixes|MERGING)$ 

The following lines indicate a conflict in the merge

CONFLICT (content): Merge conflict in src/RobotMap.cpp
Automatic merge failed; fix conflicts and then commit the result.

This means that git was unable to merge the file from master into the file from your local branch because changes were made to the same code in the master branch and in your local branch.

You will need to resolve thee conflicts

Resolving Conflicts

git mergetool <filename>

Use git mergetool to resolve the conficts

No mergetool installed

Example:

user@ubuntu-frc:~/git/frc-power-up-robot-2018(build-fixes|MERGING)$ git mergetool src/RobotMap.cpp

This message is displayed because 'merge.tool' is not configured.
See 'git mergetool --tool-help' or 'git help config' for more details.
'git mergetool' will now attempt to use one of the following tools:
meld opendiff kdiff3 tkdiff xxdiff tortoisemerge gvimdiff diffuse diffmerge ecmerge p4merge araxis bc codecompare emerge vimdiff
Merging:
src/RobotMap.cpp

Normal merge conflict for 'src/RobotMap.cpp':
  {local}: modified file
  {remote}: modified file
Hit return to start merge resolution tool (bc): 
The merge tool bc is not available as 'bcompare'
user@ubuntu-frc:~/git/frc-power-up-robot-2018(build-fixes|MERGING)$ 

In this case, there is no known mergetool available.

user@ubuntu-frc:~/git/frc-power-up-robot-2018(build-fixes|MERGING)$ git mergetool --tool-help
'git mergetool --tool=<tool>' may be set to one of the following:
		araxis

The following tools are valid, but not currently available:
		bc
		bc3
		codecompare
		deltawalker
		diffmerge
		diffuse
		ecmerge
		emerge
		gvimdiff
		gvimdiff2
		gvimdiff3
		kdiff3
		meld
		opendiff
		p4merge
		tkdiff
		tortoisemerge
		vimdiff
		vimdiff2
		vimdiff3
		winmerge
		xxdiff

Some of the tools listed above only work in a windowed
environment. If run in a terminal-only session, they will fail.

Installing Meld

sudo apt-get install meld

Resolving conflicts with Meld

git config --global merge.tool meld

Example:

user@ubuntu-frc:~/git/frc-power-up-robot-2018(build-fixes|MERGING)$ git mergetool --tool=meld
Merging:
src/RobotMap.cpp

Normal merge conflict for 'src/RobotMap.cpp':
  {local}: modified file
  {remote}: modified file

git mergetool

The window labled "LOCAL" (left window) shows the local changes to the file in your branch before the merge The window labled "REMOTE" (right window) shows the latest change from the remote repository. The center window shows the current local copy with some changes merged into the local copy.

The red lines indicate the conflicts. The green lines indicate new lines. The blue lines indicate minor changes.

For the conflicts, select the version that you want to keep. When done, save the current file, and exit.

resolved

Use git merge --continue complete the merge

git merge --continue
user@ubuntu-frc:~/git/frc-power-up-robot-2018(build-fixes|MERGING)$ git merge --continue
[build-fixes 658fa91] Merge branch 'master' into build-fixes
user@ubuntu-frc:~/git/frc-power-up-robot-2018(build-fixes)$ 

If you get the following message

user@ubuntu-frc:~/git/frc-power-up-robot-2018(build-fixes|MERGING)$ git merge --continue
error: unknown option `continue'

Other Useful GIT commads

git cherry-pick

To pull a specific commit from one branch into another branch, use git cherry-pick. Each commit is identified by a SHA1 hash. Use the first 5 characters of the SHA1 hash in the git cherry-pick commmand

git cherry-pick <commit>

if necessary, resolve conflict

git cherry-pick --continue
git cherry-pick --abort

Use --continue after resolving conflicts Use --abort to cancel the cherry-pick command

undo the last commit

If you have committed some code, that you want to revert, use the git reset command:

git reset HEAD~

git stash

Use git stashto save your current changes without commiting themgit stash save

git stash list
git stash apply
git stash drop

use "save " to provide a comment about the code that is stashed use "list" to view the stashed changes use "apply" to apply the changes to the workspace without dropping the change use "drop" to drop the changes

Updating GIT

Then you should update git:

sudo apt-add-repository ppa:git-core/ppa
sudo apt-get update
sudo apt-get install git

Misc Configuration

Setting a default git editor

Use git config to set the default git editor to be used for commit messages

git config --global core.editor <editor>

Some popular editors are:

Displaying git branch in terminal window

Add the following to PS1 in .bashrc:

$(__git_ps1 "(%s)")

For Example:

Change:

PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '

to:

PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]$(__git_ps1 "(%s)")\$ '