Commit And Push Your Working Code With Git

=Branches = Git allows you to create branches, e.g. independent copies of the source code which can be changed independently from one another. The default branch is named "master".
 * git branch
 * This command lists all locally available branches. The currently active branch is marked with *.


 * git branch -a
 * If you want to see all branches (including remote branches), use this command.


 * git branch 
 * Create a new branch.

 Syntax: git branch 
 * in the above is optional
 * If not specified, the last commit will be used
 * If specified, the corresponding commit will be used


 * git checkout 
 * Switch to your new branch


 * git branch -d 
 * Delete a branch which is not needed anymore.

If you want to check if a branch has been deleted, use Tthe git branch command. =Pull Changes =

Pull allows you to get the latest changes from another repository. To try this out, switch to your second repository, make some changes, Now push them to your remote repository and pull these changes to your first repository. cd ~
 * 1) Switch to home

cd ~/repo02 echo "A change" > test01 git commit -a -m "A change" git push origin cd ~/repo01 git pull ../remote-repository.git/ less test01
 * 1) Switch to second directory
 * 1) Make changes
 * 1) Commit
 * 1) Push changes to remote repository
 * 2) Origin is automatically maintained as we cloned from this repository
 * 1) Switch to the first repository and pull in the changes
 * 1) Check the changes

=Push a Branch To Remote Repository = By default, Git will only push matching branches to a remote repository. That means that you have to manually push a new branch once. Afterwards, "git push" will also push the new branch. git push origin testing
 * 1) Push testing branch to remote repository

git checkout testing
 * 1) Switch to the testing branch

echo "News for you" > test01 git commit -a -m "new feature in branch"
 * 1) Some changes

git push This way, you can decid which branches should be visible to other repositories and which should be local branches.
 * 1) Push all including branch

=Merging = Merge allows you to combine the changes of two branches. Merge performs a so-called three-way-merge between the latest snapshot of two branches, based on the most recent common ancestor of both.

As a result, you have a new snapshot. You can merge changes from one branch to the current active one via the following command.

Syntax: git merge 

git merge testing 

If a merge conflict occurs, Git will mark the conflict in the file and the programmer has to resolve the conflict manually. After resolving it, he can add the file to the staging index and commit the change.

=Committing =
 * git commit -m "Initial commit"
 * Commit your file to the local repository, include a message.


 * git add . && git commit -m "insert commit message here"
 * Add the changes to the index and commit


 * git commit -a -m "These are new changes"
 * Commit the changes,-a will commit changes for modified files, but will not add automatically new files.


 * git commit --amend -m "More changes - now correct"
 * Correction of commit messages - git amend

=Revert a commit = git revert If you deleted a file but have not yet added it to the index or committed the change, you can check out the file out again. rm test01 git checkout test01 If you added a file to the index but do not want to commit the file, you can remove it from the index via the git reset file command. // Create a file touch incorrect.txt // Accidently add it to the index git add. // Remove it from the index git reset incorrect.txt // Delete the file rm incorrect.txt If you deleted a directory and you have not yet committed the changes, you can restore the directory via the following command:
 * 1) Delete a file
 * 1) Revert the deletion

git checkout HEAD --  

=Create or Delete files =

Create a File And Put It Under Version Control

 * git add.
 * Add all (files and directories) to the Git repository

touch nonsense.txt git add. && git commit -m "a new file has been created"

echo "Cool new feature in this branch" > test01 git commit -a -m "new feature" git checkout master cat test01
 * 1) Some changes
 * 1) Switch to the master branch
 * 1) Check that the content of test01 is the old one

Delete a File and Commit
If you delete a file which is under version control, git add will not pick this file up. You need to use the git commit command with the -a flag or the -A flag in the git add command.

rm nonsense.txt git add. && git commit -m "a new file has been created" git commit -a -m "File nonsense.txt is now removed" git add -A. git commit -m "File nonsense.txt is now removed"
 * 1) Remove the file
 * 1) Try standard way of committing -> will not work
 * 1) Now commit with the -a flag
 * 1) Alternatively you could add deleted files to the staging index via

=Revert Changes = If you create files in your working copy which you do not want to commit, you can discard them. touch test04 echo "this is trash" > test04
 * 1) Create a new file with content

git clean -n
 * 1) Make a dry-run to see what would happen
 * 2) -n is the same as --dry-run

git clean -f You can check out older revisions of your source code via the commit ID. The commit ID is shown if you enter the git log command. It is displayed behind the commit word. cd ~/repo01 git log
 * 1) Now delete
 * 1) Switch to home
 * 1) Get the log

git checkout commit_name
 * 1) Copy one of the older commits and checkout the older revision via

If you have not yet added the changes to the staging index, you can also revert the changes directly. echo "nonsense change" > test01 git checkout test01 cat test01 echo "another nonsense change" > test01 git add test01 git reset HEAD test01 git checkout test01
 * 1) Some nonsense change
 * 1) Not added to the staging index. Therefore we can
 * 2) just checkout the old version
 * 1) Check the result
 * 1) Another nonsense change
 * 1) We add the file to the staging index
 * 1) Restore the file in the staging index
 * 1) Get the old version from the staging index

=Push Changes To Another Repository = Make some changes and push them from your first repository to the remote repository via the following commands. cd ~/repo01
 * 1) Make some changes in the first repository

echo "Hello, hello. Turn your radio on" > test01 echo "Bye, bye. Turn your radio off" > test02
 * 1) Make some changes in the file

git commit -a -m "Some changes"
 * 1) Commit the changes, -a will commit changes for modified files
 * 2) but will not add automatically new files

git push ../remote-repository.git
 * 1) Push the changes

=Show Log and Status =
 * git log
 * Displays the log file. Shows the history of commits in the current branch


 * git diff
 * Check for changes via the diff command. Shows the differences between the uncommitted files and the last commit in the current branch.


 * git status
 * View the current status of your repository (which files are changed / new / deleted)


 * gitk --all
 * Creates graphical representation of the changes

= List of Frequently Used Commands = git branch

git branch mytopic

git checkout master

git checkout mytopic

git commit -m "my message."

git pull

git pull --rebase

git rebase master

git rebase --continue

git merge mytopic

git push

Home